<script>
import { TableColumns, SearchColumns } from "./column";
import { LIST, REMOVE } from "@/api/business/purchase/apply";
import { initPage, initLayout, initPageSizes, initParams } from "@/utils/init";
export default {
  name: "PuchaseApply",
  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 deleteItem(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);
    },
    // 打开新增dialog
    async openAddDialog(prop) {
      const { open } = this.$refs.AddDialog;
      await open(prop);
    },
    // 打开查看dialog
    async openSeeDialog(prop) {
      const { id } = prop;
      const { open } = this.$refs.SeeDialog;
      await open(id);
    },
    // 打开编辑dialog
    async openEditDialog(prop) {
      const { id } = prop;
      const { setVisible, fetchItem } = this.$refs.EditdialogFef;
      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-dialog ref="SeeDialog"></see-dialog>
    <add-dialog ref="AddDialog" @submit-success="resetList"></add-dialog>
    <!-- <edit-dialog ref="EditdialogFef" @close="resetList"></edit-dialog> -->
    <el-form
      :size="size"
      :model="params"
      label-width="75px"
      label-position="right"
      style="padding: 20px 20px 0"
    >
      <el-row :gutter="20" style="display: flex; flex-wrap: wrap">
        <el-col
          v-for="column in searchColumns"
          :key="column.title"
          :span="column.span || 6"
        >
          <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 :span="8">
          <el-form-item label-width="0">
            <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-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      :size="size"
      :data="tableData"
      @row-dblclick="openSeeDialog"
      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="200">
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="openAddDialog(scope.row)"
            type="text"
            size="small"
          >
            复 制
          </el-button>
          <el-button
            @click.native.prevent="editItem(scope.row)"
            type="text"
            size="small"
          >
            编 辑
          </el-button>
          <el-button
            @click.native.prevent="deleteItem(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>
