<script>
import { LIST } from "@/api/business/purchase/task";
import { TableColumns, SearchColumns } from "./column";
import { initParams, initDicts } from "@/utils/init.js";

export default {
  name: "PuchaseTask",
  dicts: [...initDicts([...TableColumns, ...SearchColumns])],
  components: {
    // CloseModel: () => import("./close/index.vue"),
    DcButton: () => import("./dao-chu/index.vue"),
    MxButton: () => import("./ming-xi/index.vue"),
    ZpButton: () => import("./zhuan-pai/index.vue"),
    ThxqButton: () => import("./tui-hui-xu-qiu/index.vue"),
    XyzcButton: () => import("./xie-yi-zhi-cai/index.vue"),
    ElSuperTable: () => import("@/components/super-table/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
  },
  data() {
    return {
      size: "mini",
      loading: false,
      SearchColumns: SearchColumns,
      params: initParams(SearchColumns),
      tableData: [],
      selectData: [],
      TableColumns: TableColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {
    $dicts: {
      get: function () {
        return this.dict.type;
      },
    },
  },
  watch: {},
  created() {
    this.useQuery(this.params, this.page);
  },
  methods: {
    //
    setSelectable(row) {
      const { status } = row;
      if (status === "1") {
        // 已执行协议直采
        return false;
      } else {
        return true;
      }
    },
    //
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { code, rows, total } = await LIST(prop, page);
        if (code === 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (err) {
        // catch
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    async useQuery(prop, page) {
      const { pageNum, pageSize } = page;
      const { date, documentsCodes: dCode, materialCodes: mCode } = prop;
      const [startDate, endDate] = date || [];
      const documentsCodes = dCode
        ? dCode.replace(/\s*/g, "").replaceAll("，", ",")
        : undefined;
      const materialCodes = Array.isArray(mCode)
        ? mCode
        : mCode
        ? mCode.replace(/\s*/g, "").replaceAll("，", ",").split(",")
        : undefined;
      await this.fetchList(
        {
          ...prop,
          endDate,
          startDate,
          materialCodes,
          documentsCodes,
          date: undefined,
        },
        { pageNum, pageSize, isAsc: "desc", orderByColumn: "updateTime" }
      );
    },
    // 重 置
    useReset() {
      this.page.pageNum = 1;
      this.page.pageSize = 10;
      this.params = initParams(SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      this.selectData = prop;
    },
    // 明 细
    async useSee(prop) {
      const { open } = this.$refs.mingxi;
      await open(prop);
    },
    // 导 出
    async useExport(prop, page) {
      const { open } = this.$refs.daochu;
      await open(prop, page);
    },
  },
};
</script>

<template>
  <el-card v-loading="loading" :body-style="{ padding: 0 }">
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="useReset"
      @submit="useQuery(params, page)"
    ></el-super-search>
    <div style="padding: 0 20px; text-align: right">
      <el-button-group style="margin: 0 10px 0 0">
        <thxq-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></thxq-button>
        <xyzc-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></xyzc-button>
      </el-button-group>
      <el-button-group>
        <zp-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        >
        </zp-button>
        <mx-button
          v-show="false"
          :size="size"
          :select-data="selectData"
          ref="mingxi"
          @success="useQuery(params, page)"
        ></mx-button>
        <dc-button
          :size="size"
          :page="page"
          :data="params"
          @success="useQuery(params, page)"
        ></dc-button>
      </el-button-group>
    </div>
    <el-super-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      :columns="TableColumns"
      @row-dblclick="useSee"
      @selection-change="useSelect"
    >
      <el-table-column
        fixed
        width="55"
        align="center"
        type="selection"
        :selectable="setSelectable"
      >
      </el-table-column>
    </el-super-table>
    <pagination
      :total="page.total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="useQuery(params, page)"
    />
  </el-card>
</template>
<style scoped lang="scss">
.el-card {
  width: calc(100% - 20px);
  height: 100%;
  margin: 10px;
  padding: 0 0 20px 0;
}
</style>
