<script>
import { TableColumns, SearchColumns } from "./column";
import { LIST } from "@/api/business/purchase/catalogue";
import { initPage, initParams } from "@/utils/init";
export default {
  name: "PuchaseCatalogue",
  components: {
    EditModel: () => import("./edit/index.vue"),
  },
  data() {
    return {
      size: "mini",
      loading: false,
      searchColumns: SearchColumns,
      params: initParams(SearchColumns),
      tableData: [],
      tableColumns: TableColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {},
  created() {
    this.useQuery(this.params, this.page);
  },
  methods: {
    //
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { pageNum, pageSize } = page;
        const { code, rows, total } = await LIST({
          pageNum,
          pageSize,
          ...prop,
        });
        if (code === 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (err) {
        // catch
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    useQuery(prop, page) {
      this.fetchList(prop, page);
    },
    // 重 置
    useReset() {
      this.page = initPage();
      this.params = initParams(SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 编 辑
    async useEdit(prop) {
      return;
      const { open } = this.$refs.EditModel;
      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-model ref="EditModel" @success="resetList"></edit-model> -->
    <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%"
                  @keyup.enter.native="useQuery(params, page)"
                ></el-input>
                <el-switch
                  v-if="column.inputType === 'Switch'"
                  v-model="params[column.key]"
                  active-value="1"
                  inactive-value="0"
                  @keyup.enter.native="useQuery(params, page)"
                >
                </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"
                  @keyup.enter.native="useQuery(params, page)"
                >
                </dr-popover-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
    <el-row style="padding: 0 20px">
      <el-button :size="size" @click="useQuery(params, page)">
        查 询
      </el-button>
      <el-button :size="size" @click="useReset"> 重 置 </el-button>
    </el-row>
    <el-table
      @row-dblclick="useEdit"
      :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>
    <pagination
      :total="page.total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="useQuery(params, page)"
    />
  </el-card>
</template>
