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

export default {
  name: "SeeDrawer",
  props: {
    selectData: {
      type: [Array],
      require: true,
    },
  },
  components: {
    ElDictTag: () => import("@/components/DictTag/index.vue"),
    ElFilePreview: () => import("@/components/file-preview/index.vue"),
    ElComputedInputV2: () => import("@/components/computed-input-v2/index.vue"),
  },
  data() {
    const {
      TabColumns,
      FormColumns,
      TabColumns: [
        {
          item: { key: tabName },
        },
      ],
    } = useColumns();
    const params = initParams([...TabColumns, ...FormColumns]);
    return {
      column: 2,
      width: "50%",
      title: "明 细",
      visible: false,
      loading: false,
      params: params,
      tabName: tabName,
      TabColumns,
      FormColumns,
    };
  },
  computed: {
    $dicts: {
      get() {
        return this.$parent.$parent.$parent.dict.type;
      },
    },
    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,
        FormColumns,
        TabColumns: [
          {
            item: { key: tabName },
          },
        ],
      } = useColumns();
      this.visible = false;
      this.tabName = tabName;
      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
    >
      <el-descriptions
        :size="$attrs.size"
        :column="column"
        border
        style="margin: 10px"
      >
        <el-descriptions-item
          v-if="params[item.key]"
          v-for="{ item, attr } in FormColumns"
          :key="item.key"
          :label="item.title"
          :labelStyle="{ width: '150px' }"
          :contentStyle="{ width: 'auto' }"
        >
          <component
            v-if="attr.is === 'el-dict-tag'"
            v-bind="attr"
            v-model="params[item.key]"
            :size="$attrs.size"
            :options="$dicts[attr.dictName]"
          ></component>
          <component
            v-else-if="attr.is === 'el-file-preview'"
            v-bind="attr"
            v-model="params[item.key]"
          ></component>
          <component
            v-else-if="attr.is === 'el-computed-input-v2'"
            v-bind="attr"
            v-model="params[item.key]"
          ></component>
          <component is="span" v-else>{{ params[item.key] }}</component>
        </el-descriptions-item>
      </el-descriptions>
      <el-tabs v-model="tabName" :size="$attrs.size" style="margin: 10px">
        <el-tab-pane
          v-for="{ item, TableColumns } in TabColumns"
          :key="item.key"
          :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 } in TableColumns"
              :key="cItem.key"
              :prop="cItem.key"
              :label="cItem.title"
              :width="cItem.width || 300"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <component
                  v-if="cAttr.is === 'el-dict-tag'"
                  v-bind="cAttr"
                  v-model="scope.row[cItem.key]"
                  :size="$attrs.size"
                  :options="$dicts[cAttr.dictName]"
                ></component>
                <component
                  v-else-if="cAttr.is === 'el-file-preview'"
                  v-bind="cAttr"
                  v-model="scope.row[cItem.key]"
                ></component>
                <component
                  v-else-if="cAttr.is === 'el-computed-input-v2'"
                  v-bind="cAttr"
                  v-model="scope.row[cItem.key]"
                ></component>
                <component is="span" v-else>{{
                  scope.row[cItem.key] || "--"
                }}</component>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </el-button>
</template>
