<script>
import Column from "../add/column";
import useData from "../hooks/data";
import useDicts from "../hooks/dicts";
import useTable from "../hooks/table-function";
import useWatch from "../hooks/watch";
import useMethods from "../hooks/function";
const { watchContractType } = useWatch();

export default {
  name: "RecordDrawer",
  dicts: useDicts(Column),
  components: {},
  data() {
    return {
      title: "期 初 补 录",
      ...useData(Column),
    };
  },
  computed: {},
  watch: {
    "params.contractType": watchContractType(),
  },
  methods: {
    //
    async fetchRefer(prop, type, source) {
      const { fetchRefer } = useMethods();
      await fetchRefer({ _this: this, prop, type, source });
    },
    //
    async open() {
      const { open, fetchCode } = useMethods();
      await open({ _this: this });
      await fetchCode({ _this: this });
      const {
        user: {
          deptId: puDept,
          deptName: puDeptName,
          name: buyer,
          nickName: buyerName,
          orgId: puOrg,
          orgName: puOrgName,
        },
      } = this.$store.state;
      this.params.puOrg = puOrg;
      this.params.puOrgName = puOrgName;
      this.params.buyer = buyer;
      this.params.buyerName = buyerName;
      this.params.puDept = puDept;
      this.params.puDeptName = puDeptName;
      this.params.source = "期初补录";
    },
    //
    async hide() {
      const { hide } = useMethods();
      await hide({ _this: this });
    },
    //
    async useRowAdd(prop) {
      const { add } = useTable();
      await add({ _this: this, prop });
    },
    //
    async useRowRemove(prop, scope) {
      const { remove } = useTable();
      await remove({ _this: this, prop, scope });
    },
    //
    async useSubmit(prop) {
      const { submit } = useMethods();
      await submit({ _this: this, prop, type: "ADD" });
    },
  },
  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-close"
          @click="hide"
        ></el-button>
        <el-button
          :size="size"
          circle
          icon="el-icon-check"
          @click="useSubmit('ruleForm')"
        >
        </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 || 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%"
            ></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"
            >
            </dr-popover-select>
            <el-input
              v-if="column.inputType === 'Textarea'"
              v-model="params[column.key]"
              type="textarea"
              :disabled="column.disabled"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              style="width: 100%"
            ></el-input>
            <el-input-number
              v-if="column.inputType === 'InputNumber'"
              v-model="params[column.key]"
              :disabled="column.disabled"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              :controls-position="column.controlsPosition"
              style="width: 100%"
            ></el-input-number>
            <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>
            <el-date-picker
              v-if="column.inputType === 'DatePicker'"
              v-model="params[column.key]"
              :type="column.type"
              :disabled="column.disabled"
              :clearable="column.clearable"
              :placeholder="column.placeholder"
              :value-format="column.valueFormat"
              :picker-options="column.pickerOptions"
              style="width: 100%"
            >
            </el-date-picker>
            <file-upload
              v-if="column.inputType === 'Upload'"
              v-model="params[column.key]"
              :file-type="column.fileType"
            ></file-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <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"
          >
            <el-table :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"
                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>
                  <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-form>
  </el-drawer>
</template>
