<script>
import Column from "./column";
import useData from "../hooks/data";
import useDicts from "../hooks/dicts";
import { ITEM } from "@/api/business/purchase/contract";

export default {
  name: "SeeDrawer",
  dicts: useDicts(Column),
  components: {},
  data() {
    return {
      column: 3,
      title: "明 细",
      ...useData(Column),
    };
  },
  computed: {
    root: function () {
      return this.$parent.$parent;
    },
  },
  watch: {},
  methods: {
    async fetchItem(prop) {
      try {
        // try
        this.loading = true;
        const { code, data } = await ITEM(prop);
        if (code === 200) {
          this.params = data;
          this.tabName = this.tabColumns[0].key;
          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() {
      this.visible = false;
    },
    //
    async useDelete(prop) {
      await this.root
        .useDelete(prop)
        .then(() => {
          this.hide();
        })
        .catch(() => {});
    },
    //
    async useTermination(prop) {
      await this.root
        .useTermination(prop)
        .then(() => {
          this.fetchItem(this.params.id);
        })
        .catch(() => {});
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
};
</script>
<template>
  <el-drawer
    :size="width"
    :title="title"
    :show-close="false"
    :visible.sync="visible"
  >
    <template slot="title">
      <span>{{ title }}</span>
      <span>
        <el-tooltip
          v-if="root.hasPowerDelete([params])"
          effect="dark"
          content="删 除"
          placement="bottom-end"
        >
          <el-button
            :size="size"
            circle
            icon="el-icon-delete"
            @click="useDelete([params])"
          ></el-button>
        </el-tooltip>
        <el-tooltip
          v-if="root.hasPowerEdit([params])"
          effect="dark"
          content="编 辑"
          placement="bottom-end"
        >
          <el-button
            :size="size"
            circle
            icon="el-icon-edit"
            @click="root.useEdit([params])"
          ></el-button>
        </el-tooltip>
        <el-tooltip
          v-if="root.hasPowerTermination([params])"
          effect="dark"
          content="终 止"
          placement="bottom-end"
        >
          <el-button
            :size="size"
            circle
            icon="el-icon-light-rain"
            @click="useTermination([params])"
          ></el-button>
        </el-tooltip>
        <el-tooltip
          v-if="root.hasPowerPigeonhole([params])"
          effect="dark"
          content="归 档"
          placement="bottom-end"
        >
          <el-button
            :size="size"
            circle
            icon="el-icon-lightning"
            @click="root.usePigeonhole([params])"
          ></el-button>
        </el-tooltip>
        <el-tooltip
          v-if="root.hasPowerSubmit([params])"
          effect="dark"
          content="提交OA"
          placement="bottom-end"
        >
          <el-button
            :size="size"
            circle
            icon="el-icon-heavy-rain"
            @click="root.useSubmit([params])"
          ></el-button>
        </el-tooltip>
        <el-button
          :size="size"
          circle
          type="danger"
          icon="el-icon-close"
          @click="hide"
        ></el-button>
      </span>
    </template>
    <el-descriptions :size="size" :column="column" border style="margin: 10px">
      <el-descriptions-item
        v-if="params[column.key]"
        v-for="(column, index) in formColumns"
        :key="index"
        :label="column.title"
        :labelStyle="{ width: '150px' }"
        :contentStyle="{ width: 'auto' }"
      >
        <dict-tag
          v-if="column.inputType === 'Select'"
          :size="size"
          :value="params[column.key]"
          :options="dict.type[column.referName]"
        />
        <span v-else-if="column.inputType === 'Upload'">
          <dr-file-preview v-model="params[column.key]"></dr-file-preview>
        </span>
        <span v-else>{{ params[column.key] }}</span>
      </el-descriptions-item>
    </el-descriptions>
    <el-tabs v-model="tabName" :size="size" style="margin: 10px">
      <el-tab-pane
        v-for="(column, index) in tabColumns"
        :key="index"
        :name="column.key"
        :label="column.title"
        lazy
      >
        <el-table :size="size" :data="params[column.key]">
          <el-table-column
            v-for="(cColumn, cIndex) in column.tableColumns"
            :key="cIndex"
            :prop="cColumn.key"
            :label="cColumn.title"
            :width="cColumn.width"
          >
            <template slot-scope="scope">
              <dict-tag
                v-if="cColumn.inputType === 'Select'"
                :size="size"
                :value="scope.row[cColumn.key]"
                :options="dict.type[cColumn.referName]"
              />
              <span v-else>{{ scope.row[cColumn.key] }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </el-drawer>
</template>
