<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-06-05 16:38:54
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
    <el-form-item prop="code" :label="$t('supplier.code')">
      <el-input v-model="dataForm.code" :placeholder="$t('supplier.code')">
      </el-input>
    </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="projectTypeId" :label="$t('supplier.projectTypeName')">
      <el-select v-model="dataForm.projectTypeId" :placeholder="$t('supplier.projectTypeName')">
        <el-option v-for="item in projectTypeList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="requirement" :label="$t('supplier.requirement')">
      <el-input v-model="dataForm.requirement" :placeholder="$t('supplier.requirement')">
      </el-input>
    </el-form-item>
    <el-form-item prop="statisticalValue" :label="$t('supplier.statisticalValue')">
      <K v-model="dataForm.closedLoop" :active-value="1" :inactive-value="0">
      </K>
    </el-form-item>
  </el-form>
</template>

<script>
import debounce from "lodash/debounce";
import basicAdd from "@/mixins/basic-add";
export default {
  mixins: [basicAdd],
  data() {
    return {
      urlOptions: {
        submitURL: "/supplier/qmsEvaluationItemList",
        infoURL: "/supplier/qmsEvaluationItemList/{id}",
        getProjectTypeURL: '/supplier/qmsProjectType/page',
        getCodeURL: '/supplier/qmsEvaluationItemList/getCode'
      },
      options: [{
        value: 0,
        label: '不可用'
      },
      {
        value: 1,
        label: '可用'
        }],
      supplierList:null,
      enterpriseNatureList: [
        {
          value: 0,
          label: '私营'
        },
        {
          value: 1,
          label: '国有'
        },
           {
          value: 2,
          label: '外资'
        },
           {
          value: 3,
          label: '其他'
        }
      ],
      projectTypeList:[],
      visible: false,
      listQuery: {
        limit: 999,
        page:1
      },
      dataForm: {
        id: "",
        title:null,
        projectTypeId: undefined,
        requirement: null,
        statisticalValue: 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: {
    init(data,) {
      console.log(data)
      console.log(this.dataForm.projectTypeId)
      // this.dataForm.dictTypeId = dictTypeId || "";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields()
        if (data) {
          this.dataForm.id = data.id ? data.id : ''
          this.dataForm.projectTypeId = data.projectTypeId ? data.projectTypeId : ''
        }
        if (this.dataForm.id) {
          this.getInfo()
        } else {
          this.getCode()
        }
      });
    },
    getCode() {
      this.$http
        .get(this.urlOptions.getProjectTypeURL, { params: this.listQuery })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.projectTypeList = res.data.list
          }
        })
        .catch(() => {
        })
      this.$http
        .post(this.urlOptions.getCodeURL)
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.dataForm.code = res.data
          }
        })
        .catch(() => {
        })
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/supplier/qmsEvaluationItemList/${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: () => {
                  console.log(1111);
                  this.visible = false;
                  this.$emit("successSubmit");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
