<script>
import useColumns from "./columns";
import { getTargetTemplate } from "@/api/business/spd/starget/targetTemplate";

export default {
  name: "SeeDrawer",
  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 {
      width: "50%",
      column: 2,
      title: "明 细",
      visible: false,
      loading: false,
      params: {},
      tabName,
      TabColumns: TabColumns,
      TableColumns: TableColumns,
    };
  },
  props: {
    dict: {
      type: Object,
    },
    model: {
      type: Object,
    },
    selectData: {
      type: [Array],
      require: true,
    },
  },
  computed: {
    disabled: {
      get() {
        const { selectData } = this;
        if (selectData.length !== 1) {
          return true;
        }
      },
      set() {},
    },
  },
  watch: {},
  methods: {
    //
    async fetchTemplate(id) {
      try {
        this.loading = true;
        const { code, data } = await getTargetTemplate(id);
        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.fetchTemplate(prop[0].id);
    },
    //
    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"
  >
    {{ title }}
    <el-drawer
      :size="width"
      :title="title"
      :visible.sync="visible"
      append-to-body
      @close="hide(selectData)"
    >
      <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-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 }, index) in TabColumns"
          :key="index"
          :name="item.key"
          :label="item.title"
          lazy
        >
          <el-super-table
            v-model="params[item.key]"
            :size="$attrs.size"
            :dict="dict"
            :columns="columns"
          >
          </el-super-table>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </el-button>
</template>
