<script>
import { dicts } from "./dicts";
import useColumns from "./columns";
import { LIST } from "@/api/business/purchase/contract";
export default {
  name: "PuchaseContract",
  dicts: dicts,
  components: {
    ElAddForm: () => import("./add/index.vue"),
    ElEditForm: () => import("./edit/index.vue"),
    ElSeeForm: () => import("./see/index.vue"),
    ElDeleteButton: () => 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,
      drawerVisible: false,
      components: { is: "", title: "" },
      params: params,
      tableData: [],
      selectData: [],
      TableColumns: TableColumns,
      SearchColumns: SearchColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {
    SeeDisabled: {
      get() {
        const { selectData } = this;
        if (selectData.length !== 1) {
          return true;
        }
      },
      set() {},
    },
    EditDisabled: {
      get() {
        const {
          selectData,
          selectData: [{ status } = {}],
        } = this;
        if (selectData.length !== 1) {
          return true;
        }
        if (selectData.length === 1 && status === "1") {
          return true;
        }
      },
      set() {},
    },
  },
  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.fetchList(prop, page);
    },
    // 重 置
    useReset() {
      this.page.pageNum = 1;
      this.page.pageSize = 10;
      this.params = this.$init.params(this.SearchColumns);
      this.$refs.superTable.clearSelection();
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      this.selectData = prop;
    },
    // 明 细
    async useSee(prop) {
      const { id } = prop;
      this.drawerVisible = true;
      this.components.title = id ? `明细-${id}` : "明细";
      this.components.is = "ElSeeForm";
    },
    // 新 增
    useAdd() {
      this.drawerVisible = true;
      this.components.title = "新增";
      this.components.is = "ElAddForm";
    },
    // 期 初 补 录
    useReord() {
      this.drawerVisible = true;
      this.components.title = "期初补录";
      this.components.is = "ElAddForm";
    },
    // 编 辑
    useEdit() {
      this.drawerVisible = true;
      this.components.title = "编 辑";
      this.components.is = "ElEditForm";
    },
    // 删 除
    useDelete() {
      this.useQuery(this.params, this.page);
      this.$refs.superTable.clearSelection();
    },
  },
};
</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-drawer
      size="100%"
      title="title"
      :show-close="false"
      :with-header="false"
      :visible.sync="drawerVisible"
      append-to-body
      destroy-on-close
    >
      <components
        :size="size"
        :dict="dict"
        :is="components.is"
        :title="components.title"
        :select-data="selectData"
        @close="drawerVisible = false"
        @submit-success="useQuery(params, page)"
      ></components>
    </el-drawer>
    <div style="margin: 18px 0; text-align: right">
      <el-button-group>
        <el-button :size="size" :disabled="SeeDisabled" @click="useSee">
          明细
        </el-button>
      </el-button-group>
      <el-button-group>
        <el-button :size="size" @click="useAdd">新增</el-button>
        <el-button :size="size" @click="useReord">期初补录</el-button>
      </el-button-group>
      <el-button-group>
        <el-button :size="size" :disabled="EditDisabled" @click="useEdit">
          编辑
        </el-button>
        <el-delete-button
          :size="size"
          :select-data="selectData"
          @submit-success="useDelete"
        ></el-delete-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"
          :isCancal="false"
          @success="useReset"
        ></pige-button>
        <pige-button
          :size="size"
          :select-data="selectData"
          :isCancal="true"
          @success="useReset"
        ></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"
      ref="superTable"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      :selectable="setSelectable"
      index
      checkbox
      pagination
      convenitentOperation
      storage-key="PuchaseContractSuperTable1"
      @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 8px;
}
</style>
