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

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: "100%",
      column: 3,
      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 fetchItem(prop) {
      try {
        // try
        this.loading = true;
        const { model } = this.$props;
        const { billDates } = model;
        const [startTime, endTime] = billDates || [];

        const {
          total,
          rows: [{ id }],
        } = await LIST(
          { ...model, startTime, endTime },
          { pageNum: prop, pageSize: 1 }
        );
        const { code, data } = await ITEM(id, false);
        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() {
      const {
        TabColumns: [
          {
            item: { key: tabName },
          },
        ],
      } = useColumns();
      this.visible = false;
      this.tabName = tabName;
      this.$emit("success");
    },
    async jumpOA() {
      toOA(this.$store.state.user.name, this.params.flowId).then((res) => {
        if (res.code === 200) {
          window.open(res.oaUrl);
        }
      });
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-button
    v-bind="$attrs"
    v-on="$listeners"
    :disabled="disabled"
    @click="open"
  >
    {{ title }}
    <el-drawer
      :show-close="false"
      :size="width"
      :title="title"
      :visible.sync="visible"
      append-to-body
      @close="hide(selectData)"
    >
      <template slot="title">
        <span>{{ title }}</span>
        <el-button
          :size="$attrs.size"
          :loading="loading"
          @click="jumpOA"
          v-if="params.flowId"
        >
          流程跳转
        </el-button>
        <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-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
            index
            v-model="params[item.key]"
            :size="$attrs.size"
            :dict="dict"
            :columns="columns"
            :cell-style="{ height: '30px' }"
          >
          </el-super-table>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </el-button>
</template>
