<script>
import { TableColumns, SearchColumns } from "./column";
import { LIST } from "@/api/business/purchase/catalogue";
import { initPage, initLayout, initPageSizes, initParams } from "@/utils/init";
export default {
  name: "PuchaseCatalogue",
  components: {
    // AddDrawer: () => import("./add/index.vue"),
    // SeeDrawer: () => 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() {
      this.fetchList(this.params, this.page);
    },
    // 重置操作
    resetList() {
      this.page = initPage();
      this.params = initParams(SearchColumns);
      this.fetchList(this.params, this.page);
    },
    // 页大小变
    sizeChange(prop) {
      this.page.pageSize = prop;
      this.fetchList(this.params, this.page);
    },
    // 当前页变
    currentChange(prop) {
      this.page.pageNum = prop;
      this.fetchList(this.params, this.page);
    },
    // 打开编辑dialog
    async openEditDialog(prop) {
      return;
      const { open } = this.$refs.Editdialog;
      await open(prop);
    },
  },
};
</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="resetList"></add-drawer> -->
    <edit-dialog ref="Editdialog" @submit-success="resetList"></edit-dialog>
    <el-form
      :size="size"
      :model="params"
      label-width="auto"
      label-position="right"
      style="padding: 20px 0 0"
    >
      <el-row style="display: flex; flex-wrap: wrap">
        <el-col :span="22">
          <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-if="column.inputType === 'Input'"
                  v-model="params[column.key]"
                  :placeholder="column.placeholder"
                  style="width: 100%"
                ></el-input>
                <el-switch
                  v-if="column.inputType === 'Switch'"
                  v-model="params[column.key]"
                  active-value="1"
                  inactive-value="0"
                >
                </el-switch>
                <dr-popover-select
                  v-if="column.inputType === 'PopoverSelect'"
                  v-model="params[column.key]"
                  :size="size"
                  :source.sync="params"
                  :title="column.title"
                  :type="column.referName"
                  :multiple="column.multiple"
                  :readonly="column.readonly"
                  :value-key="column.valueKey"
                  :placeholder="column.placeholder"
                  :data-mapping="column.dataMapping"
                >
                </dr-popover-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="1" :offset="1">
          <el-row style="display: flex; flex-wrap: wrap">
            <el-col :span="24" style="margin-bottom: 20px">
              <el-button
                circle
                :size="size"
                icon="el-icon-search"
                @click="queryList(params, page)"
              ></el-button>
            </el-col>
            <el-col :span="24">
              <el-button
                circle
                :size="size"
                icon="el-icon-refresh"
                @click="resetList"
              ></el-button>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      @row-dblclick="openEditDialog"
      :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>
    <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>
