<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="70"
          :label="$t('handle')"
          :method-list="tableBtn"
          @clickBtn="handleClick"
        />
      </base-table>
      <pagination
        :limit.sync="listQuery.limit"
        :page.sync="listQuery.page"
        :total="listQuery.total"
        @pagination="getDataList"
      />
      <!-- 弹窗, 条件搜索-->
      <!-- <base-dialog
        :dialogTitle="searchsTitle"
        :dialogVisible="searchsVisible"
        @cancel="searchsCancel"
        @confirm="searchsConfirm"
        :before-close="searchsCancel"
      >
        <searchs ref="searchsRef" @refreshDataList="searchsSubmit"></searchs>
      </base-dialog> -->
      <!-- 弹窗, 新增 / 修改 -->
      <base-dialog
        :dialogTitle="addOrEditTitle"
        :dialogVisible="addOrUpdateVisible"
        @cancel="handleCancel"
        @confirm="handleConfirm"
        :before-close="handleCancel"
        width=80%
      >
        <add-or-update ref="addOrUpdate" @refreshDataList="successSubmit"></add-or-update>
      </base-dialog>
    </div>
  </el-card>
</template>

<script>
import AddOrUpdate from "./components/eightDisciplineList-detail";
// import Searchs from "./components/searchs1";
import basicPage from "@/mixins/basic-page";
import codeFilter from "@/filters/code-filter";
import { timeFormatter } from "@/filters/code-filter";
import i18n from "@/i18n";

const tableProps = [
  {
    prop: "title",
    label: i18n.t("code.title"),
    align: "center",
  },
  {
    prop: "productName",
    label: "产品",
    align: "center",
  },
  {
    prop: "defectName",
    label: "缺陷",
    align: "center",
  },
  {
    prop: "description",
    label: "关键字",
    align: "center",
  },
  // {
  //   prop: "associationNumber",
  //   label: "关联次数",
  //   align: "center",
  // },
];
const tableBtn = [
  {
    type: "detail",
    btnName: "详情",
  },
];

export default {
  mixins: [basicPage],
  data() {
    return {
      urlOptions: {
        getDataListURL: "/code/startEightDiscipline/best/practice",
      },
      tableProps,
      tableBtn,
      tableData: [],
      optionArrUrl: ["/basic/qmsProduct/page","/code/problemType/page"],
      formConfig: [
        {
          type: "input",
          label: i18n.t("code.title"),
          placeholder: i18n.t("code.title"),
          param: "title",
        },
        {
          type: "select",
          label: "产品",
          selectOptions: [],
          param: "productId",
          defaultSelect: "",
          labelField: "productName",
          filterable: true,
        },
        {
          type: "select",
          label: "缺陷",
          selectOptions: [],
          param: "defectId",
          defaultSelect: "",
          filterable: true,
        },
        {
          type: "input",
          label: "关键字",
          placeholder: "关键字",
          param: "description",
        },
        {
          type: "button",
          btnName: "搜索",
          name: "search",
          color: "primary",
        },
      ],
    };
  },
  components: {
    AddOrUpdate,
    // Searchs,
  },
  created() {
    this.getArr();
  },
  methods: {
    getArr() {
      this.optionArrUrl.forEach((item, index) => {
        this.$http
          .get(item, {
            params: {
              page: 1,
              limit: 500,
            },
          })
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.formConfig[index + 1].selectOptions = res.data.list;
          })
          .catch(() => {});
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http
        .post(this.urlOptions.getDataListURL, this.listQuery)
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.tableData = [];
            this.listQuery.total = 0;
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.listQuery.total = res.data.total;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
    //search-bar点击
    searchsClick() {
      this.searchsTitle = "条件搜索";
      this.searchsHandle();
    },
    buttonClick(val) {
      switch (val.btnName) {
        case "search":
          this.listQuery.page = 1;
          this.listQuery.limit = 10;
          this.listQuery.title = val.title;
          this.listQuery.description = val.description;
          this.listQuery.defectId = val.defectId;
          this.listQuery.productId = val.productId;
          this.getDataList();
          break;
        default:
          console.log(val);
      }
    },
    otherMethods(val) {
      this.addOrEditTitle = "详情";
      this.addOrUpdateVisible = true;
      this.addOrUpdateHandle(val.data.id);
    },
  },
};
</script>
