<script>
import useColumns from "./columns";
import { ITEM } from "@/api/business/purchase/contract";

export default {
  name: "SeeDrawer",
  props: {
    dict: {
      type: Object,
    },
    selectData: {
      type: [Array],
      require: true,
    },
  },
  components: {
    ElSuperDescriptions: () =>
      import("@/components/super-descriptions/index.vue"),
    ElSuperTable: () => import("@/components/super-table/index.vue"),
  },
  data() {
    const {
      TabColumns,
      TableColumns,
      TabColumns: [
        {
          item: { key: tabName },
        },
      ],
    } = useColumns();
    return {
      column: 2,
      width: "100%",
      title: "明 细",
      visible: false,
      loading: false,
      params: {},
      tabName: tabName,
      TabColumns,
      TableColumns,
    };
  },
  computed: {
    disabled: {
      get() {
        const { selectData } = this;
        if (selectData.length !== 1) {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    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 = await this.fetchItem(prop);
    },
    //
    async hide() {
      const {
        TabColumns: [
          {
            item: { key: tabName },
          },
        ],
      } = useColumns();
      this.visible = false;
      this.tabName = tabName;
    },
  },
  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
    >
      <el-super-descriptions
        v-model="params"
        :dict="dict"
        :column="column"
        :size="$attrs.size"
        :columns="TableColumns"
      >
      </el-super-descriptions>
      <el-tabs v-model="tabName" :size="$attrs.size" style="margin: 10px">
        <el-tab-pane
          v-for="{ item, TableColumns: columns } in TabColumns"
          :key="item.key"
          :name="item.key"
          :label="item.title"
          lazy
        >
          <el-super-table
            v-model="params[item.key]"
            :size="$attrs.size"
            :dict="dict"
            :columns="columns"
            :iconOperation="false"
          >
          </el-super-table>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </el-button>
</template>
