<script>
import { Columns as TableColumns, SearchColumns } from "./column";
import { LIST, REMOVE } from "@/api/business/purchase/contract";
import { initPage, initLayout, initPageSizes, initParams } from "@/utils/init";
export default {
  name: "PuchaseContract",
  components: {
    AddDialog: () => import("./add/index.vue"),
    SeeDialog: () => import("./see/index.vue"),
    EditDialog: () => 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.queryList(this.params, this.page);
  },
  methods: {
    //
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { pageNum, pageSize } = page;
        const { code, msg, rows, total } = await LIST({
          pageNum,
          pageSize,
          ...prop,
        });
        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;
      }
    },
    // 查询操作
    queryList(prop, page) {
      this.fetchList(prop, page);
    },
    // 重置操作
    resetList() {
      this.page = initPage();
      this.params = initParams(SearchColumns);
      this.queryList(this.params, this.page);
    },
    // 删除操作
    async deleteList(prop) {
      try {
        this.loading = true;
        const { id } = prop;
        const { code, msg } = await REMOVE(id);
        if (code === 200) {
          this.$notify.success({ title: msg });
          this.queryList(this.params, this.page);
        } else {
          this.$notify.warning({ title: msg });
        }
      } catch (err) {
        //
      } finally {
        this.loading = false;
      }
    },
    // 页大小变
    sizeChange(prop) {
      this.page.pageSize = prop;
      this.queryList(this.params, this.page);
    },
    // 当前页变
    currentChange(prop) {
      this.page.pageNum = prop;
      this.queryList(this.params, this.page);
    },
    // 打开新增drawer
    async openAddDialog() {
      const { open } = this.$refs.AddDialog;
      await open();
    },
    // 打开查看drawer
    async openSeeDialog(prop) {
      const { id } = prop;
      const { open } = this.$refs.SeeDialog;
      await open(id);
    },
    // 打开编辑drawer
    async openEditDialog(prop) {
      const { id } = prop;
      const { open } = this.$refs.EditDialog;
      await open(id);
    },
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    style="width: calc(100% - 24px); height: 100%; margin: 10px"
    :body-style="{ padding: 0 }"
  >
    <see-dialog ref="SeeDialog"></see-dialog>
    <add-dialog ref="AddDialog" @submit-success="resetList"></add-dialog>
    <edit-dialog ref="EditDialog" @submit-success="resetList"></edit-dialog>
    <el-form
      :size="size"
      :model="params"
      label-width="75px"
      label-position="right"
    >
      <el-row :gutter="24" style="padding: 20px 20px">
        <el-col
          v-for="column in searchColumns"
          :key="column.title"
          :xl="4"
          :lg="6"
          :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="4" :lg="6" :md="8" :sm="12" :xs="24">
          <el-button
            circle
            :size="size"
            icon="el-icon-search"
            @click="queryList(params, page)"
          ></el-button>
          <el-button
            circle
            :size="size"
            icon="el-icon-refresh"
            @click="resetList"
          ></el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="24" style="padding: 0 20px">
      <el-col :span="6">
        <el-button :size="size" @click="openAddDialog"> 新 增 </el-button>
      </el-col>
    </el-row>
    <el-table
      @row-dblclick="openSeeDialog"
      :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="openEditDialog(scope.row)"
            type="text"
            size="small"
          >
            编 辑
          </el-button>
          <el-button
            @click.native.prevent="deleteList(scope.row)"
            type="text"
            size="small"
          >
            删 除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChange"
      @current-change="currentChange"
      :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>
