<template>
    <div class="">
        <el-dialog
            :title="titleOption[type]"
            :visible.sync="dialogVisible"
            @closed="$emit('closed')"
        >
            <el-form
              ref="dialogForm"
              :model="form"
              label-width="100px"
              label-position="right"
              :rules="rules"
            >
                <el-form-item label="检查类型" >
                    <el-select
                        v-model="form.cate"
                        placeholder="检查类型"
                    >
                    <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.key"
                        :value="item.value"
                    >
                    </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="检查机构" >
                    <el-input v-model="form.checker"></el-input>
                </el-form-item>
                <el-form-item label="检查日期" >
                    <el-date-picker
                        v-model="form.date_inspect"
                        type="date"
                        placeholder="选择日期"
                        value-format="yyyy-MM-dd"
                        style="width:50%"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="检查结果" >
                    <el-input v-model="form.result"></el-input>
                </el-form-item>
            </el-form>
            <div style="text-align: right">
                <el-button type="danger" @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" :loading="isSaveing" @click="onSubmit('Form')">确认</el-button>
            </div>
        </el-dialog>
    </div>
  </template>
  
  <script>
  import { createOinspect,updateOinspect} from "@/api/task";
  export default {
    emits: ["onSubmit", "onCancel"],
    data() {
      return {
        form:{
            checker: "",
            result: "",
            date_inspect: "",
            cate: 10,
            task2do: 0
        },
        titleOption: {
            add: "新增外部监督检查",
            edit: "外部监督检查编辑",
        },
        isSaveing:false,
        dialogVisible:false,
        rules: {
            number: [{ required: true, message: "请填入" }],
            vchannel: [{ required: true, message: "请选择视频" }],
        },
        options:[
            {key:'市场监管部门检查',value:10},
            {key:'行业主管部门检查',value:20},
            {key:'其他检查',value:30}
        ],
      }
    },
    methods: {
        //显示
        open(type = "add",task2do) {
            this.type = type;
            if(task2do){
                this.form.task2do = task2do;
            }
            this.dialogVisible = true;
            return this;
        },
        //表单注入数据
        setData(data) {
            Object.assign(this.form, data);
            debugger;
            console.log(this.form);
        },
        onSubmit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                this.isSaveing = true;
                try {
                    var res;
                    if (this.type == "add") {
                        createOinspect(this.form).then(res=>{
                            this.isSaveing = false;
                            if(res.code>=200){
                                this.dialogVisible = false;
                                this.$emit("success",this.type);
                            }else{
                                this.$message.warning(res.data.msg);
                            }
                        })
                    } else if (this.type == "edit") {
                        updateOinspect(this.form.id,this.form).then(res=>{
                            this.isSaveing = false;
                            if(res.code>=200){
                                this.dialogVisible = false;
                                this.$emit("success",this.type);
                            }else{
                                this.$message.warning(res.data.msg);
                            }
                        })
                    }
                    return res;
                } catch (err) {
                    //可以处理校验错误
                    this.isSaveing = false;
                    return err;
                }
                }
            });
        },
        onCancel() {
            this.$emit("closed");
        }
    }
  }
  </script>
  
  <style scoped>
  .line{
    text-align: center;
  }
  </style>
  
  