<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-input v-model="form.content"></el-input>
                </el-form-item>
                <el-form-item label="风险类别" >
                    <el-select
                        v-model="form.level"
                        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.code"></el-input>
                </el-form-item>
                <el-form-item label="整改情况" >
                    <el-select
                        v-model="form.state"
                        placeholder="整改情况"
                    >
                    <el-option
                        v-for="item in stateOptions"
                        :key="item.value"
                        :label="item.key"
                        :value="item.value"
                    >
                    </el-option>
                    </el-select>
                </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 {createRisk,updateRisk} from "@/api/task";
  export default {
    emits: ["onSubmit", "onCancel"],
    data() {
      return {
        form:{
            content: "",
            code: "",
            level: 10,
            state: 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}
        ],
        stateOptions:[
            {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") {
                        createRisk(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") {
                        updateRisk(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>
  
  