<template>
  <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
    <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="id">{{ model.id }}</detail-list-item>
      <detail-list-item term="集团统一编码">{{ model.uniteNo }}</detail-list-item>
      <detail-list-item term="自编号">{{ model.zbh }}</detail-list-item>
      <detail-list-item term="备件编码">{{ model.no }}</detail-list-item>
      <detail-list-item term="备件名称">{{ model.name }}</detail-list-item>
      <detail-list-item term="名称编码">{{ model.nameNo }}</detail-list-item>
      <detail-list-item term="规格型号">{{ model.ggxh }}</detail-list-item>
      <detail-list-item term="规格编码">{{ model.ggNo }}</detail-list-item>
      <detail-list-item term="备件类别">{{ model.typeName }}</detail-list-item>
      <detail-list-item term="备件等级">{{ BaseTool.Object.getField(levelMap,model.level) }}</detail-list-item>
      <detail-list-item term="备件价值">{{ BaseTool.Amount.formatter(model.initialValue) }}</detail-list-item>
      <detail-list-item term="原厂编号">{{ model.initNo }}</detail-list-item>
      <detail-list-item term="计量单位">{{ model.unit }}</detail-list-item>
      <detail-list-item term="包装单位">{{ model.unitBz }}</detail-list-item>
      <detail-list-item term="英文名称">{{ model.englishName }}</detail-list-item>
      <detail-list-item term="助记码">{{ model.zjm }}</detail-list-item>
      <detail-list-item term="产地">{{ BaseTool.Object.getField(cdMap,model.cd) }}</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>
    </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="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-spare ref="baseModal" @ok="handleOk"/>
    <sb-info-select-modal :type="'checkbox'" ref="sbInfoSelectModal" @selected="handleSbSelected"/>
  </a-card>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { addSbModelBomBatch, deleteSbModelBoms, selectSbModelListBySpareId } from '@/api/sb/modelbom'
import BaseFormSpare from '@/views/sb/modelbom/modules/BaseFormSpare'
import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'
const DetailListItem = DetailList.Item

export default {
  name: 'SparePartInfoDetail',
  components: {
    DetailList,
    DetailListItem,
    SbInfoSelectModal,
    BaseFormSpare
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      visible: false,
      // 下拉框map
      levelMap: {},
      unitMap: {},
      cdMap: {},
      periodTypeMap: {},
      ytMap: {},
      model: {
        'no': null,
        'name': null,
        'ggxh': null,
        'typeId': null,
        'level': null,
        'initialValue': null,
        'producerId': null,
        'unit': null,
        'unitBz': null,
        'currentStock': null,
        'maxStock': null,
        'qrCode': null,
        'minStock': null,
        'remark': null,
        'createdUserId': null,
        'updateUserId': null,
        'updateTime': null,
        'createdUserName': null,
        'updateUserName': null,
        'producerName': null,
        'typeName': null
      },
      // 表头
      columns: [
        {
          title: '型号',
          dataIndex: 'model'
        },
        {
          title: '名称',
          dataIndex: 'name'
        },
        {
          title: '新号',
          dataIndex: 'no'
        },
        {
          title: '旧号',
          dataIndex: 'zbh'
        },
        {
          title: '更换周期',
          dataIndex: 'period',
          customRender: (text, record, index) => {
            return text + this.BaseTool.Table.getMapText(this.periodTypeMap, record.periodType)
          }
        },
        {
          title: '数量',
          dataIndex: 'num'
        },
        {
          title: '操作',
          key: 'action',
          checked: true,
          align: 'center',
          // fixed: 'right',
          scopedSlots: { customRender: 'action' }
        }
      ],
      data: []
    }
  },
  created () {
    // 下拉框map
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SPARE_PART_INFO_LEVEL)
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.cdMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PRODUCER_AREA)
    this.ytMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SPARE_USE_TYPE)
    this.periodTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_PLAN_PERIOD_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      this.modalTitle = '详情'
      this.model = record
      selectSbModelListBySpareId({ id: record.id }).then(res => {
        this.data = res.data
      })
    },
    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]
      }
      deleteSbModelBoms(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
      })
    },
    handleOk () {
      selectSbModelListBySpareId({ id: this.model.id }).then(res => {
        this.data = res.data
      })
    },
    handleSbSelect () {
      const modal = this.$refs.sbInfoSelectModal
      modal.base()
    },
    handleSbSelected (keys, rows) {
      const addData = []
      for (let i = 0; i < rows.length; i++) {
        addData.push({ spareId: this.model.id, sbId: rows[i].id, spareName: this.model.name, num: 1, period: 500, periodType: 6 })
      }
      addSbModelBomBatch(addData)
        .then((response) => {
          this.$message.info('批量添加成功')
          this.handleOk()
        }).catch(() => {
          this.confirmLoading = false
        })
    },
    handleEdit (record) {
      const modal = this.$refs.baseModal
      modal.base(record)
    },
    handleAdd () {
      const modal = this.$refs.baseModal
      modal.base({ spareId: this.model.id })
    }
  }
}
</script>
