<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-07-14 15:57:26
 * @LastEditors: DY
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
    <el-form-item prop="productId" :label="$t('supplier.productName')">
      <el-select v-model="dataForm.productId" :placeholder="$t('supplier.productName')">
        <el-option v-for="item in productList" :key="item.id" :label="item.productName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="supplierId" :label="$t('supplier.supplierName')">
      <el-select v-model="dataForm.supplierId" :placeholder="$t('supplier.supplierName')">
        <el-option v-for="item in supplierList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </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/qmsSupplierProduct",
        infoURL: "/supplier/qmsSupplierProduct/{id}",
        getProductListURL: '/basic/qmsProduct/page',
        // getCodeURL: '/supplier/qmsSupplierType/getCode',
        getSupplierListURL: '/supplier/qmsSupplier/page'
      },
      options: [{
        value: 0,
        label: '不可用'
      },
      {
        value: 1,
        label: '可用'
      }],
      supplierList: [],
      productList:[],
      visible: false,
      listQuery: {
        limit: 999,
        page:1
      },
      dataForm: {
        id: "",
        supplierId:null,
        productId: null,
      },
    };
  },
  computed: {
    dataRule() {
      return {
        supplierId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        productId: [
          {
            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
        .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.getProductListURL, { params: this.listQuery })
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.productList = res.data.list
          }
        })
        .catch(() => {
        })
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/supplier/qmsSupplierProduct/${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>
