<!--
 * @Author: zhp
 * @Date: 2023-02-14 15:02:26
 * @LastEditTime: 2023-04-04 15:35:31
 * @LastEditors: zhp
 * @Description: 
-->
<template>
  <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
    <el-form-item prop="productCode" :label="$t('basic.code')">
      <el-input v-model="dataForm.productCode" :placeholder="$t('basic.code')"></el-input>
    </el-form-item>
    <el-form-item prop="productName" :label="$t('basic.name')">
      <el-input v-model="dataForm.productName" :placeholder="$t('basic.name')"></el-input>
    </el-form-item>
    <el-form-item prop="productSpecs" :label="$t('basic.specification')">
      <el-input v-model="dataForm.productSpecs" :placeholder="$t('basic.specification')"></el-input>
    </el-form-item>
    <el-form-item prop="productTypeId" :label="$t('basic.productTypeName')">
      <el-select v-model="dataForm.productTypeId" :placeholder="$t('basic.productTypeName')">
        <el-option v-for="item in typeList" :key="item.id" :label="item.productTypeName" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item prop="remark" :label="$t('basic.remark')">
      <el-input v-model="dataForm.remark" :placeholder="$t('basic.remark')"></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: "/basic/qmsProductType",
        infoURL: "/basic/qmsProductType/{id}",
        getCodeURL: '/basic/qmsProduct/getCode',
        getTypeListURL: '/basic/qmsProductType/page'
      },
      typeList:[],
      visible: false,
      listQuery: {
        limit: 999,
        page:1
      },
      dataForm: {
        id: "",
        remark:null,
        productCode:null,
        productName: null,
        productTypeId:null,
        productTypeStatus: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(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() {
      // console.log(111111);
      this.$http
        .post(this.urlOptions.getCodeURL)
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res);
            this.dataForm.productTypeCode = res.data
          }
        })
        .catch(() => {
        });
      this.$http
        .get(this.urlOptions.getTypeListURL,this.listQuery )
        .then(({ data: res }) => {
          if (res.code === 0) {
            console.log(res.data);
            this.typeList = res.data.list
          }
        })
        .catch(() => {
        });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/basic/qmsProductType/${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>
