<script>
import { FormColumns, SearchColumns } from "./column";
import { LIST } from "@/api/business/purchase/contract";
import { initPage, initDicts, initParams } from "@/utils/init";
export default {
  name: "PuchaseContract",
  dicts: [...initDicts(SearchColumns), ...initDicts(FormColumns)],
  components: {
    AddModel: () => import("./add/index.vue"),
    SeeModel: () => import("./see/index.vue"),
    EditModel: () => import("./edit/index.vue"),
    ExportModel: () => import("./export/index.vue"),
    ImportModel: () => import("./import/index.vue"),
    RecordModel: () => import("./record/index.vue"),
    DeleteModel: () => import("./delete/index.vue"),
    TerminationModel: () => import("./termination/index.vue"),
  },
  data() {
    return {
      size: "mini",
      loading: false,
      searchColumns: SearchColumns,
      params: initParams(SearchColumns),
      tableData: [],
      currentData: [],
      tableColumns: FormColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {},
  created() {
    this.useQuery(this.params, this.page);
  },
  methods: {
    //
    async fetchList(prop, page) {
      try {
        // 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
        console.error(err);
      } 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);
    },
    // 选 择
    useSelect(prop) {
      this.currentData = [prop];
    },
    // 新 增
    async useAdd() {
      const { open } = this.$refs.AddModel;
      await open();
    },
    // 删 除
    async useDelete(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.DeleteModel;
      await open(id);
    },
    // 编 辑
    async useEdit(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.EditModel;
      await open(id);
    },
    // 明 细
    async useSee(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.SeeModel;
      await open(id);
    },
    // 终 止
    async useTermination(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.TerminationModel;
      await open(id);
    },
    // 导 出
    async useExport(prop) {
      const { pageNum, pageSize } = this.page;
      const { open } = this.$refs.ExportModel;
      await open({ ...prop, pageNum, pageSize });
    },
    // 导 入
    async useImport() {
      const { open } = this.$refs.ImportModel;
      await open();
    },
    // 期初补录
    async useRecord() {
      const { open } = this.$refs.RecordModel;
      await open();
    },
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    style="
      width: calc(100% - 20px);
      height: 100%;
      margin: 10px;
      padding: 0 20px 20px 0;
    "
    :body-style="{ padding: 0 }"
  >
    <see-model ref="SeeModel"></see-model>
    <add-model ref="AddModel" @success="useReset"></add-model>
    <record-model ref="RecordModel" @success="useReset"></record-model>
    <edit-model ref="EditModel" @success="useQuery(params, page)"></edit-model>
    <export-model ref="ExportModel"></export-model>
    <import-model ref="ImportModel"></import-model>
    <delete-model
      ref="DeleteModel"
      @success="useQuery(params, page)"
    ></delete-model>
    <termination-model
      ref="TerminationModel"
      @success="useQuery(params, page)"
    ></termination-model>
    <el-form
      :size="size"
      :model="params"
      label-width="auto"
      label-position="right"
      style="padding: 20px 20px 0"
      @submit.native.prevent
    >
      <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"
              @keyup.enter.native="useQuery(params, page)"
            ></el-input>
          </el-form-item>
        </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-button :size="size" @click="useAdd"> 新 增 </el-button>
      <el-button :size="size" @click="useRecord"> 期初补录 </el-button>
      <el-button
        :size="size"
        :disabled="!currentData.length"
        @click="useEdit(currentData)"
      >
        编 辑
      </el-button>
      <el-button
        :size="size"
        :disabled="!currentData.length"
        @click="useDelete(currentData)"
      >
        删 除
      </el-button>
      <el-button
        :size="size"
        :disabled="!currentData.length"
        @click="useTermination(currentData)"
      >
        终 止
      </el-button>
      <el-button :size="size" :disabled="!currentData.length">
        变 更
      </el-button>
      <el-button :size="size" :disabled="!currentData.length">
        归 档
      </el-button>
      <el-button
        :size="size"
        :disabled="!currentData.length"
        @click="useExport(params)"
      >
        导 出
      </el-button>
      <!-- <el-button :size="size" @click="useImport"> 导 入 </el-button> -->
    </el-row>
    <el-table
      :size="size"
      :data="tableData"
      highlight-current-row
      @row-click="useSelect"
      @row-dblclick="useSee"
      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
      >
        <template slot-scope="scope">
          <dict-tag
            v-if="column.inputType === 'Select'"
            :size="size"
            :value="scope.row[column.key]"
            :options="dict.type[column.referName]"
          />
          <dr-file-preview
            v-else-if="column.inputType === 'Upload'"
            v-model="scope.row[column.key]"
          ></dr-file-preview>
          <span v-else>{{ scope.row[column.key] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click.native.prevent="useEdit(scope.row)"
          >
            编 辑
          </el-button>
          <el-button
            type="text"
            size="small"
            @click.native.prevent="useEdit(scope.row)"
          >
            变 更
          </el-button>
          <el-button
            type="text"
            size="small"
            @click.native.prevent="useEdit(scope.row)"
          >
            归 档
          </el-button>
          <el-button
            type="text"
            size="small"
            @click.native.prevent="useTermination(scope.row)"
          >
            终 止
          </el-button>
          <el-button
            type="text"
            size="small"
            @click.native.prevent="useDelete(scope.row)"
          >
            删 除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      :total="page.total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="useQuery(params, page)"
    />
  </el-card>
</template>
