<template>
  <div v-show="visible">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>
    <title-divider title="设备信息" width="90px"></title-divider>
    <detail-list title="" :col="3">
      <detail-list-item term="设备新号">{{ model.no }}</detail-list-item>
      <detail-list-item term="设备旧号">{{ model.zbh }}</detail-list-item>
      <detail-list-item term="型号">{{ model.model }}</detail-list-item>
      <detail-list-item term="设备名称">{{ model.name }}</detail-list-item>
    </detail-list>
    <title-divider title="子设备" width="90px"></title-divider>
    <div class="table-operator" style="margin-bottom:8px;">
      <a-button style="margin-left:8px;" type="primary" @click="handleSbSelect">
        <a-icon type="plus"/>
        添加子设备
      </a-button>
    </div>
    <a-table
      :data-source="data"
      :columns="columns"
      size="default"
      :scroll="{x: 1500 }"
      tableLayout="auto"
      rowKey="id">
      <span slot="action" slot-scope="record">
        <template>
          <a-popconfirm v-if="$auth('sb-infos-del')" title="是否要取消关联？" @confirm="batchCancel(record.id)">
            <a>取消</a>
          </a-popconfirm>
        </template>
      </span>
    </a-table>
    <sb-info-select-modal :type="'checkbox'" ref="sbInfoSelectModal" @selected="handleSbSelected"/>
  </div>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { updateSbInfoChildBatch, updateSbInfoChildCancel, querySbInfo, deleteSbInfos, fetchSbInfo, exportSbInfo } from '@/api/sb/info'
import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'
const DetailListItem = DetailList.Item

export default {
  name: 'DetailSbCheck',
  components: {
    DetailList,
    DetailListItem,
    SbInfoSelectModal
  },
  props: {
    /**
     * 检查类型： 1-点检 2-巡检
     */
    checkType: {
      type: Number,
      default: 2
    }
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      model: {
        'id': null,
        'modelId': null,
        'no': null,
        'zbh': null,
        'name': null,
        'nameModel': null,
        'unit': null,
        'level': null,
        'useType': null
      },
      modalTitle: null,
      visible: false,
      typeMap: {},
      actionTypeMap: {},
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          customRender: (text, record, index) => {
            return index + 1
          }
        },
        {
          title: '设备名称',
          checked: true,
          width: 250,
          dataIndex: 'name'
        },
        {
          title: '设备编号',
          dataIndex: 'no',
          width: 150,
          checked: true
        },
        {
          title: '设备类型',
          checked: true,
          dataIndex: 'type',
          width: 150,
          customRender: (text, record, index) => {
            return record.typeName
          }
        },
        {
          title: '设备等级',
          checked: true,
          dataIndex: 'level',
          width: 150,
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.levelMap, text)
          }
        },
        {
          title: '购置日期',
          dataIndex: 'buyDate',
          width: 100,
          checked: true
        },
        {
          title: '投用日期',
          dataIndex: 'startDate',
          width: 100,
          checked: true
        },
        {
          title: '检定日期',
          dataIndex: 'checkDate',
          width: 150,
          checked: true
        },
        {
          title: '检定周期',
          dataIndex: 'checkPeriod',
          width: 100,
          checked: true,
          customRender: (text, record, index) => {
            if (record.nextCheckDate == null) {
              return '未知'
            } else {
              return text + '月'
            }
          }
        },
        {
          title: '检定有效期',
          dataIndex: 'nextCheckDate',
          width: 150,
          fixed: 'right',
          checked: true
        },
        {
          title: '检定截止',
          dataIndex: 'restDay',
          width: 100,
          fixed: 'right',
          checked: true,
          customRender: (text, record, index) => {
            if (record.nextCheckDate == null) {
              return '未知'
            } else {
              return this.BaseTool.Date.getCountBetween(new Date(), record.nextCheckDate, 1) + '天'
            }
          }
        },
        {
          title: '状态',
          checked: true,
          dataIndex: 'status',
          width: 100,
          fixed: 'right',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          key: 'action',
          checked: true,
          align: 'center',
          width: 100,
          fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      data: []
    }
  },
  created () {
    // 下拉框map
    this.depreciationTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_DEPRECIATIONTYPE)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_LEVEL)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_INFO_STATUS)
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.sourceTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBTYPE_SOURCETYPE)
    this.useTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_USE_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.modalTitle = '详情2'
      querySbInfo({ parentId: record.id }).then(res => {
        this.data = res.data
      })
    },
    handleOk () {
      querySbInfo({ parentId: this.model.id, type: this.checkType }).then(res => {
        this.data = res.data
      })
    },
    handleAdd () {
      const modal = this.$refs.baseModal
      modal.base(null, this.model.id)
    },
    handleView (record) {
      fetchSbInfo({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        res.data.partName = record.partName
        modal.base(res.data)
      })
    },
    handleEdit (record) {
      fetchSbInfo({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        modal.base(res.data)
      })
    },
    handleCopy (record) {
      const modal = this.$refs.baseModal
      const data = record
      data.id = null
      modal.base(data)
    },
    handleGenerate (record) {
      const modal = this.$refs.baseModalInsert
      modal.base(null, record.id)
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok')
    },
    batchDelete (id) {
      let ids = []
      if (this.BaseTool.String.isBlank(id)) {
        if (length === 0) {
          this.$message.info('请选择要删除的记录')
          return
        }
        ids = this.selectedRows.map(item => item.id)
      } else {
        ids = [id]
      }
      deleteSbInfos(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
      })
    },
    batchCancel (id) {
      if (this.BaseTool.String.isBlank(id)) {
        this.$message.info('请选择要取消的记录')
        return
      }
      updateSbInfoChildCancel({ id: id }).then(res => {
        this.$message.info('取消成功')
        this.handleOk()
      })
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      parameter.modelId = this.model.id
      exportSbInfo(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base()
    },
    handleSbSelected (keys, rows) {
      const data = []
      for (let i = 0; i < rows.length; i++) {
        data.push({ parentId: this.model.id, id: rows[i].id })
      }
      updateSbInfoChildBatch(data)
        .then((response) => {
          this.$message.info('绑定成功')
          this.handleOk()
        }).catch(() => {
          this.confirmLoading = false
        })
    }
  }
}
</script>
