<template>
  <a-modal
    :title="modalTitle"
    :width="850"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <a-row :gutter="24">
      <a-col class="gutter-row" :span="16">
        <detail-list title="" :col="2">
          <detail-list-item term="标题">{{ model.title }}</detail-list-item>
          <detail-list-item term="所属部门">{{ model.currentDeptName }}</detail-list-item>
          <detail-list-item term="目标部门">{{ model.targetDeptName }}</detail-list-item>
          <detail-list-item term="申请时间">{{ model.startTime }}</detail-list-item>
          <detail-list-item term="结束时间">{{ model.endTime }}</detail-list-item>
          <detail-list-item term="申请人">{{ model.createdUserName }}</detail-list-item>
          <detail-list-item term="状态"><badge :status="DictCache.COLOR.SB_ALLOCATE_APPLY_STATUS[model.status]" :text="applyStatusMap[model.status]"></badge></detail-list-item>
        </detail-list>
        <detail-list title="" :col="1">
          <detail-list-item term="备注">{{ model.remark }}</detail-list-item>
        </detail-list>
      </a-col>
      <a-col class="gutter-row" :span="8">
        <a-steps direction="vertical" size="small" :current="current" :status="taskStatus">
          <a-step
            :key="task.id"
            v-for="(task,index) in taskList"
            :class="current === index
              && task.status === DictCache.VALUE.SB_ALLOCATE_TASK_STATUS.AUDIT_WAIT?'future-step':''">
            <template slot="title">
              <a-tooltip :title="task.remark">
                {{ task.title }}
              </a-tooltip>
            </template>
            <template slot="description">
              <a-tooltip :title="task.remark">
                {{ '操作人：'+(task.userName? task.userName:'未审批') }}
                <div>{{ task.updateTime|| task.createdTime }}</div>
              </a-tooltip>

            </template>
          </a-step>
        </a-steps>
      </a-col>
    </a-row>
    <a-tabs type="card" default-active-key="1">
      <a-tab-pane key="1" tab="设备列表">
        <a-table
          bordered
          :data-source="data"
          :columns="columns"
          tableLayout="auto"
          rowKey="id"
          :scroll="{ x: 1600, y: BaseTool.Constant.scrollY }"
        >
          <span slot="action" slot-scope="record">
            <template>
              <a @click="handleView(record)">查看</a>
            </template>
          </span>
          <span slot="status" slot-scope="text">
            <badge :status="DictCache.COLOR.SB_INFO_STATUS[text]" :text="statusMap[text]" />
          </span>
        </a-table>
      </a-tab-pane>
    </a-tabs>
    <template slot="footer">
      <a-button :loading="confirmLoading" type="primary" @click="handleCancel()">返回</a-button>
    </template>
    <sb-detail ref="sbDetail"/>
  </a-modal>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { fetchSbInfo } from '@/api/sb/info'
import SbDetail from '@/views/sb/info/modules/Detail'
const DetailListItem = DetailList.Item

export default {
  name: 'SbAllocateApplyDetail',
  components: {
    DetailList,
    SbDetail,
    DetailListItem
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      // 下拉框map
      typeMap: {},
      taskList: [],
      levelMap: {},
      applyStatusMap: {},
      statusMap: {},
      current: 1,
      taskStatus: 'process',
      rowSelection: {
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys
          this.selectedRows = selectedRows
        }
      },
      treeData: [],
      // 表头
      columns: [
        {
          title: '设备编号',
          dataIndex: 'no',
          width: 200
        },
        {
          title: '设备名称',
          dataIndex: 'name',
          width: 200
        },
        {
          title: '设备类型',
          dataIndex: 'type',
          width: 200,
          customRender: (text, record, index) => {
            return record.typeName
          }
        },
        {
          title: '设备等级',
          dataIndex: 'level',
          width: 200,
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.levelMap, text)
          }
        },
        {
          title: '生产商',
          dataIndex: 'producerId',
          width: 200,
          customRender: (text, record, index) => {
            return record.producerName
          }
        },
        {
          title: '规格型号',
          dataIndex: 'guigeId',
          width: 200,
          customRender: (text, record, index) => {
            return record.guigeName
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 200,
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          align: 'center',
          fixed: 'right',
          scopedSlots: { width: 200, customRender: 'action' }
        }
      ],
      data: [],
      model: {
        'title': null,
        'type': null,
        'currentDept': null,
        'targetDept': null,
        'currentDeptName': null,
        'targetDeptName': null,
        'remark': null,
        'startTime': null,
        'endTime': null,
        'createdUserId': null,
        'updateUserId': null,
        'updateUserName': null,
        'updateTime': null,
        'sbList': []
      }
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_ALLOCATE_APPLY_TYPE)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_INFO_STATUS)
    this.applyStatusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_ALLOCATE_APPLY_STATUS)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_LEVEL)
  },
  methods: {
    base (record) {
      this.visible = true
      this.modalTitle = '详情'
      this.model = record
      this.taskList = record.taskList
      this.data = record.sbInfos !== null ? record.sbInfos : []
      let flag = false
      for (let index = 0; index < this.taskList.length; index++) {
        const task = this.taskList[index]
        if (task.status === this.DictCache.VALUE.SB_ALLOCATE_TASK_STATUS.AUDIT_LOSE) {
          flag = true
          this.current = index
          this.taskStatus = 'error'
          break
        }
        if (task.status === this.DictCache.VALUE.SB_ALLOCATE_TASK_STATUS.AUDIT_WAIT) {
          flag = true
          this.current = index
          this.taskStatus = 'process'
          break
        }
      }
      if (!flag) {
        this.taskStatus = 'finish'
        this.current = this.taskList.length - 1
      }
    },
    handleView (record) {
      fetchSbInfo({ id: record.id }).then(res => {
        const modal = this.$refs.sbDetail
        modal.base(res.data)
      })
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok', '')
    }

  }
}
</script>
<style>
  .future-step .ant-steps-icon{
    color: #fff !important;
  }
</style>
