<script>
import { TableColumns, FormColumns } from "./column";
import { LIST } from "@/api/business/purchase/contract";
import { initDicts, initParams } from "@/utils/init.js";
export default {
  name: "PuchaseContract",
  dicts: [...initDicts(FormColumns), ...initDicts(TableColumns)],
  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"),
    DeleteModel: () => import("./delete/index.vue"),
    TerminationModel: () => import("./termination/index.vue"),
    PigeonholeModel: () => import("./pigeonhole/index.vue"),
    SubmitModel: () => import("./submit/index.vue"),
    ElDictTag: () => import("@/components/DictTag/index.vue"),
    ElFilePreview: () => import("@/components/file-preview/index.vue"),
    ElComputedInputV2: () => import("@/components/computed-input-v2/index.vue"),
  },
  data() {
    return {
      size: "mini",
      loading: false,
      batching: false,
      FormColumns: FormColumns,
      params: initParams(FormColumns),
      tableData: [],
      selectData: [],
      TableColumns: TableColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {
    $dicts: {
      get: function () {
        return this.dict.type;
      },
    },
    $power: {
      get() {
        const {
          selectData,
          selectData: [{ status, pigeonhole } = {}],
        } = this;
        const rule1 = selectData.length === 1;
        const rule2 = selectData.length > 1;
        const rule3 = status === "0" || status === "3";
        const rule4 = status === "2";
        const rule5 = pigeonhole === "Y";
        return {
          // 修 改
          edit: rule1 && !rule4,
          // 删 除
          delt: rule1 && rule3,
          // 终 止
          term: rule1 && rule4,
          // 归 档
          pige: rule1 && !rule5,
          // 提 交
          subm: rule1,
        };
      },
    },
  },
  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.map((item, index) => ({
            ...item,
            $index: (pageNum - 1) * pageSize + index + 1,
          }));
          this.page.total = total;
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    useQuery(prop, page) {
      this.selectData = [];
      this.fetchList(prop, page);
    },
    // 重 置
    useReset() {
      this.page.pageNum = 1;
      this.page.pageSize = 10;
      this.params = initParams(FormColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useRowClick(prop) {
      if (!this.batching) this.selectData = prop;
    },
    // 选 择
    useSelect(prop) {
      if (this.batching) this.selectData = prop;
    },
    // 批 量
    useBatch() {
      this.selectData = [];
      this.batching = !this.batching;
    },
    // 新 增
    async useAdd(prop) {
      const { open } = this.$refs.AddModel;
      await open(prop);
    },
    // 删 除
    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 usePigeonhole(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.PigeonholeModel;
      await open(id);
    },
    // 导 出
    async useExport(prop, page) {
      const { pageNum, pageSize } = page;
      const { open } = this.$refs.ExportModel;
      await open({ ...prop, pageNum, pageSize });
    },
    // 导 入
    // async useImport() {
    //   const { open } = this.$refs.ImportModel;
    //   await open();
    // },
    // 提交OA
    async useSubmit(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.SubmitModel;
      await open(id);
    },
  },
};
</script>

<template>
  <el-card v-loading="loading" :body-style="{ padding: 0 }">
    <see-model ref="SeeModel"></see-model>
    <add-model ref="AddModel" @success="useReset"></add-model>
    <edit-model ref="EditModel" @success="useQuery(params, page)"></edit-model>
    <export-model ref="ExportModel"></export-model>
    <!-- <import-model ref="ImportModel"></import-model> -->
    <submit-model
      ref="SubmitModel"
      @success="useQuery(params, page)"
    ></submit-model>
    <delete-model
      ref="DeleteModel"
      @success="useQuery(params, page)"
    ></delete-model>
    <termination-model
      ref="TerminationModel"
      @success="useQuery(params, page)"
    ></termination-model>
    <pigeonhole-model
      ref="PigeonholeModel"
      @success="useQuery(params, page)"
    ></pigeonhole-model>

    <el-form
      :size="size"
      :model="params"
      label-width="auto"
      label-position="right"
      @submit.native.prevent
    >
      <el-row :gutter="20" style="display: flex; flex-wrap: wrap">
        <el-col
          v-for="{ item, attr } in FormColumns"
          :key="item.key"
          :span="item.span || 6"
        >
          <el-form-item :prop="item.key" :label="item.title">
            <component
              v-if="attr.is === 'el-input'"
              v-bind="attr"
              v-model="params[item.key]"
              @keyup.enter.native="useQuery(params, page)"
            ></component>
            <component
              v-if="attr.is === 'el-select'"
              v-bind="attr"
              v-model="params[item.key]"
              style="width: 100%"
              @change="useQuery(params, page)"
              @keyup.enter.native="useQuery(params, page)"
            >
              <el-option
                v-for="option in $dicts[attr.dictName]"
                :key="option.value"
                :label="option.label"
                :value="option.value"
              >
              </el-option>
            </component>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="padding: 0 20px; display: flex; justify-content: space-between">
      <div>
        <el-button :size="size" @click="useQuery(params, page)">
          查 询
        </el-button>
        <el-button :size="size" @click="useReset"> 重 置 </el-button>
        <el-button :size="size" @click="useAdd('add')"> 新 增 </el-button>
        <el-button :size="size" @click="useAdd('record')"> 期初补录 </el-button>
        <el-button :size="size" @click="useExport(params, page)">
          导 出
        </el-button>
        <el-button
          v-show="$power.edit"
          :size="size"
          @click="useEdit(selectData)"
        >
          编 辑
        </el-button>
        <el-button
          :size="size"
          v-show="$power.subm"
          @click="useSubmit(selectData)"
        >
          提交OA
        </el-button>
        <el-button
          v-show="$power.delt"
          :size="size"
          @click="useDelete(selectData)"
        >
          删 除
        </el-button>
        <el-button
          v-show="$power.term"
          :size="size"
          @click="useTermination(selectData)"
        >
          终 止
        </el-button>
        <el-button
          v-show="$power.pige"
          :size="size"
          @click="usePigeonhole(selectData)"
        >
          归 档
        </el-button>
      </div>
      <!-- <el-button :size="size" @click="useImport"> 导 入 </el-button> -->
      <div>
        <el-button
          :size="size"
          :icon="batching ? 'el-icon-close' : 'el-icon-check'"
          @click="useBatch"
        >
          批 量
        </el-button>
      </div>
    </div>
    <el-table
      v-if="tableData.length"
      :size="size"
      :data="tableData"
      border
      stripe
      highlight-current-row
      @row-dblclick="useSee"
      @selection-change="useSelect"
      @row-click="useRowClick([$event])"
    >
      <el-table-column
        v-if="batching"
        fixed
        width="55"
        align="center"
        type="selection"
        :selectable="setSelectable"
      >
      </el-table-column>
      <el-table-column width="55" align="center" type="index">
        <template slot-scope="scope">
          {{ scope.row.$index }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="{ item, attr } in TableColumns"
        :key="item.key"
        :prop="item.key"
        :label="item.title"
        :width="item.width || 200"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <component
            v-if="attr.is === 'el-dict-tag'"
            v-bind="attr"
            v-model="scope.row[item.key]"
            :size="size"
            :options="dict.type[attr.dictName]"
          ></component>
          <component
            v-else-if="attr.is === 'el-file-preview'"
            v-bind="attr"
            v-model="scope.row[item.key]"
          ></component>
          <component
            v-else-if="attr.is === 'el-computed-input-v2'"
            v-bind="attr"
            v-model="scope.row[item.key]"
          ></component>
          <component is="span" v-else>{{
            scope.row[item.key] || "--"
          }}</component>
        </template>
      </el-table-column>
    </el-table>
    <el-empty v-else :image-size="200"></el-empty>
    <pagination
      :total="page.total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="useQuery(params, page)"
    />
  </el-card>
</template>
<style scoped lang="scss">
.el-card {
  width: calc(100% - 20px);
  height: 100%;
  margin: 10px;
  padding: 0 0 20px 0;
  .el-form {
    padding: 20px 0 0;
  }
  .el-row {
    display: flex;
    flex-wrap: wrap;
    padding: 0 20px;
  }
  ::v-deep .el-table--mini {
    width: 100%;
    margin: 20px 0 0 0;
    .el-table__cell {
      height: 50px;
    }
    td.el-table__cell {
      border-right: 0;
      // border-bottom: 0;
    }
  }
}
</style>
