<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 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" @click="doImport">
        <a-icon type="upload"/>
        新增导入
      </a-button>
      <a-button style="margin-left:8px;" type="primary" v-if="selectedRowKeys.length > 0" @click="batchDelete">
        <a-icon type="delete"/>
        删除
      </a-button>
      <a-form-item style="margin-left:8px;" label="维护等级">
        <a-select v-model="queryParam.level" placeholder="请选择">
          <a-select-option
            v-for="(label,value) in levelMap"
            :key="value"
            :label="label"
            @change="changeLevel"
            :value="parseInt(value)">{{ label }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <!--      <a-button style="margin-left: 8px" type="primary" icon="download" @click="doExport">导出</a-button>-->
    </div>
    <a-table
      :data-source="data"
      :columns="columns"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      tableLayout="auto"
      :scroll="{x: 1100, y: BaseTool.Constant.scrollY}"
      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 @click="handleCopy(record)">复制</a>
          <a-divider type="vertical" />
          <a @click="handleGenerate(record)">插入任务</a>
        </template>
      </span>
    </a-table>
    <base-form ref="baseModal" :check-type="checkType" @ok="handleOk"/>
    <base-form-insert ref="baseModalInsert" @ok="handleOk"/>
    <detail ref="detailModal"/>
    <sb-info-select-modal :type="'radio'" ref="sbInfoSelectModal" @selected="handleSbSelected"/>
    <import-form-add ref="importModal" @ok="handleOk"/>
  </div>
</template>

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

export default {
  name: 'DetailSbCheck',
  components: {
    DetailList,
    DetailListItem,
    BaseForm,
    Detail,
    SbInfoSelectModal,
    BaseFormInsert,
    ImportFormAdd
  },
  props: {
    /**
     * 检查类型： 1-点检 2-巡检
     */
    checkType: {
      type: Number,
      default: 2
    }
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      queryParam: {
      },
      levelMap: {},
      model: {
        'id': null,
        'modelId': null,
        'no': null,
        'zbh': null,
        'name': null,
        'nameModel': null,
        'unit': null,
        'level': null,
        'useType': null
      },
      modalTitle: null,
      visible: false,
      typeMap: {},
      selectedRowKeys: [], // Check here to configure the default column
      checkUserTypeMap: {},
      actionTypeMap: {},
      // 表头
      columns: [
        {
          title: '标准编码',
          dataIndex: 'no',
          checked: true,
          width: '120px'
        },
        {
          title: '维护部位',
          width: '100px',
          checked: true,
          dataIndex: 'partName'
        },
        {
          title: '维护等级',
          dataIndex: 'level',
          width: '100px',
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.levelMap, text)
          }
        },
        {
          title: '标准内容',
          dataIndex: 'requirement',
          checked: true,
          width: '250px',
          customRender: (text, record, index) => {
            if (text != null) {
              if (text.length > 40) {
                return text.slice(0, 40)
              } else {
                return text
              }
            } else {
              return text
            }
          }
        },
        {
          title: '周期',
          dataIndex: 'periodType',
          width: '100px',
          checked: true,
          customRender: (text, record, index) => {
            return record.period + this.BaseTool.Table.getMapText(this.periodTypeMap, text)
          }
        },
        /*     {
          title: '动作类型',
          dataIndex: 'actionType',
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.actionTypeMap, text)
          }
        }, */
        {
          title: '标准工时',
          width: '100px',
          checked: true,
          dataIndex: 'standardHours'
        },
        {
          title: '上次执行日期',
          dataIndex: 'lastDate',
          checked: true,
          width: '200px'
        },
        {
          title: '预计下次执行日期',
          dataIndex: 'nextDate',
          checked: true,
          width: '200px'
        },
        {
          title: '操作',
          key: 'action',
          checked: true,
          align: 'center',
          width: '250px',
          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)
    this.checkUserTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_USER_TYPE)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.CHECK_PLAN_LEVEL)
  },
  methods: {
    onSelectChange (selectedRowKeys, selectedRows) {
      console.log('selectedRowKeys changed: ', selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    base (record) {
      this.visible = true
      this.model = record
      this.modalTitle = '详情2'
      queryCheckStandard({ sbId: record.id, type: this.checkType }).then(res => {
        this.data = res.data
      })
    },
    changeLevel (value) {
      queryCheckStandard({ sbId: this.model.id, level: value, type: this.checkType }).then(res => {
        this.data = res.data
      })
    },
    handleOk () {
      queryCheckStandard({ sbId: this.model.id, type: this.checkType }).then(res => {
        this.data = res.data
      })
    },
    handleAdd () {
      const modal = this.$refs.baseModal
      modal.base(null, this.model.id, this.model.no)
    },
    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
        res.data.sbNo = this.model.no
        modal.base(res.data, this.model.id)
      })
    },
    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.selectedRowKeys = []
      this.$emit('ok')
    },
    batchDelete (id) {
      let ids = []
      const length = this.selectedRowKeys.length
      if (length === 0) {
        this.$message.info('请选择记录')
        return
      }
      ids = this.selectedRowKeys
      console.log(this.selectedRowKeys[0])
      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)
      })
    },
    doImport () {
      this.$refs.importModal.base(this.model.no)
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base()
    },
    handleSbSelected (keys, rows) {
      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>
