<template>
  <a-modal
    :title="modalTitle"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    class="ant-modal2"
    @cancel="handleCancel"
  >
    <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.sbNo }}</detail-list-item>
      <detail-list-item term="设备名称">{{ model.sbName }}</detail-list-item>
      <detail-list-item term="预留维修时间">{{ model.limitHours }}</detail-list-item>
      <detail-list-item term="使用位置">{{ model.sbCph }}</detail-list-item>
      <detail-list-item term="报修时间">{{ model.applyTime }}</detail-list-item>
      <detail-list-item term="维修结束时间">{{ model.repairEndTime }}</detail-list-item>
    </detail-list>
    <title-divider title="维修报告" width="180px"></title-divider>
    <div class="table-operator" style="margin-bottom:8px;">
    </div>
    <a-table
      :data-source="data"
      :columns="columns"
      tableLayout="auto"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :scroll="{x: 1500 }"
      rowKey="id">
      <span slot="action" slot-scope="record">
        <template>
          <a v-if="$auth('sb-infos-edit')" @click="handleView(record)">查看</a>
        </template>
      </span>
    </a-table>
    <base-form ref="baseModal" @ok="handleOk"/>
    <detail ref="detailModal" @ok="handleOk"/>
  </a-modal>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { queryRepairReason, deleteRepairReasons, fetchRepairReason, exportRepairReason } from '@/api/repair/repair-reason'
import BaseForm from './BaseForm'
import Detail from './Detail'
import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'
const DetailListItem = DetailList.Item

export default {
  name: 'DetailRepairReason',
  components: {
    DetailList,
    DetailListItem,
    BaseForm,
    Detail,
    SbInfoSelectModal
  },
  props: {
  },
  data () {
    return {
      status: null,
      selectedRowKeys: [], // Check here to configure the default column
      optionAlertShow: false,
      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,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${index + 1}`
          }
        },
        {
          title: '编号',
          dataIndex: 'no'
        },
        {
          title: '分析时间',
          dataIndex: 'analyzeTime',
          width: '200px'
        },
        {
          title: '故障部位',
          dataIndex: 'sbPartName',
          ellipsis: true,
          width: '150px'
        },
        {
          title: '故障现象',
          dataIndex: 'problemDesc',
          ellipsis: true,
          width: '200px'
        },
        {
          title: '处理过程',
          dataIndex: 'checkProcess',
          ellipsis: true,
          width: '200px'
        },
        {
          title: '改进措施类别',
          dataIndex: 'type',
          ellipsis: true,
          width: '200px',
          customRender: (text, record, index) => {
            return this.BaseTool.Object.getField(this.typeMap, text)
          }
        },
        {
          title: '改进内容',
          dataIndex: 'changeRepairPlanContent',
          ellipsis: true,
          width: '200px'
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      typeMap: {},
      data: []
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_REASON_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.modalTitle = '历史故障维修报告'
      queryRepairReason({ repairId: this.model.id }).then(res => {
        this.data = res.data
      })
    },
    handleOk () {
      queryRepairReason({ repairId: this.model.id }).then(res => {
        this.data = res.data
      })
    },
    handleAdd () {
      const modal = this.$refs.baseModal
      modal.base(null, this.model.id)
    },
    handleView (record) {
      fetchRepairReason({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        res.data.partName = record.partName
        modal.base(res.data)
      })
    },
    handleEdit (record) {
      fetchRepairReason({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        modal.base(res.data)
      })
    },
    handleGenerate (record) {
      const modal = this.$refs.baseModalInsert
      modal.base(null, record.id)
    },
    handleCancel () {
      this.visible = false
      this.selectedRowKeys = []
      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]
      }
      deleteRepairReasons(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
      })
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      parameter.modelId = this.model.id
      exportRepairReason(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base()
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    }
  }
}
</script>
