<!--
 * @Author: zwq
 * @Date: 2023-01-04 10:29:40
 * @LastEditors: zhp
 * @LastEditTime: 2023-02-14 14:12:11
 * @Description: 
-->
<template>
  <div>
    <base-table
      :table-props="tableProps"
      :page="listQuery.page"
      :limit="listQuery.limit"
      :table-data="tableData"
    >
      <method-btn
        v-if="tableBtn.length"
        slot="handleBtn"
        :width="100"
        label="操作"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <pagination
      :limit.sync="listQuery.limit"
      :page.sync="listQuery.page"
      :total="listQuery.total"
      @pagination="getDataList"
    />
  </div>
</template>

<script>
import debounce from "lodash/debounce";
// import basicAdd from "@/mixins/basic-add";
import basicPage from "@/mixins/basic-page";
const tableProps = [
  {
    prop: "productCode",
    label: "编号",
  },
  {
    prop: "productName",
    label: "名称",
  },
];
const tableBtn = [
  {
    type: "synchronous",
    btnName: "同步",
    showParam: {
      type: "&",
      data: [
        {
          type: "equal",
          name: "controlCodeId",
          value:null,
        },
      ],
    },
  },
  {
    type: "cancel",
    btnName: "取消关联",
    showParam: {
      type: "&",
      data: [
        {
          type: "unequal",
          name: "controlCodeId",
          value: null,
        },
      ],
    },
  },
];
export default {
  // mixins: [basicAdd],
  mixins: [basicPage],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/basic/qmsProduct/page",
        // submitURL: "/sys/params/",
        // infoURL: "/sys/params",
      },
      tableProps,
      tableBtn,
      productData:{},
      visible: false,
      dataForm: {
        id: "",
        paramCode: "",
        paramValue: "",
        remark: "",
      },
    };
  },
  computed: {
    dataRule() {
      return {
        // paramCode: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
        // paramValue: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   },
        // ],
      };
    },
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    init(data) {
      console.log();
      // this.dataForm.id = id || "";
      this.productData = data? data: {}
      this.visible = true;
      this.$nextTick(() => {
        this.listQuery.controllerModeId = data ? data.id : null;
        // this.$refs["dataForm"].resetFields();
        // if (this.dataForm.id) {
        this.getDataList();
        // }
      });
    },
    handleClick() {
      if (val.type === "synchronous") {
        if (!this.listQuery.controllerModeId) {
          this.$message({
            message: '请先选择控制模式',
            type: 'warning'
          });
        } else {
          const data={
            controlCodeId: this.listQuery.controlCodeId,
            finishInspection: this.productData.finishInspection,
            incomingInspection: this.productData.finishInspection,
            processInspection:	this.productData.processInspection
          }
          this.$http['put']('/basic/qmsProduct',data).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('refreshDataList')
              }
            })
          }).catch(() => { })
        }
        // this.addOrUpdateVisible = true;
        // this.addOrEditTitle = "编辑";
        // this.$nextTick(() => {
        //   this.$refs.addOrUpdate.init(val.data.id);
        // });
      } else if (val.type === 'cancel') { 
        const data = {
          controlCodeId: null,
          finishInspection: null,
          incomingInspection: null,
          processInspection: null
        }
        this.$http['put']('/basic/qmsProduct', data).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('refreshDataList')
            }
          })
        }).catch(() => { })
      } 
    },
    // 获取信息
    // getInfo() {
    //   this.$http
    //     .get(`/sys/params/${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"](
        //     "/sys/params",
        //     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("refreshDataList");
        // },
        //       });
        //     })
        //     .catch(() => {});
        // });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
