<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-07-12 15:54:54
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" :rules="dataRule" @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-form-item>
    <el-form-item prop="name" :label="$t('supplier.name')">
      <el-input v-model="dataForm.name" :placeholder="$t('supplier.name')">
      </el-input>
    </el-form-item>
    <el-form-item prop="abbreviation" :label="$t('supplier.abbreviation')">
      <el-input v-model="dataForm.abbreviation" :placeholder="$t('supplier.abbreviation')">
      </el-input>
    </el-form-item>
    <el-form-item prop="grade" :label="$t('supplier.grade')">
      <el-input v-model="dataForm.grade" :placeholder="$t('supplier.grade')">
      </el-input>
    </el-form-item>
    <el-form-item prop="supplierTypeId" :label="$t('supplier.supplierType')">
      <el-select v-model="dataForm.supplierTypeId" :placeholder="$t('supplier.supplierType')">
        <el-option v-for="item in supplierTypeList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="address" :label="$t('supplier.address')">
      <el-input v-model="dataForm.address" :placeholder="$t('supplier.address')">
      </el-input>
    </el-form-item>
    <el-form-item prop="ment" :label="$t('supplier.ment')">
      <el-select v-model="dataForm.ment" :placeholder="$t('supplier.ment')">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="supplierStatus" :label="$t('supplier.supplierStatus')">
      <el-select v-model="dataForm.supplierStatus" :placeholder="$t('supplier.supplierStatus')">
        <el-option v-for="item in supplierStatus" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="contact" :label="$t('supplier.contact')">
      <el-input v-model="dataForm.contact" :placeholder="$t('supplier.contact')">
      </el-input>
    </el-form-item>
    <el-form-item prop="contactEmail" :label="$t('supplier.contactEmail')">
      <el-input v-model="dataForm.contactEmail" :placeholder="$t('supplier.contactEmail')">
      </el-input>
    </el-form-item>
    <el-form-item prop="contactPhone" :label="$t('supplier.contactPhone')">
      <el-input v-model="dataForm.contactPhone" :placeholder="$t('supplier.contactPhone')">
      </el-input>
    </el-form-item>
    <el-form-item prop="enterpriseNature" :label="$t('supplier.enterpriseNature')">
      <el-select v-model="dataForm.enterpriseNature" :placeholder="$t('supplier.enterpriseNature')">
        <el-option v-for="item in enterpriseNatureList" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="industry" :label="$t('supplier.industry')">
      <el-input v-model="dataForm.industry" :placeholder="$t('supplier.industry')">
      </el-input>
    </el-form-item>
    <el-form-item prop="productService" :label="$t('supplier.productService')">
      <el-input v-model="dataForm.productService" :placeholder="$t('supplier.productService')">
      </el-input>
    </el-form-item>

    <!-- <el-form-item prop="productTypeStatus" :label="$t('basic.status')">
      <el-select v-model="dataForm.productTypeStatus" :placeholder="$t('basic.status')">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </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/qmsSupplier",
        infoURL: "/supplier/qmsSupplier/{id}",
        // getProductListURL: '/basic/qmsProduct/page',
        getCodeURL: '/supplier/qmsSupplier/getCode',
        getSupplierTypeListURL: '/supplier/qmsSupplierType/page'
      },
      options: [{
        value: 0,
        label: '不可用'
      },
      {
        value: 1,
        label: '可用'
        }
      ],
      supplierStatus: [{
        value: 0,
        label: '合格'
      },
      {
        value: 1,
        label: '不合格'
      },
      {
        value: 2,
        label: '黑名单'
      }
      ],
      enterpriseNatureList: [
        {
          value: 0,
          label: '私营'
        },
        {
          value: 1,
          label: '国有'
        },
           {
          value: 2,
          label: '外资'
        },
           {
          value: 3,
          label: '其他'
        }
      ],
      
      supplierTypeList: [],
      productList:[],
      visible: false,
      listQuery: {
        limit: 999,
        page:1
      },
      dataForm: {
        id: "",
        code:null,
        supplierId:null,
        productId: null,
        ment: null,
        supplierTypeId:null,
        name:null,
        address: null,
        abbreviation: null,
        supplierStatus:null,
        grade: null,
        contact:null,
        contactEmail:null,
        contactPhone: null,
        enterpriseNature: null,
        industry: null,
        productService: null
      },
    };
  },
  computed: {
    dataRule() {
      return {
        code: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        ment: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        supplierStatus: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
      };
    },
  },
  methods: {
    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
        .post(this.urlOptions.getCodeURL )
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.dataForm.code = res.data
          }
        })
        .catch(() => {
        })
      this.$http
        .get(this.urlOptions.getSupplierTypeListURL, { params: this.listQuery })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.supplierTypeList = res.data.list
          }
        })
        .catch(() => {
        })
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/supplier/qmsSupplier/${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>
