<script>
import { dicts } from "./dicts";
import useColumns from "./columns";
import { LIST } from "@/api/business/purchase/contract";
export default {
  name: "PuchaseContract",
  dicts: dicts,
  components: {
    AddButton: () => import("./add/index.vue"),
    SeeButton: () => import("./see/index.vue"),
    EditButton: () => import("./edit/index.vue"),
    DeleButton: () => import("./delete/index.vue"),
    SubmButton: () => import("./submit/index.vue"),
    TermButton: () => import("./termination/index.vue"),
    PigeButton: () => import("./pigeonhole/index.vue"),
    ExportButton: () => import("./export/index.vue"),
    ElSuperTable: () => import("@/components/super-table/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
  },
  data() {
    const { TableColumns, SearchColumns } = useColumns();
    const params = this.$init.params(SearchColumns);
    return {
      size: "mini",
      loading: false,
      params: params,
      tableData: [],
      selectData: [],
      TableColumns: TableColumns,
      SearchColumns: SearchColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {},
  created() {
    this.useQuery(this.params, this.page);
  },
  methods: {
    setSelectable(){
      return true
    },
    //
    async fetchList(prop, page) {
      try {
        // try
        this.loading = true;
        const { pageNum, pageSize } = page;
        const { code, rows, total } = await LIST({
          pageNum,
          pageSize,
          ...prop,
        });
        if (code === 200) {
          this.tableData = rows.map((item, index) => ({
            ...item,
            $index: (pageNum - 1) * pageSize + index + 1,
          }));
          this.page.total = total;
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    useQuery(prop, page) {
      this.selectData = [];
      this.fetchList(prop, page);
    },
    // 重 置
    useReset() {
      this.page.pageNum = 1;
      this.page.pageSize = 10;
      this.params = this.$init.params(this.SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      this.selectData = prop;
    },
    // 明 细
    async useSee(prop) {
      const { id } = prop;
      const { open } = this.$refs.SeeButton;
      await open(id);
    },
  },
};
</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="margin: 20px 0 ; text-align: right">
      <el-button-group>
        <add-button
          :size="size"
          :dict="dict"
          :add-type="'add'"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></add-button>
        <add-button
          :size="size"
          :dict="dict"
          :add-type="'record'"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></add-button>
      </el-button-group>
      <el-button-group>
        <edit-button
          :size="size"
          :dict="dict"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></edit-button>
        <dele-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></dele-button>
        <see-button
          v-show="false"
          :size="size"
          :dict="dict"
          :select-data="selectData"
          ref="SeeButton"
        ></see-button
      ></el-button-group>
      <el-button-group>
        <subm-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></subm-button>
        <term-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></term-button>
        <pige-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></pige-button>
      </el-button-group>
      <el-button-group>
        <export-button
          :size="size"
          :page="page"
          :data="params"
          @success="useQuery(params, page)"
        ></export-button>
      </el-button-group>
    </div>
    <el-super-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      max-height="600"
      :columns="TableColumns"
      :selectable="setSelectable"
      checkbox
      pagination
      @row-dblclick="useSee"
      @selection-change="useSelect"
      :page="page"
      @pagination="useQuery(params, page)"
    >
      <!-- <el-table-column fixed width="55" align="center" label="#" prop="$index">
      </el-table-column> -->
      <!-- <el-table-column fixed width="55" align="center" type="selection">
      </el-table-column> -->
    </el-super-table>
    <!-- <pagination
      :total="page.total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="useQuery(params, page)"
      style="height: 32px; margin: 20px 0 0 0; padding: 0 !important"
    /> -->
  </el-card>
</template>
<style scoped lang="scss">
.el-card {
  width: calc(100% - 20px);
  height: 100%;
  margin: 10px;
  padding: 20px;
}
.el-button-group + .el-button-group {
  margin: 0 0 0 10px;
}
</style>
