<script>
import { LIST } from "@/api/business/purchase/task";
import { TableColumns, SearchColumns } from "./column";
import { initPage, initDicts, initParams } from "@/utils/init";
export default {
  name: "PuchaseTask",
  dicts: [...initDicts(TableColumns), ...initDicts(SearchColumns)],
  components: {
    SeeModel: () => import("./see/index.vue"),
    CloseModel: () => import("./close/index.vue"),
    ExportModel: () => import("./export/index.vue"),
    ShiftModel: () => import("./modify-buyer/index.vue"),
    DirectModel: () => import("./first-direct/index.vue"),
    ReturnModel: () => import("./documents-return/index.vue"),
  },
  data() {
    return {
      size: "mini",
      loading: false,
      searchColumns: SearchColumns,
      params: initParams(SearchColumns),
      tableData: [],
      selectData: [],
      tableColumns: TableColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.useQuery(this.params, this.page);
  },
  methods: {
    //
    setSelectable(row) {
      const { status } = row;
      if (status === "1") return true;
      else if (status === "3") return true;
      else return true;
    },
    //
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { code, rows, total } = await LIST(prop, page);
        if (code === 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (err) {
        // catch
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    async useQuery(prop, page) {
      const { pageNum, pageSize } = page;
      const {
        date: [startDate, endDate],
        documentsCodes: code,
      } = prop;
      const documentsCodes = code
        ? code.replace(/\s*/g, "").replaceAll("，", ",")
        : undefined;
      await this.fetchList(
        {
          ...prop,
          endDate,
          startDate,
          documentsCodes,
          date: undefined,
        },
        { pageNum, pageSize, isAsc: "desc", orderByColumn: "updateTime" }
      );
    },
    // 重 置
    useReset() {
      this.page = initPage();
      this.params = initParams(SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      this.selectData = prop;
    },
    // 行 关
    async useClose(prop) {
      const { open } = this.$refs.CloseModel;
      await open(prop);
    },
    // 退 回
    async useReturn(prop) {
      const { open } = this.$refs.ReturnModel;
      await open(prop);
    },
    // 转 派
    async useShift(prop) {
      const { id } = prop;
      const { open } = this.$refs.ShiftModel;
      await open(id);
    },
    // 明 细
    async useSee(prop) {
      const { id } = prop;
      const { open } = this.$refs.SeeModel;
      await open(id);
    },
    // 直 采
    async useDirect(prop) {
      const { open } = this.$refs.DirectModel;
      await open(prop);
    },
    // 导 出
    async useExport(prop, page) {
      const { open } = this.$refs.ExportModel;
      await open(prop, page);
    },
  },
};
</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>
    <export-model ref="ExportModel"></export-model>
    <close-model ref="CloseModel" @success="useReset"></close-model>
    <shift-model ref="ShiftModel" @success="useReset"></shift-model>
    <direct-model ref="DirectModel" @success="useReset"></direct-model>
    <return-model ref="ReturnModel" @success="useReset"></return-model>
    <el-backtop target=".el-scrollbar__wrap"></el-backtop>
    <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-if="column.inputType === 'Input'"
              v-model="params[column.key]"
              :disabled="column.disabled"
              :clearable="column.clearable"
              :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>
            <el-date-picker
              v-if="column.inputType === 'DatePicker'"
              v-model="params[column.key]"
              :type="column.type"
              :placeholder="column.placeholder"
              :value-format="column.valueFormat"
              :unlink-panels="column.unlinkPanels"
              :picker-options="column.pickerOptions"
              :range-separator="column.rangeSeparator"
              :end-placeholder="column.endPlaceholder"
              :start-placeholder="column.startPlaceholder"
              style="width: 100%"
              @change="useQuery(params, page)"
              @keyup.enter.native="useQuery(params, page)"
            >
            </el-date-picker>
            <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"
              @change="useQuery(params, page)"
              @keyup.enter.native="useQuery(params, page)"
            >
            </dr-popover-select>
            <dr-popover-tree-select
              v-if="column.inputType === 'PopoverTreeSelect'"
              v-model="params[column.key]"
              :source.sync="params"
              :title="column.title"
              :type="column.referName"
              :multiple="column.multiple"
              :value-key="column.valueKey"
              :placeholder="column.placeholder"
              :query-params="column.queryParams"
              @change="useQuery(params, page)"
              @keyup.enter.native="useQuery(params, page)"
            >
            </dr-popover-tree-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="useExport(params, page)">
        导 出
      </el-button> -->
      <el-button
        :size="size"
        :disabled="selectData.length !== 1"
        @click="useShift(selectData[0])"
      >
        转 派
      </el-button>
      <el-button
        :size="size"
        :disabled="!selectData.length"
        @click="useReturn(selectData)"
      >
        退回需求
      </el-button>
      <!-- <el-button
          :size="size"
          :disabled="selectData.length !== 1"
          @click="useClose(selectData[0])"
        >
          行关闭
        </el-button> -->
      <el-button
        :size="size"
        :disabled="!selectData.length"
        @click="useDirect(selectData)"
      >
        协议直采
      </el-button>
    </el-row>
    <el-table
      :data="tableData"
      size="mini"
      style="width: 100%; margin: 20px 0 0 0"
      @row-dblclick="useSee"
      @selection-change="useSelect"
    >
      <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 || 180"
        :show-overflow-tooltip="column.showOverflowTooltip || true"
      >
        <template slot-scope="scope">
          <dict-tag
            v-if="column.referName"
            :size="size"
            :value="scope.row[column.key]"
            :options="dict.type[column.referName]"
          />
          <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>
