<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-item term="检定日期">{{ model.checkDate }}</detail-list-item>
      <detail-list-item term="检定周期">{{ model.checkPeriod }}个月</detail-list-item>
      <detail-list-item term="检定有效期">{{ model.nextCheckDate }}</detail-list-item>
      <detail-list-item term="预警天数">{{ model.seatNumber }}天</detail-list-item>
    </detail-list>
    <title-divider title="检定信息" width="90px"></title-divider>
    <div class="table-operator">
      <a-button type="primary" @click="handleAdd">
        <a-icon type="plus"/>
        添加
      </a-button>
<!--      <a-button style="margin-left:8px;" type="primary" @click="handleSbSelect">
        <a-icon type="plus"/>
        复制
      </a-button>-->
      <!--      <a-button style="margin-left: 8px" type="primary" icon="download" @click="doExport">导出</a-button>-->
    </div>
    <a-table
      :data-source="data"
      :columns="columns"
      tableLayout="auto"
      rowKey="id">
      <span slot="action" slot-scope="record">
        <template>
          <a v-if="$auth('sb-infos-edit')" @click="handleView(record)">查看</a>
          <a-divider type="vertical" />
          <a v-if="$auth('sb-infos-edit')" @click="handleEdit(record)">修改</a>
          <a-divider type="vertical" />
          <a-popconfirm v-if="$auth('sb-infos-del')" title="是否要删除该条数据？" @confirm="batchDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </template>
      </span>
    </a-table>
    <base-form ref="baseModal" @ok="handleOk"/>
    <base-form-insert ref="baseModalInsert" @ok="handleOk"/>
    <detail ref="detailModal"/>
    <sb-info-select-modal :type="'radio'" ref="sbInfoSelectModal" @selected="handleSbSelectd"/>

  </div>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import {
  queryCheckStandard,
  deleteCheckStandards,
  exportCheckStandard,
  fetchCheckStandard
} from '@/api/sb/measurelog'
import BaseForm from './BaseForm'
import BaseFormInsert from './BaseFormInsert'
import Detail from './Detail'
import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'
import { fetchSbInfo } from '@/api/sb/info'
const DetailListItem = DetailList.Item

export default {
  name: 'DetailSbMeasure',
  components: {
    DetailList,
    DetailListItem,
    BaseForm,
    Detail,
    SbInfoSelectModal,
    BaseFormInsert
  },
  props: {
  },
  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',
          customRender: (text, record, index) => {
            return index + 1
          }
        },
        {
          title: '检定单号',
          dataIndex: 'no'
        },
        {
          title: '检定人',
          dataIndex: 'name'
        },
        {
          title: '检定日期',
          dataIndex: 'lastDate'
        },
        {
          title: '创建日期',
          dataIndex: 'createdTime'
        },
        {
          title: '操作',
          key: 'action',
          checked: true,
          align: 'center',
          // fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      data: []
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_STANDARD_TYPE)
    this.periodTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_PLAN_PERIOD_TYPE)
    this.actionTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_PLAN_ACTION_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.modalTitle = '详情2'
      queryCheckStandard({ sbId: record.id }).then(res => {
        this.data = res.data
      })
    },
    handleOk () {
      queryCheckStandard({ sbId: this.model.id }).then(res => {
        this.data = res.data
      })
      fetchSbInfo({ id: this.model.id }).then(res => {
        this.model = res.data
      })
    },
    handleAdd () {
      const modal = this.$refs.baseModal
      modal.base(null, this.model.id)
    },
    handleView (record) {
      fetchCheckStandard({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        res.data.partName = record.partName
        modal.base(res.data)
      })
    },
    handleEdit (record) {
      fetchCheckStandard({ 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]
      }
      deleteCheckStandards(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
      })
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      parameter.modelId = this.model.id
      exportCheckStandard(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base()
    },
    handleSbSelectd (keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      // 日期处理
      copy({ sbId: this.model.id, copySbId: row.id })
        .then((response) => {
          this.$message.info(response.data)
          this.handleOk()
        }).catch(() => {
          this.confirmLoading = false
        })
    }
  }
}
</script>
