<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="18">
        <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.remark }}</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>
      </a-col>
      <a-col class="gutter-row" :span="6">
        <a-steps direction="vertical" size="small" :current="current" :status="taskStatusStr">
          <a-step :key="task.id" :title="task.title" v-for="task in taskList" :description="'操作人：'+(task.userName? task.userName:'未审批')" />
        </a-steps>
      </a-col>
    </a-row>

    <a-form :form="form">
      <row-list :col="1" v-show="false">
        <row-item>
          <a-form-item>
            <a-input v-decorator="['id']" type="hidden"/>
          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="2">
        <row-item>
          <a-form-item
            label="审批"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select
              v-model="taskStatus"
            >
              <a-select-option :value="4">
                通过
              </a-select-option>
              <a-select-option :value="5">
                不通过
              </a-select-option>
            </a-select>

          </a-form-item>
        </row-item>
      </row-list>
      <row-list :col="1">
        <row-item>
          <a-form-item
            label="说明"
            :labelCol="BaseTool.Constant.labelCol2"
            :wrapperCol="BaseTool.Constant.wrapperCol2"
          >
            <a-textarea
              :rows="4"
              v-model="taskRemark"/>
          </a-form-item>
        </row-item>
      </row-list>
    </a-form>
    <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="save()">保存</a-button>
      <a-button :loading="confirmLoading" type="primary" @click="handleCancel()">返回</a-button>
    </template>
  </a-modal>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { fetchSbInfo } from '@/api/sb/info'
import { auditSbAllocateApply } from '@/api/sb/allocate-apply'
const DetailListItem = DetailList.Item

export default {
  name: 'SbAllocateApplyDetail',
  components: {
    DetailList,
    DetailListItem
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      taskStatus: 4,
      taskRemark: null,
      modalTitle: null,
      form: this.$form.createForm(this),
      visible: false,
      // 下拉框map
      typeMap: {},
      taskList: [],
      levelMap: {},
      statusMap: {},
      current: 1,
      taskStatusStr: '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.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 : []
      this.taskList.forEach((task, index) => {
        console.log(333333, task)
        if (task.status === 4 || task.status === 5) {
          this.current = index
        } else {
          this.current = index - 1
        }
        if (task.status === 3) {
          this.taskStatusStr = 'wait'
        } else if (task.status === 4) {
          this.taskStatusStr = 'finish'
        } else if (task.status === 5) {
          this.taskStatusStr = 'error'
        } else {
          this.taskStatusStr = 'finish'
        }
      })
    },
    save () {
      console.log(this.taskStatus, this.taskRemark, this.model.id)
      this.confirmLoading = true
      auditSbAllocateApply({ id: this.model.id, taskStatus: this.taskStatus, taskRemark: this.taskRemark })
        .then(() => {
          this.handleCancel({})
        }).catch(() => {
          this.confirmLoading = false
        })
    },
    handleView (record) {
      fetchSbInfo({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        modal.base(res.data)
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.taskStatus = 4
      this.taskRemark = null
      if (this.BaseTool.Object.isNotBlank(values)) {
        this.$emit('ok', values)
      }
    }

  }
}
</script>
