<template>
    <div class="addDataModel">
        <el-dialog title="编辑页面" width="800px" center :close-on-click-modal="false"
                   :visible.sync="editData.editVisible">
            <el-form :label-width="widthStyle" :rules="rules" ref="editFormData" :model="editFormData">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="评价名称:" prop="pjmc">
                            <el-input v-model="editFormData.pjmc" clearable required></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="评价日期:" prop="pjrq">
                            <el-date-picker v-model="editFormData.pjrq" type="date" value-format="yyyyMMdd" clearable
                                            required></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="评价年度:" prop="pjnd">
                            <el-input v-model="editFormData.pjnd" clearable required></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="评价有效期:" prop="pjyxq">
                            <el-date-picker v-model="editFormData.pjyxq" type="date" value-format="yyyyMMdd" clearable
                                            required></el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="发布时间:" prop="fbsj">
                            <el-date-picker v-model="editFormData.fbsj" type="date" value-format="yyyyMMdd" clearable
                                            required></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="评价机关全称:" prop="pjjgqc">
                            <el-input v-model="editFormData.pjjgqc" clearable required></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="评价机关统一社会信用代码:" prop="pjjgtyshxydm"
                                      style="text-align:left;float: none;word-break: break-word;">
                            <el-input v-model="editFormData.pjjgtyshxydm" clearable required></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item></el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="btnSave(0, 'editFormData')" class="iconfont iconbaocun">保存</el-button>
                <el-button type="primary" @click="btnSave(1, 'editFormData')" class="iconfont icontijiao">提交</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {editById, updateBatch} from '@/api/grainCoManage/enterpriseCredit/index';

    export default {
        // 接收列表页参数
        props: {
            editData: Object
        },
        data() {
            return {
                // 详情参数定义
                editFormData: {},
                widthStyle: "150px",
                // 表单非空效验
                rules: {
                    pjmc: [{required: true, message: '评价名称', trigger: 'blur'}],
                    pjrq: [{required: true, message: '评价日期', trigger: 'blur'}],
                    pjnd: [{required: true, message: '评价年度', trigger: 'blur'}],
                    pjyxq: [{required: true, message: '评价有效期', trigger: 'blur'}],
                    fbsj: [{required: true, message: '发布时间', trigger: 'blur'}],
                    pjjgqc: [{required: true, message: '评价机关全称', trigger: 'blur'}],
                    pjjgtyshxydm: [{required: true, message: '评价机关统一社会信用代码', trigger: 'blur'}]
                },
            }
        },
        created() {
            this.getInfo(this.editData)
        },
        methods: {
            getInfo(editData) {
                // console.log("editData==>", editData);
                this.editFormData = JSON.parse(JSON.stringify(editData.editFormDataC));
            },
            btnSave(type, formName) {
                // 编辑所有数据(ids为空编辑所有)
                if (this.editData.ids === 'plbj') {
                    // console.log("====编辑所有====", this.editData);
                    this.editData.ids = "";
                    this.$confirm('您当前操作为批量编辑所有数据,是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.updateBatchUtil(type, formName);
                    }).catch(() => {
                        this.$message({type: 'info', message: '已取消编辑'});
                    });
                } else if (this.editData.ids === '') { // 单条编辑
                    // console.log("====根据id编辑====");
                    this.updateUtil(type, formName);
                } else { // 根据ids编辑,批量编辑
                    // console.log("====根据ids编辑====");
                    this.updateBatchUtil(type, formName);
                }
            },
            // 单条修改接口
            updateUtil(type, formName) {
                let zt;
                if (type === 0) { // 保存
                    this.editFormData.zt = '待提交';
                    zt = "保存成功"
                }
                if (type === 1) { // 提交
                    this.editFormData.zt = '已提交';
                    zt = '提交成功';
                }
                // 获取效验
                this.$refs[formName].validate((valid, dom) => {
                    // 表单效验
                    if (valid) {
                        editById(this.editFormData).then(res => {
                            // console.log("res--->", res);
                            if (res.infoCode === 200) {
                                this.$message.success(zt);
                                this.editData.editVisible = false;
                                // this.$emit("refreshList");
                                this.$parent.refreshList();
                            } else {
                                this.$message.error(res.data);
                            }
                        }).catch((err) => {
                            this.$message.error(err.data);
                            this.editData.editVisible = false;
                        });
                    }
                });
            },
            // 批量编辑
            updateBatchUtil(type, formName) {
                let plzt;
                if (type === 0) { // 保存
                    this.editFormData.zt = '待提交';
                    plzt = "批量保存成功";
                }
                if (type === 1) { // 提交
                    this.editFormData.zt = '已提交';
                    plzt = "批量提交成功";
                }
                this.$refs[formName].validate((valid, dom) => {
                    // 表单效验
                    if (valid) {
                        // console.log("ids==>", this.editData.ids);
                        // console.log("editFormData==>", this.editFormData);
                        updateBatch(this.editData.ids, this.editFormData).then(res => {
                            // console.log("res==>", res);
                            if (res.infoCode === 200) {
                                this.editData.editVisible = false;
                                this.$message.success(plzt);
                                this.$parent.refreshList();
                            } else {
                                this.$message.error(res.data);
                            }
                        }).catch((err) => {
                            this.$message.error(err.data);
                            this.editData.editVisible = false;
                        });
                    }
                });
            },
        }
    }
</script>

<style scoped>

</style>