<template>
  <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
    <a-row :gutter="48" slot="extra" v-show="visibleOther">
      <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" v-show="visibleOther"></title-divider>
    <detail-list title="" :col="3" v-show="visibleOther">
      <detail-list-item term="型号">{{ model.model }}</detail-list-item>
      <detail-list-item term="名称型号">{{ model.nameModel }}</detail-list-item>
      <detail-list-item term="设备名称">{{ model.name }}</detail-list-item>
      <detail-list-item term="设备类型">{{ model.typeName }}</detail-list-item>
      <detail-list-item term="自定义类型">{{ BaseTool.Object.getField(useTypeMap,model.useType) }}</detail-list-item>
      <detail-list-item term="设备等级">{{ BaseTool.Object.getField(levelMap,model.level) }}</detail-list-item>
      <detail-list-item term="生产商">{{ model.producerName }}</detail-list-item>
      <detail-list-item term="计量单位">{{ model.unit }}</detail-list-item>
      <detail-list-item term="设备原值">{{ BaseTool.Amount.formatter(model.value) }}</detail-list-item>
    </detail-list>
    <title-divider title="BOM信息" width="90px" v-show="visibleOther"></title-divider>
    <div class="table-operator" style="margin-bottom: 8px;" v-show="visibleOther">
<!--      <a-button type="primary" @click="handleAdd">
        <a-icon type="plus"/>
        单条添加
      </a-button>-->
      <a-button style="margin-left:8px;" type="primary" @click="handleSpareStoreSelect">
        <a-icon type="plus"/>
        批量添加
      </a-button>
<!--      <a-button style="margin-left:8px;" type="primary" @click="handleAddSpareInfo">
        <a-icon type="plus"/>
        添加备件
      </a-button>-->
      <a-button style="margin-left: 8px" type="primary" icon="download" @click="downTemplate">下载模板</a-button>
      <a-button style="margin-left: 8px" type="primary" icon="download" @click="doExport">导出</a-button>
      <a-button style="margin-left:8px;" type="primary" @click="doImport">
        <a-icon type="upload"/>
        导入
      </a-button>
    </div>
    <a-table
      :data-source="data"
      v-show="visibleOther"
      :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 ref="baseModal" @ok="handleOk"/>
    <spare-base-form ref="spareBaseModal" @ok="handleOk"/>
    <import-form-add ref="importModal" @ok="handleOk"></import-form-add>
    <spare-part-info-select-modal :type="'checkbox'" ref="spareSelectModal" @selected="handleSpareSelected"/>
    <spare-store-select-modal ref="spareStoreSelectModal" @selected="handleSpareStoreSelected"/>
  </a-card>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import { addSbModelBomBatch, selectSpareInfoListByModelId, deleteSbModelBoms, exportSbModelBom } from '@/api/sb/modelbom'
import SparePartInfoSelectModal from '@/views/sqarepartmanage/sparepartinfo/modules/SparePartInfoSelectModal'
import BaseForm from './BaseForm'
import ImportFormAdd from './ImportFormAdd'
import SpareBaseForm from '@/views/sqarepartmanage/sparepartinfo/modules/BaseForm'
import SpareStoreSelectModal from '@/views/store/sparestore/modules/SpareStoreSelectModalYY'
const DetailListItem = DetailList.Item

