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

export default {
  name: "SeeDrawer",
  components: {
    ElSuperDescriptions: () =>
      import("@/components/super-descriptions/index.vue"),
  },
  data() {
    const { TableColumns } = useColumns();
    return {
      column: 3,
      title: "明 细",
      width: "100%",
      visible: false,
      loading: false,
      params: {},
      TableColumns: TableColumns,
    };
  },
  props: {
    dict: {
      type: Object,
    },
    selectData: {
      type: [Array],
      require: true,
    },
  },
  computed: {
    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) {
      const [{ $index }] = prop;
      this.visible = await this.fetchItem($index);
    },
    //
    async hide() {
      this.visible = false;
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="open(selectData)"
  >
    {{ title }}
    <el-drawer
      :show-close="false"
      :size="width"
      :title="title"
      :visible.sync="visible"
      append-to-body
    >
      <template slot="title">
        <span>{{ title }}</span>
        <el-button :size="$attrs.size" :loading="loading" @click="hide">
          取 消
        </el-button>
      </template>
      <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-drawer>
  </el-button>
</template>
