<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__user">
      <search-bar :formConfigs="formConfig" ref="searchBarForm" @headBtnClick="buttonClick" />
      <base-table
        :table-props="tableProps"
        :page="listQuery.page"
        :limit="listQuery.limit"
        :table-data="tableData"
      >
        <method-btn
          v-if="tableBtn.length"
          slot="handleBtn"
          :width="150"
          :label="$t('handle')"
          :method-list="tableBtn"
          @clickBtn="handleClick"
        />
      </base-table>
      <pagination
        :limit.sync="listQuery.limit"
        :page.sync="listQuery.page"
        :total="listQuery.total"
        @pagination="getDataList"
      />
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @successSubmit="getDataList"
      ></add-or-update>
    </div>
  </el-card>
</template>

<script>
import AddOrUpdate from "./components/productList-add";
import inspectionStage from "./inspectionStage";
import basicPage from "@/mixins/basic-page";
import basicFilter from "@/filters/basic-filter";
import i18n from "@/i18n";

const tableProps = [
  {
    prop: "name",
    label: i18n.t("role.name"),
  },
  {
    prop: "code",
    label: i18n.t("params.paramCode"),
  },
  {
    prop: "specifications",
    label: i18n.t("module.basicData.specs"),
  },
  {
    prop: "drawing",
    label: i18n.t("module.basicData.drawing"),
  },
  {
    prop: "productType",
    label: i18n.t("module.basicData.productType"),
  },
  {
    prop: "remark",
    label: i18n.t("schedule.remark"),
  },
  {
    prop: "descs",
    label: i18n.t("module.basicData.descs"),
  },
  {
    prop: "inspectionStandard",
    label: i18n.t("module.basicData.inspectionStandard"),
  },
  {
    prop: "inspectionStage",
    label: i18n.t("module.basicData.inspectionStage"),
    subcomponent: inspectionStage,
  },
  {
    prop: "status",
    label: i18n.t("module.basicData.status"),
    filter: basicFilter("status"),
  },
];
const tableBtn = [
  {
    type: "edit",
    btnName: i18n.t("edit"),
  },
  {
    type: "delete",
    btnName: i18n.t("delete"),
  },
  {
    type: "change",
    btnName: "改变状态",
  },
];

export default {
  mixins: [basicPage],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/basic/product/page",
        deleteURL: "/basic/product/delete",
        statusUrl: "/basic/product/status",
        exportUrl: "/basic/product/export",
      },
      tableProps,
      tableBtn,
      tableData: [],
      formConfig: [
        {
          type: "input",
          label: i18n.t("role.name"),
          placeholder: i18n.t("placeholder.name"),
          param: "name",
        },
        {
          type: "input",
          label: i18n.t("params.paramCode"),
          placeholder: i18n.t("placeholder.code"),
          param: "code",
        },
        {
          type: "select",
          label: i18n.t("module.basicData.productType"),
          selectOptions: [],
          param: "product_type_id",
          defaultSelect: "",
        },
        {
          type: "select",
          label: i18n.t("module.basicData.inspectionStage"),
          selectOptions: [
            { id: 1, name: "进货检验" },
            { id: 2, name: "过程检验" },
            { id: 3, name: "成品检验" },
            { id: 4, name: "出货检验" },
          ],
          param: "inspection_stage",
          defaultSelect: "",
        },
        {
          type: "button",
          btnName: i18n.t("query"),
          name: "search",
          color: "primary",
        },
        {
          type: "button",
          btnName: i18n.t("add"),
          name: "add",
          color: "primary",
          plain: true,
        },
        {
          type: "button",
          btnName: i18n.t("export"),
          name: "export",
          color: "primary",
          plain: true,
        },
      ],
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {
    this.getArr();
  },
  methods: {
    //search-bar点击
    buttonClick(val) {
      switch (val.btnName) {
        case "search":
          this.listQuery.name = val.name;
          this.listQuery.code = val.code;
          this.listQuery.product_type_id = val.product_type_id;
          this.listQuery.inspection_stage = val.inspection_stage;
          this.listQuery.page = 1;
          this.getDataList();
          break;
        case "add":
          this.addOrEditTitle = this.$t("add");
          this.addOrUpdateVisible = true;
          this.addOrUpdateHandle();
          break;
        case "export":
          this.exportHandle("产品列表");
          break;
        default:
          console.log(val);
      }
    },
    getArr() {
      this.$http
        .get("/basic/productType/page", {
          params: {
            page: 1,
            limit: 500,
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            this.formConfig[2].selectOptions = [];
            return this.$message.error(res.msg);
          }
          this.formConfig[2].selectOptions = res.data.list;
        })
        .catch(() => {});
    },
  },
};
</script>
