<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 v-if="3 === model.status" style="margin-left: 8px" type="danger" @click="adjustCheckPlan()">出入库矫正</a-button>
          <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="2">
      <detail-list-item term="编码">{{ model.no }}</detail-list-item>
      <detail-list-item term="计划名称">{{ model.name }}</detail-list-item>
      <detail-list-item term="盘点仓库">{{ model.storeName }}</detail-list-item>
      <detail-list-item term="备件类型">{{ model.spareTypeName }}</detail-list-item>
      <detail-list-item term="计划开始时间">{{ model.startTime }}</detail-list-item>
      <detail-list-item term="计划结束时间">{{ model.endTime }}</detail-list-item>
      <detail-list-item term="是否启用"><badge :status="DictCache.COLOR.YES_NO[model.enable]" :text="enableMap[model.enable]"></badge></detail-list-item>
      <detail-list-item term="状态"><badge :status="DictCache.COLOR.STORE_CHECK_PLAN_STATUS[model.status]" :text="statusMap[model.status]"></badge></detail-list-item>
      <detail-list-item term="更新时间">{{ model.updateTime }}</detail-list-item>
    </detail-list>
    <detail-list title="" :col="1">
      <detail-list-item term="备注">{{ model.remark }}</detail-list-item>
    </detail-list>
    <title-divider title="盘点明细" width="90px"></title-divider>
    <div class="table-operator">
       <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 @click="handleView(record)">查看</a>
          <a-divider type="vertical" />
          <a @click="handleFinish(record)">填报</a>
        </template>
      </span>
    </a-table>
    <base-form ref="baseModal" :check-type="checkType" @ok="handleOk"/>
    <detail ref="detailModal"/>
  </div>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import BaseForm from './BaseForm'
import Detail from './Detail'
import { executeJob, exportCheckJob, fetchCheckJob, queryCheckJob } from '@/api/store/storecheckjob'
import { adjustCheckPlan, fetchCheckPlan } from '@/api/store/storecheckplan'
const DetailListItem = DetailList.Item

export default {
  name: 'StoreCheckJobTable2',
  components: {
    DetailList,
    DetailListItem,
    BaseForm,
    Detail
  },
  props: {
    /**
     * 检查类型： 1-点检 2-巡检
     */
    checkType: {
      type: Number,
      default: 2
    }
  },
  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,
      typeMap: {},
      statusMap: {},
      enableMap: {},
      actionTypeMap: {},
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${index + 1}`
          }
        },
        {
          title: '备件名称',
          dataIndex: 'spareName'
        },
        {
          title: '备件规格',
          dataIndex: 'ggxh'
        },
        {
          title: '备件编码',
          dataIndex: 'no'
        },
        {
          title: '待盘数量',
          dataIndex: 'checkNum'
        },
        {
          title: '实际数量',
          dataIndex: 'realNum'
        },
        {
          title: '盘盈盘亏',
          dataIndex: 'profit',
          customRender: (text) => {
            if (text === 0) {
              return '--'
            }
            if (text === 1) {
              return '盘亏'
            }
            if (text === 2) {
              return '盘盈'
            }
          }
        },
        {
          title: '差异数量',
          dataIndex: 'profitNum'
        },
        {
          title: '备件金额',
          dataIndex: 'price'
        },
        {
          title: '差异金额',
          dataIndex: 'profitPrice'
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          align: 'center',
          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.enableMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.YES_NO)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.STORE_CHECK_PLAN_STATUS)
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.modalTitle = '详情'
      queryCheckJob({ planId: record.id }).then(res => {
        this.data = res.data
      })
    },
    handleOk () {
      fetchCheckPlan({ id: this.model.id }).then(res => {
        this.model = res.data
      })
      queryCheckJob({ planId: this.model.id }).then(res => {
        this.data = res.data
      })
    },
    handleEdit (record) {
      fetchCheckJob({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        modal.base(res.data)
      })
    },
    handleFinish (record) {
      fetchCheckJob({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        modal.base(res.data)
      })
    },
    handleExecute (record) {
      executeJob({ id: record.id }).then(res => {
        this.handleOk()
      })
    },
    handleView (record) {
      const modal = this.$refs.detailModal
      modal.base(record)
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok')
    },
    adjustCheckPlan () {
      adjustCheckPlan({ id: this.model.id }).then(res => {
        this.$message.info('已补录出入库单据，请核对出入库明细。')
        fetchCheckPlan({ id: this.model.id }).then(res => {
          this.model = res.data
        })
      })
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      parameter.planId = this.model.id
      parameter.storeId = this.model.storeId
      exportCheckJob(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    }
  }
}
</script>
