<script>
import { LIST } from "@/api/business/purchase/apply";
import { FormColumns, SearchColumns } from "./column";
import { initPage, initDicts, initParams } from "@/utils/init";
export default {
  name: "PuchaseApply",
  dicts: [...initDicts(SearchColumns), ...initDicts(FormColumns)],
  components: {
    AddModel: () => import("./add/index.vue"),
    SeeModel: () => import("./see/index.vue"),
    EditModel: () => import("./edit/index.vue"),
    SubmitModel: () => import("./submit/index.vue"),
    DeleteModel: () => import("./delete/index.vue"),
  },
  data() {
    return {
      size: "mini",
      loading: false,
      searchColumns: SearchColumns,
      params: initParams(SearchColumns),
      tableData: [],
      selectData: [],
      tableColumns: FormColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {},
  created() {
    this.params.status = "0";
    this.useQuery(this.params, this.page);
  },
  methods: {
    //
    setSelectable(row) {
      const { status } = row;
      // 审批中
      if (status === "1") return false;
      // 已完成
      else if (status === "2") return false;
      // other
      else return true;
    },
    //
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { pageNum, pageSize } = page;
        const { code, rows, total } = await LIST(
          { ...prop },
          { pageNum, pageSize }
        );
        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.selectData = [];
      this.fetchList(prop, page);
    },
    // 重 置
    useReset() {
      this.page = initPage();
      this.params = initParams(SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      this.selectData = prop;
    },
    // 新 增
    async useAdd() {
      const { open } = this.$refs.AddModel;
      await open();
    },
    // 复 制
    async useCopy(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.AddModel;
      await open(id);
    },
    hasPowerCopy(prop) {
      return prop.length === 1;
    },
    // 删 除
    async useDelete(prop) {
      const { open } = this.$refs.DeleteModel;
      await open(prop);
    },
    hasPowerDelete(prop) {
      if (prop.length === 1) {
        const [{ status }] = prop;
        if (status === "1") return false;
        else if (status === "2") return false;
        else return true;
      } else {
        return !!prop.length;
      }
    },
    // 编 辑
    async useEdit(prop) {
      const [{ id }] = prop;
      const { open } = this.$refs.EditModel;
      await open(id);
    },
    hasPowerEdit(prop) {
      if (prop.length === 1) {
        const [{ status }] = prop;
        if (status === "1") return false;
        else if (status === "2") return false;
        else return true;
      } else {
        return !!prop.length;
      }
    },
    // 明 细
    async useSee(prop) {
      const { id } = prop;
      const { open } = this.$refs.SeeModel;
      await open(id);
    },
    // 审 核
    async useSubmit(prop, done) {
      const { open } = this.$refs.SubmitModel;
      await open(prop, done);
    },
    hasPowerSubmit(prop) {
      if (prop.length === 1) {
        const [{ status }] = prop;
        if (status === "1") return false;
        else if (status === "2") return false;
        else return true;
      } else {
        return !!prop.length;
      }
    },
  },
};
</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>
    <edit-model ref="EditModel" @success="useQuery(params, page)"></edit-model>
    <submit-model
      ref="SubmitModel"
      @success="useQuery(params, page)"
    ></submit-model>
    <delete-model
      ref="DeleteModel"
      @success="useQuery(params, page)"
    ></delete-model>
    <el-form
      :size="size"
      :model="params"
      label-width="auto"
      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-if="column.inputType === 'Input'"
              v-model="params[column.key]"
              :placeholder="column.placeholder"
              style="width: 100%"
              @keyup.enter.native="useQuery(params, page)"
            ></el-input>
            <el-select
              v-if="column.inputType === 'Select'"
              v-model="params[column.key]"
              :disabled="column.disabled"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              style="width: 100%"
              @change="useQuery(params, page)"
              @keyup.enter.native="useQuery(params, page)"
            >
              <el-option
                v-for="item in dict.type[column.referName]"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <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"
              @change="useQuery(params, page)"
              @keyup.enter.native="useQuery(params, page)"
            >
            </dr-popover-select>
          </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
        v-show="hasPowerCopy(selectData)"
        :size="size"
        @click="useCopy(selectData)"
      >
        复 制
      </el-button>
      <el-button
        v-show="hasPowerDelete(selectData)"
        :size="size"
        @click="useDelete(selectData)"
      >
        删 除
      </el-button>
    </el-row>
    <el-table
      :size="size"
      :data="tableData"
      highlight-current-row
      @row-dblclick="useSee"
      @selection-change="useSelect"
      @row-click="useSelect([$event])"
      style="width: 100%; margin: 20px 0 0 0"
    >
      <el-table-column
        fixed
        width="55"
        align="center"
        type="selection"
        :selectable="setSelectable"
      >
      </el-table-column>
      <el-table-column
        v-for="(column, index) in tableColumns"
        :key="index"
        :prop="column.key"
        :label="column.title"
        :width="column.width || 200"
        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' && scope.row[column.key]"
            v-model="scope.row[column.key]"
          ></dr-file-preview>
          <span v-else>{{ scope.row[column.key] }}</span>
        </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>
