<script>
import { dicts } from "./dicts";
import useColumns from "./columns";
import { LIST } from "@/api/business/purchase/catalogue";

export default {
  name: "PuchaseCatalogue",
  dicts: dicts,
  components: {
    SeeButton: () => import("./see/index.vue"),
    InvButton: () => import("./invalid/index.vue"),
    EnaButton: () => import("./enable/index.vue"),
    ExpButton: () => 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: 20, total: 0 },
    };
  },
  computed: {},
  created() {
    this.useQuery(this.params, this.page);
  },
  methods: {
    //
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { pageNum, pageSize } = page;
        const { code, rows, total } = await LIST(
          { ...prop },
          { pageNum, pageSize }
        );
        if (code === 200) {
          this.tableData = rows.map((item, index) => ({
            ...item,
            $index: (pageNum - 1) * pageSize + index + 1,
          }));
          this.page.total = total;
        }
      } catch (err) {
        // catch
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    useQuery(prop, page) {
      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 { open } = this.$refs.SeeButton;
      await open([prop]);
    },
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    :body-style="{
      height: '100%',
      padding: 0,
      display: 'flex',
      'flex-direction': 'column',
    }"
  >
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="useReset"
      @submit="useQuery(params, page)"
    ></el-super-search>
    <div class="my-4" style="text-align: right">
      <el-button-group>
        <ena-button
          :size="size"
          :select-data="selectData"
          status="Y"
          @success="useQuery(params, page)"
        ></ena-button>
        <ena-button
          :size="size"
          :select-data="selectData"
          status="N"
          @success="useQuery(params, page)"
        ></ena-button>
      </el-button-group>
      <el-button-group>
        <inv-button
          ref="InvButton"
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></inv-button>
        <see-button
          v-show="false"
          ref="SeeButton"
          :size="size"
          :dict="dict"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></see-button
      ></el-button-group>
      <el-button-group>
        <exp-button
          :size="size"
          :page="page"
          :data="params"
          @success="useQuery(params, page)"
        ></exp-button>
      </el-button-group>
    </div>
    <el-super-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      index
      checkbox
      pagination
      convenitentOperation
      @row-dblclick="useSee"
      @selection-change="useSelect"
      @pagination="useQuery(params, page)"
    >
    </el-super-table>
  </el-card>
</template>
<style scoped lang="scss">
.el-card {
  width: calc(100% - 32px);
  height: calc(100vh - 32px);
  margin: 16px;
  padding: 16px;
  border-radius: 8px;
}
.el-button-group + .el-button-group {
  margin: 0 0 0 10px;
}
</style>
