<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-04-19 15:29:42
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
    <el-form-item prop="evaluationTemplateId" :label="$t('supplier.evaluationTemplateName')">
      <el-select v-model="dataForm.evaluationTemplateId" :placeholder="$t('supplier.evaluationTemplateName')"
        @change="getProductCode">
        <el-option v-for="item in templateList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="evaluationType" :label="$t('supplier.evaluationType')">
      <el-select v-model="dataForm.evaluationType" :placeholder="$t('supplier.evaluationType')">
        <el-option v-for="item in evaluationTypeList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="title" :label="$t('supplier.title')">
      <el-input v-model="dataForm.title" :placeholder="$t('supplier.title')">
      </el-input>
    </el-form-item>
    <el-form-item prop="description" :label="$t('supplier.description')">
      <el-input v-model="dataForm.description" :placeholder="$t('supplier.description')">
      </el-input>
    </el-form-item>
    <el-form-item prop="startTime" :label="$t('supplier.startTime')">
      <el-date-picker v-model="dataForm.startTime" type="datetime" format='yyyy-MM-dd HH:mm:ss'
        valueFormat='yyyy-MM-ddTHH:mm:ss' :placeholder="$t('supplier.startTime')">
      </el-date-picker>
    </el-form-item>
    <el-form-item prop="evaluationPeriod" :label="$t('supplier.evaluationPeriod')">
      <el-date-picker v-model="dataForm.evaluationPeriod" type="datetime" format='yyyy-MM-dd HH:mm:ss'
        valueFormat='yyyy-MM-ddTHH:mm:ss' :placeholder="$t('supplier.evaluationPeriod')">
      </el-date-picker>
    </el-form-item>
  </el-form>
</template>

<script>
import debounce from "lodash/debounce";
import basicAdd from "@/mixins/basic-add";
export default {
  mixins: [basicAdd],
  data() {
    return {
      templateList:[],
      urlOptions: {
        submitURL: "/supplier/qmsEvaluationTask",
        infoURL: "/supplier/qmsEvaluationTask/{id}",
        getSupplierListURL: '/supplier/qmsSupplier/page',
        getTemplateListURL: '/supplier/qmsEvaluationTask/page'
      },
      options: [{
        value: 0,
        label: '不可用'
      },
      {
        value: 1,
        label: '可用'
        }],
      supplierList:null,
      enterpriseNatureList: [
        {
          value: 0,
          label: '私营'
        },
        {
          value: 1,
          label: '国有'
        },
           {
          value: 2,
          label: '外资'
        },
           {
          value: 3,
          label: '其他'
        }
      ],
      evaluationTypeList: [
        {
          id: 0,
          name: '年度'
        },
        {
          id: 1,
          name: '1季度'
        },
        {
          id: 2,
            name: '月度'
        }
      ],
      supplierTypeList: [],
      productList:[],
      visible: false,
      listQuery: {
        limit: 999,
        page:1
      },
      dataForm: {
        id: "",
        title: null,
        supplierId: null,
        evaluationPeriod:null,
        evaluationType:null,
        evaluationTemplateId: null,
        startTime: null,
        description:null
      },
    };
  },
  computed: {
    dataRule() {
      return {
        // dictLabel: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
        // dictValue: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
        // sort: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
      };
    },
  },
  methods: {
    getProductCode(val) {
      this.productList.forEach(ele => {
        if (val === ele.id) {
          this.dataForm.productCode = ele.productCode
        }
      });
    },
    init(id, ) {
      this.dataForm.id = id || ""
      // this.dataForm.dictTypeId = dictTypeId || "";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.getCode()
        }
      });
    },
    getCode() {
      this.$http
        .get(this.urlOptions.getSupplierListURL, { params: this.listQuery })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.supplierList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .get(this.urlOptions.getTemplateListURL, { params: this.listQuery })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.templateList = res.data.list
          }
        })
        .catch(() => {
        })
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/supplier/qmsEvaluationTask/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](this.urlOptions.submitURL, this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false
                  this.$emit("successSubmit")
                },
              })
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