export default {
  name: 'SbModelDetail',
  components: {
    DetailList,
    DetailListItem,
    BaseForm,
    SparePartInfoSelectModal,
    ImportFormAdd,
    SpareBaseForm,
    SpareStoreSelectModal
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      model: {
        'id': null,
        'modelId': null,
        'no': null,
        'name': null,
        'nameModel': null,
        'unit': null,
        'level': null,
        'useType': null
      },
      modalTitle: null,
      visible: false,
      visibleOther: true,
      useTypeMap: {},
      levelMap: {},
      unitMap: {},
      periodTypeMap: {},
      // 表头
      columns: [
        /*{
          title: '备件编号',
          dataIndex: 'spareId'
        },*/
        {
          title: '备件名称',
          dataIndex: 'spareName'
        },
        {
          title: '备件编码',
          dataIndex: 'no'
        },
        /*{
          title: '自编号',
          dataIndex: 'zbh'
        },*/
        {
          title: '规格型号',
          dataIndex: 'ggxh'
        },
        /*{
          title: '更换周期',
          dataIndex: 'period',
          customRender: (text, record, index) => {
            return text + this.BaseTool.Table.getMapText(this.periodTypeMap, record.periodType)
          }
        },
        {
          title: '数量',
          dataIndex: 'num'
        },
        {
          title: '更换日期',
          dataIndex: 'checkDate'
        },
        {
          title: '下次更换日期',
          dataIndex: 'nextCheckDate'
        },
        {
          title: '助记码',
          dataIndex: 'zjm'
        },*/
        {
          title: '操作',
          key: 'action',
          checked: true,
          align: 'center',
          // 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_MODEL_RECOMMEND)
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.enableMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_STANDARD_TYPE)
    this.useTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_USE_TYPE)
    this.periodTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_PLAN_PERIOD_TYPE)
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.modalTitle = '详情'
      selectSpareInfoListByModelId({ id: record.id }).then(res => {
        this.data = res.data
      })
    },
    handleOk () {
      this.visibleOther = true
      selectSpareInfoListByModelId({ id: this.model.id }).then(res => {
        this.data = res.data
      })
    },
    handleAddSpareInfo () {
      this.visibleOther = false
      this.$refs.spareBaseModal.base('', this.model.id)
    },
    handleSpareSelect () {
      this.$refs.spareSelectModal.base({}, { yt: 1 })
    },
    handleSpareSelected (record, keys, rows) {
      const addData = []
      for (let i = 0; i < rows.length; i++) {
        addData.push({ sbId: this.model.id, modelId: this.model.id, spareId: rows[i].id, spareName: rows[i].name, num: 1, period: 500, periodType: 6 })
      }
      addSbModelBomBatch(addData)
        .then((response) => {
          this.$message.info('批量添加成功')
          this.handleOk()
        }).catch(() => {
          this.confirmLoading = false
        })
    },
    handleSpareStoreSelect () {
      this.$refs.spareStoreSelectModal.base()
    },
    handleSpareStoreSelected (record, keys, rows) {
      const addData = []
      for (let i = 0; i < rows.length; i++) {
        addData.push({ spareId: 111111, sbId: this.model.id, modelId: this.model.id, no: rows[i].no, spareName: rows[i].spareName, num: 1, ggxh: rows[i].ggxh })
      }
      addSbModelBomBatch(addData)
        .then((response) => {
          this.$message.info('批量添加成功')
          this.handleOk()
        }).catch(() => {
          this.confirmLoading = false
        })

      /*const { data } = this
      for (let i = 0; i < rows.length; i++) {
        let find = false
        for (let j = 0; j < data.length; j++) {
          if (rows[i].cbatch === data[j].cbatch) {
            find = true
            break
          }
        }
        if (!find) {
          const selectData = rows[i]
          selectData.num = 1
          data.push(selectData)
        }
      }*/
    },
    handleAdd () {
      const modal = this.$refs.baseModal
      modal.base({ modelId: null, sbId: this.model.id })
    },
    handleEdit (record) {
      console.log(record)
      const modal = this.$refs.baseModal
      record.modelId = this.model.id
      modal.base(record)
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok')
    },
    doImport () {
      this.$refs.importModal.base(this.model.id)
    },
    downTemplate () {
      const a = document.createElement('a')
      a.href = this.DictCache.getValueByCode(this.DictCache.TYPE.FILE_DOWNLOAD_TEMPLATE, 'SB_MODEL_BOM_DOWNLOAD_TEMPLATE')
      a.target = '_blank'
      a.click()
    },
    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()
      })
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      parameter.modelId = this.model.id
      exportSbModelBom(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    }
  }
}
</script>
