<script>
import Column from "./column";
import useData from "../hooks/data";
import useDicts from "../hooks/dicts";
import useWatch from "../hooks/watch";
import useMethods from "../hooks/function";
import { ITEM, SAVE } from "@/api/business/purchase/apply";
const { watchPuOrgName } = useWatch();

export default {
  name: "AddDrawer",
  dicts: useDicts(Column),
  components: {},
  data() {
    return {
      title: "新 增",
      ...useData(Column),
    };
  },
  computed: {},
  watch: {
    "params.puOrgName": watchPuOrgName(),
  },
  methods: {
    //
    async fetchRefer(prop, type, source) {
      const { fetchRefer } = useMethods();
      await fetchRefer({ _this: this, prop, type, source });
    },
    //
    async fetchItem(prop) {
      try {
        // try
        this.loading = true;
        const { code, data } = await ITEM(prop);
        if (code === 200) {
          this.params = data;
          return true;
        } else {
          return false;
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
    //
    async open(prop) {
      this.visible = prop ? await this.fetchItem(prop) : true;
    },
    //
    async hide() {
      this.visible = false;
      this.params = this.resetParams();
      this.tabName = this.tabColumns[0].key;
    },
    //
    async useRowAdd(prop) {
      const {
        $notify,
        params: { tabColumns, puOrgName, supplierName },
      } = this;
      if (!supplierName) {
        return $notify.info("请选择供应商");
      }
      if (!puOrgName) {
        return $notify.info("请选择采购组织");
      }
      const { tableColumns } = tabColumns.find(
        (element) => element.key === prop
      );
      this.params[prop].push(initParams(tableColumns));
    },
    //
    async useRowRemove(prop, scope) {
      const { $index } = scope;
      this.params[prop].splice($index, 1);
    },
    //
    async useSubmit(prop) {
      this.$refs[prop].validate(async (valid) => {
        if (valid) {
          try {
            // try
            const {
              priceApplyOrgs: _priceApplyOrgs,
              priceApplyItems: _priceApplyItems,
            } = this.params;
            const id = undefined;
            const priceApplyOrgs = _priceApplyOrgs.map((item) => ({
              ...item,
              id: undefined,
              applyId: undefined,
            }));
            const priceApplyItems = _priceApplyItems.map((item) => ({
              ...item,
              id: undefined,
              applyId: undefined,
            }));
            const params = {
              ...this.params,
              id,
              priceApplyOrgs,
              priceApplyItems,
            };
            const { msg, code } = await SAVE(params);
            if (code === 200) {
              this.hide();
              this.$emit("success");
              this.$notify.success(msg);
            }
          } catch (err) {
            // catch
            console.error(err);
          } finally {
            // finally
            this.loading = false;
          }
        } else {
          return false;
        }
      });
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-drawer
    :size="width"
    :title="title"
    :show-close="false"
    :visible.sync="visible"
  >
    <template slot="title">
      <span>{{ title }}</span>
      <span>
        <el-button
          :size="size"
          circle
          icon="el-icon-check"
          @click="useSubmit('ruleForm')"
        >
        </el-button>
        <el-button
          :size="size"
          circle
          type="danger"
          icon="el-icon-close"
          @click="hide"
        ></el-button>
      </span>
    </template>
    <el-form
      ref="ruleForm"
      v-loading="loading"
      :size="size"
      :rules="rules"
      :model="params"
      label-width="auto"
      label-position="right"
      style="padding: 10px"
    >
      <el-row :gutter="20" style="display: flex; flex-wrap: wrap">
        <el-col
          v-for="(column, index) in formColumns"
          :key="index"
          :span="column.span || 8"
        >
          <el-form-item :prop="column.key" :label="column.title">
            <el-input
              v-if="column.inputType === 'Input'"
              v-model="params[column.key]"
              :disabled="column.disabled"
              :readonly="column.readonly"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              style="width: 100%"
            ></el-input>
            <dr-popover-select
              v-if="column.inputType === 'PopoverSelect'"
              v-model="params[column.key]"
              :source.sync="params"
              :title="column.title"
              :type="column.referName"
              :disabled="column.disabled"
              :readonly="column.readonly"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              :data-mapping="column.dataMapping"
              style="width: 100%"
            >
            </dr-popover-select>
            <el-select
              v-if="column.inputType === 'Select'"
              v-model="params[column.key]"
              :disabled="column.disabled"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              style="width: 100%"
            >
              <el-option
                v-for="item in dict.type[column.referName]"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <file-upload
              v-if="column.inputType === 'Upload'"
              v-model="params[column.key]"
              :file-type="column.fileType"
            ></file-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label-width="0">
            <el-tabs v-model="tabName">
              <el-tab-pane
                v-for="(column, index) in tabColumns"
                :key="index"
                :label="column.title"
                :name="column.key"
                lazy
              >
                <el-table :size="size" :data="params[column.key]">
                  <el-table-column label="序号">
                    <template slot-scope="scope">
                      {{ scope.$index + 1 }}
                    </template>
                  </el-table-column>
                  <el-table-column
                    v-for="(cColumn, cIndex) in column.tableColumns"
                    :key="cIndex"
                    :prop="cColumn.key"
                    :label="cColumn.title"
                    :width="cColumn.width || 200"
                    show-overflow-tooltip
                  >
                    <template slot-scope="scope">
                      <el-input
                        v-if="cColumn.inputType === 'Input'"
                        v-model="scope.row[cColumn.key]"
                        :size="size"
                        :disabled="cColumn.disabled"
                        :clearable="cColumn.clearable"
                        :placeholder="cColumn.placeholder"
                        style="width: 100%"
                      ></el-input>
                      <dr-computed-input
                        v-else-if="cColumn.inputType === 'ComputedInput'"
                        v-model="scope.row[cColumn.key]"
                        :source="scope.row"
                        :computed="cColumn.computed"
                        :placeholder="cColumn.placeholder"
                        style="width: 100%"
                      ></dr-computed-input>
                      <dr-popover-select
                        v-else-if="cColumn.inputType === 'PopoverSelect'"
                        v-model="scope.row[cColumn.key]"
                        :size="size"
                        :title="cColumn.title"
                        :source.sync="scope.row"
                        :type="cColumn.referName"
                        :disabled="cColumn.disabled"
                        :readonly="cColumn.readonly"
                        :clearable="cColumn.clearable"
                        :placeholder="cColumn.placeholder"
                        :data-mapping="cColumn.dataMapping"
                        @change="fetchRefer"
                      >
                      </dr-popover-select>
                      <el-input-number
                        v-else-if="cColumn.inputType === 'InputNumber'"
                        v-model="scope.row[cColumn.key]"
                        :size="size"
                        :disabled="cColumn.disabled"
                        :clearable="cColumn.clearable"
                        :placeholder="cColumn.placeholder"
                        :controls-position="cColumn.controlsPosition"
                        style="width: 100%"
                      ></el-input-number>
                      <el-select
                        v-else-if="cColumn.inputType === 'Select'"
                        v-model="scope.row[cColumn.key]"
                        :disabled="cColumn.disabled"
                        :clearable="cColumn.clearable"
                        :placeholder="cColumn.placeholder"
                        style="width: 100%"
                      >
                        <el-option
                          v-for="item in dict.type[cColumn.referName]"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        >
                        </el-option>
                      </el-select>
                      <el-date-picker
                        v-else-if="cColumn.inputType === 'DatePicker'"
                        v-model="scope.row[cColumn.key]"
                        :type="cColumn.type"
                        :disabled="cColumn.disabled"
                        :clearable="cColumn.clearable"
                        :placeholder="cColumn.placeholder"
                        :value-format="cColumn.valueFormat"
                        :picker-options="cColumn.pickerOptions"
                        style="width: 100%"
                      >
                      </el-date-picker>
                      <span v-else> {{ scope.row[cColumn.key] }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column fixed="right" label="操作" width="100">
                    <template slot="header" slot-scope="scope">
                      <el-button
                        circle
                        icon="el-icon-plus"
                        :size="size"
                        @click="useRowAdd(tabName)"
                      >
                      </el-button>
                    </template>
                    <template slot-scope="scope">
                      <el-button
                        circle
                        icon="el-icon-minus"
                        :size="size"
                        @click.native.prevent="useRowRemove(tabName, scope)"
                      >
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-drawer>
</template>
