<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.complainant"></el-input>
                </el-form-item>
                <el-form-item label="投诉内容" >
                    <el-input v-model="form.content"></el-input>
                </el-form-item>
                <el-form-item label="投诉日期">
                    <el-date-picker
                        v-model="form.date_occurrence"
                        type="date"
                        placeholder="选择日期"
                        value-format="yyyy-MM-dd"
                        style="width:50%"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="处理结果反馈日期" >
                    <el-date-picker
                        v-model="form.date_feedback"
                        type="date"
                        placeholder="处理结果反馈日期"
                        value-format="yyyy-MM-dd"
                        style="width:50%"
                    >
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="客户是否满意" >
                    <el-switch
                        v-model="form.is_satisfied"
                        active-text="满意"
                        inactive-text="不满意">
                    </el-switch>
                </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 {createComplaint,updateComplaint} from "@/api/task";
  export default {
    emits: ["onSubmit", "onCancel"],
    data() {
      return {
        form:{
            complainant: "",
            content: "",
            date_occurrence:"",
            date_feedback: "",
            is_satisfied: true,
            task2do: 0
        },
        titleOption: {
            add: "新增客户投诉",
            edit: "客户投诉编辑",
        },
        isSaveing:false,
        dialogVisible:false,
        rules: {
        number: [{ required: true, message: "请填入" }],
        vchannel: [{ required: true, message: "请选择视频" }],
      },
      }
    },
    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") {
                        createComplaint(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") {
                        createComplaint(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>
  
  