<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.name"></el-input>
                </el-form-item>
                <el-form-item label="项目编号" >
                    <el-input v-model="form.number"></el-input>
                </el-form-item>
                <el-form-item label="组织方" >
                    <el-input v-model="form.organizer"></el-input>
                </el-form-item>
                <el-form-item label="参加参数名称" >
                    <el-input v-model="form.params"></el-input>
                </el-form-item>
                <el-form-item label="结果" >
                    <el-select
                        v-model="form.result"
                        placeholder="结果"
                        @change="resultChange"
                    >
                    <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-select
                        v-model="form.handle_result"
                        placeholder="结果处理状况"
                        :disabled="form.result==10"
                    >
                    <el-option
                        v-for="item in handleOptions"
                        :key="item.value"
                        :label="item.key"
                        :value="item.value"
                    >
                    </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="证书日期" >
                    <el-date-picker
                        v-model="form.certificate_date"
                        type="date"
                        placeholder="选择日期"
                        value-format="yyyy-MM-dd"
                        style="width:50%"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="领域" >
                    <el-select
                        v-model="form.field"
                        placeholder="领域"
                    >
                    <el-option
                        v-for="item in fields"
                        :key="item.value"
                        :label="item.key"
                        :value="item.value"
                    >
                    </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否A类" >
                    <el-select
                        v-model="form.a_class"
                        placeholder="是否A类"
                    >
                    <el-option
                        v-for="item in a_classes"
                        :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 {createPt,updatePt} from "@/api/task";
import { options } from "runjs";
  export default {
    emits: ["onSubmit", "onCancel"],
    data() {
      return {
        form:{
            name: "",
            number: "",
            organizer: "",
            params: "",
            result: 10,
            handle_result:0,
            task2do: 0
        },
        titleOption: {
            add: "新增能力验证结果",
            edit: "能力验证结果编辑",
        },
        options:[
            {key:'满意',value:10},
            {key:'有问题',value:20},
            {key:'不满意',value:30}
        ],
        fields:[
            {key:'建工',value:0},
            {key:'建材',value:10},
            {key:'环境',value:20},
            {key:'食农',value:30}
        ],
        handleOptions:[
            {key:'/',value:0},
            {key:'已整改',value:10},
            {key:'被暂停',value:20},
            {key:'已被撤销认可资格',value:30}
        ],
        a_classes:[{key:'是',value:true},{key:'否',value:false}],
        isSaveing:false,
        dialogVisible:false,
        rules: {
        number: [{ required: true, message: "请填入" }],
        vchannel: [{ required: true, message: "请选择视频" }],
      },
      }
    },
    methods: {
        //显示
        open(type = "add",task2do) {
            // debugger;
            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);
        },
        resultChange(value){
            debugger;
            console.log(value)
            if(value==10){
                this.form.handle_result = 0;
            }
        },
        onSubmit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                this.isSaveing = true;
                try {
                    var res;
                    if (this.type == "add") {
                        createPt(this.form).then(res=>{
                            this.isSaveing = false;
                            if(res.code>=200){
                                debugger;
                                this.dialogVisible = false;
                                this.$emit("success");
                            }else{
                                this.$message.warning(res.data.msg);
                            }
                        })
                    } else if (this.type == "edit") {
                        updatePt(this.form.id,this.form).then(res=>{
                            this.isSaveing = false;
                            if(res.code>=200){
                                debugger;
                                this.dialogVisible = false;
                                this.$emit("success");
                            }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>
  
  