<script>
import { TableColumns, SearchColumns } from "./column";
import { list, remove } from "@/api/business/purchase/catalogue";
import { initPage, initLayout, initPageSizes, initParams } from "@/utils/init";
export default {
  name: "PuchaseContract",
  components: {
    AddDrawer: () => import("./add/index.vue"),
    SeeDrawer: () => import("./see/index.vue"),
    EditDrawer: () => import("./edit/index.vue"),
  },
  data() {
    return {
      size: "mini",
      loading: false,
      searchColumns: SearchColumns,
      params: initParams(SearchColumns),
      tableData: [],
      tableColumns: TableColumns,
      page: initPage(),
      layout: initLayout(),
      pageSizes: initPageSizes(),
    };
  },
  computed: {},
  created() {
    this.handleQueryList();
  },
  methods: {
    //
    async fetchList(params, page) {
      try {
        this.loading = true;
        const { pageNum, pageSize } = page;
        const { code, msg, rows, total } = await list({
          pageNum,
          pageSize,
          ...params,
        });
        if (code === 200) {
          this.tableData = rows;
          this.page.total = total;
          this.$notify.success({ title: msg });
        } else {
          this.$notify.warning({ title: msg });
        }
      } catch (err) {
        //
      } finally {
        this.loading = false;
      }
    },
    // 查询操作
    handleQueryList() {
      this.fetchList(this.params, this.page);
    },
    // 重置操作
    handleResetList() {
      this.page = initPage();
      this.params = initParams(SearchColumns);
      this.fetchList(this.params, this.page);
    },
    // 删除操作
    async handleDeleteList(prop) {
      try {
        this.loading = true;
        const { id } = prop;
        const { code, msg } = await remove(id);
        if (code === 200) {
          this.$notify.success({ title: msg });
          this.fetchList(this.params, this.page);
        } else {
          this.$notify.warning({ title: msg });
        }
      } catch (err) {
        //
      } finally {
        this.loading = false;
      }
    },
    // 页大小变
    handleSizeChange(prop) {
      this.page.pageSize = prop;
      this.fetchList(this.params, this.page);
    },
    // 当前页变
    handleCurrentChange(prop) {
      this.page.pageNum = prop;
      this.fetchList(this.params, this.page);
    },
    // 打开新增drawer
    handleOpenAddDrawer() {
      console.log(this.$refs.AddDrawerFef);
      const { setVisible } = this.$refs.AddDrawerFef;
      setVisible(true);
    },
    // 打开查看drawer
    async handleOpenSeeDrawer(prop) {
      const { id } = prop;
      const { setVisible, fetchItem } = this.$refs.SeeDrawerFef;
      await setVisible(true);
      await fetchItem(id);
    },
    // 打开编辑drawer
    async handleOpenEditDrawer(prop) {
      const { id } = prop;
      const { setVisible, fetchItem } = this.$refs.EditDrawerFef;
      await setVisible(true);
      await fetchItem(id);
    },
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    style="width: calc(100% - 24px); height: 100%; margin: 10px"
    :body-style="{ padding: 0 }"
  >
    <see-drawer ref="SeeDrawerFef"></see-drawer>
    <add-drawer ref="AddDrawerFef" @close="handleResetList"></add-drawer>
    <edit-drawer ref="EditDrawerFef" @close="handleResetList"></edit-drawer>
    <el-form :size="size" label-position="top" :model="params">
      <el-row :gutter="24" style="padding: 0 20px">
        <el-col
          v-for="column in searchColumns"
          :key="column.title"
          :xl="4"
          :lg="4"
          :md="8"
          :sm="12"
          :xs="24"
        >
          <el-form-item :prop="column.key" :label="column.title">
            <el-input
              v-model="params[column.key]"
              :placeholder="column.placeholder"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xl="6" :lg="6" :md="8" :sm="12" :xs="24"> </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="24" style="padding: 0 20px">
      <el-col :span="24">
        <el-button :size="size" icon="el-icon-search" @click="handleQueryList"
          >搜索</el-button
        >
        <el-button :size="size" icon="el-icon-refresh" @click="handleResetList"
          >重置</el-button
        >
      </el-col>
    </el-row>
    <el-table
      @row-dblclick="handleOpenSeeDrawer"
      :data="tableData"
      :size="size"
      style="width: 100%; margin: 20px 0 0 0"
    >
      <el-table-column
        v-for="(column, index) in tableColumns"
        :key="index"
        :prop="column.key"
        :label="column.title"
        :width="column.width || 180"
        :show-overflow-tooltip="column.showOverflowTooltip || true"
      >
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="handleDeleteList(scope.row)"
            type="text"
            size="small"
          >
            删 除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :total="page.total"
      :page-sizes="pageSizes"
      :page-size="page.pageSize"
      :current-page="page.pageNum"
      :layout="layout"
      style="margin: 16px 0"
    >
    </el-pagination>
  </el-card>
</template>
