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

export default {
  name: "PuchaseApply",
  dicts: dicts,
  components: {
    SeeButton: () => import("./see/index.vue"),
    AddButton: () => import("./add/index.vue"),
    CopyButton: () => import("./copy/index.vue"),
    EditButton: () => import("./edit/index.vue"),
    DeleButton: () => import("./delete/index.vue"),
    SubmButton: () => import("./submit/index.vue"),
    ElSuperTable: () => import("@/components/super-table/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
  },
  data() {
    const { TableColumns, SearchColumns } = useColumns();
    const page = this.$init.page();
    const params = this.$init.params(SearchColumns);
    return {
      size: "mini",
      loading: false,
      params: params,
      page: page,
      tableData: [],
      selectData: [],
      SearchColumns: SearchColumns,
      TableColumns: TableColumns,
    };
  },
  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
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    useQuery(prop, page) {
      this.selectData = [];
      this.fetchList(prop, page);
    },
    // 重 置
    useReset() {
      this.page = this.$init.page();
      this.params = this.$init.params(this.SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      console.log("prop", 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>
    <el-row class="my-4" style="text-align: right">
      <el-button-group>
        <add-button :size="size" :dict="dict" @success="useQuery(params, page)">
        </add-button>
        <copy-button
          :size="size"
          :dict="dict"
          :select-data="selectData"
          @success="useQuery(params, page)"
        >
        </copy-button>
      </el-button-group>
      <el-button-group>
        <see-button
          v-show="false"
          :size="size"
          :dict="dict"
          :model="params"
          :select-data="selectData"
          ref="SeeButton"
          @success="useQuery(params, page)"
        ></see-button>
        <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>
      </el-button-group>
      <el-button-group>
        <subm-button
          :size="size"
          :select-data="selectData"
          @success="useQuery(params, page)"
        ></subm-button>
      </el-button-group>
    </el-row>
    <el-super-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      index
      checkbox
      pagination
      convenitentOperation
      @row-dblclick="useSee"
      @row-select="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 8px;
}
</style>
