<script>
import Column from "../add/column";
import { initRules, initParams } from "@/utils/init.js";
import { ITEM, LIST } from "@/api/business/purchase/apply";

export default {
  name: "SeeDrawer",

  data() {
    const {
      TabColumns,
      FormColumns,
      TabColumns: [
        {
          item: { key: tabName },
        },
      ],
    } = Column;
    const params = initParams([...TabColumns, ...FormColumns]);
    const rules = initRules(FormColumns);
    return {
      width: "50%",
      column: 2,
      title: "明 细",
      visible: false,
      loading: false,
      rules: rules,
      params: params,
      tabName,
      TabColumns,
      FormColumns,
    };
  },
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  computed: {
    $dicts: {
      get() {
        return this.$parent.$parent.$parent.$dicts;
      },
    },
    disabled: {
      get() {
        const { selectData } = this;
        if (selectData.length !== 1) {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    async fetchItem(prop) {
      try {
        // try
        this.loading = true;
        const { params } = this.$parent.$parent.$parent;
        const {
          total,
          rows: [{ id }],
        } = await LIST({ ...params }, { pageNum: prop, pageSize: 1 });
        const { code, data } = await ITEM(id);
        if (code === 200) {
          this.params = data;
          this.params.$index = prop;
          this.params.$total = total;
          return true;
        } else {
          return false;
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
    //
    async open(prop) {
      this.visible = await this.fetchItem(prop);
    },
    //
    async hide() {
      const { TabColumns, FormColumns, initParams } = this;
      this.visible = false;
      this.tabName = TabColumns[0].item.key;
      this.params = initParams([...TabColumns, ...FormColumns]);
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="open(selectData[0])"
  >
    {{ title }}
    <el-drawer
      :size="width"
      :title="title"
      :visible.sync="visible"
      append-to-body
    >
      <div
        style="
          z-index: 6666;
          position: fixed;
          right: 20px;
          top: 50%;
          transform: translateY(-50%);
          display: flex;
          flex-direction: column;
        "
      >
        <el-button
          :size="$attrs.size"
          :disabled="params.$index === 1"
          circle
          icon="el-icon-top"
          @click="fetchItem(params.$index - 1)"
          style="margin: 0 0 10px 0"
        ></el-button>
        <el-button
          :size="$attrs.size"
          :disabled="params.$index === params.$total"
          circle
          icon="el-icon-bottom"
          @click="fetchItem(params.$index + 1)"
          style="margin: 0"
        ></el-button>
      </div>
      <el-descriptions
        :size="$attrs.size"
        :column="column"
        border
        style="margin: 10px"
      >
        <el-descriptions-item
          v-if="params[item.key]"
          v-for="({ item, attr }, index) in FormColumns"
          :key="index"
          :label="item.title"
        >
          <dict-tag
            v-if="attr.is === 'el-select'"
            :size="$attrs.size"
            :value="params[item.key]"
            :options="$dicts[attr.dictName]"
          />
          <dr-file-preview
            v-else-if="attr.is === 'file-upload'"
            v-model="params[item.key]"
          ></dr-file-preview>
          <span v-else>{{ params[item.key] }}</span>
        </el-descriptions-item>
      </el-descriptions>
      <el-tabs v-model="tabName" :size="$attrs.size" style="margin: 10px">
        <el-tab-pane
          v-for="({ item, attr, TableColumns }, index) in TabColumns"
          :key="index"
          :name="item.key"
          :label="item.title"
          lazy
        >
          <el-table :size="$attrs.size" :data="params[item.key]">
            <el-table-column
              v-for="({ item: cItem, attr: cAttr }, cIndex) in TableColumns"
              :key="cIndex"
              :prop="cItem.key"
              :label="cItem.title"
              :width="cItem.width || 250"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <dict-tag
                  v-if="cAttr.is === 'el-select'"
                  :size="$attrs.size"
                  :value="scope.row[cItem.key]"
                  :options="$dicts[cAttr.dictName]"
                />
                <span v-else>{{
                  cAttr.formatter
                    ? cAttr.formatter(scope.row[cItem.key])
                    : scope.row[cItem.key]
                }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </el-button>
</template>
