<template>
  <!-- <el-dialog
    title="查看"
    width="70%"
    :close-on-click-modal="false"
    :visible.sync="visible"> -->
  <div>
    <div class="my-title">查看</div>
    <div style="margin-left: 20px;margin-right: 20px">
      <!-- 工作流 -->
      <div v-show="flowVisible">
        <el-steps :active="flowVisible?dataList[0].workFlowBusinessExt.workFlowProcessStepList.length + 2:0" align-center style="margin-bottom: 20px">
          <template v-for="(item, i) in stepList">
            <el-step :icon="item.icon" :title="item.title" :description="item.description"></el-step>
          </template>
        </el-steps>
        <el-collapse style="margin-bottom: 20px">
          <el-collapse-item>
            <template slot="title">
              <span style="color: red">审批日志（展开查看更多）：</span>
            </template>
            <template v-for="(item, i) in logList">
              <div>{{++i}}：{{item.approverName}}  {{item.createTime}}  {{item.approvalValue}}</div>
            </template>
          </el-collapse-item>
        </el-collapse>
      </div>
      <div v-show="isFlow">
        <approve-component ref="approve" @approveFinished="approveFinished"/>
      </div>
      <e-desc title="基本信息" column="3">
        <e-desc-item label="盘点编码">{{dataForm.inventoryCode}}</e-desc-item>
        <e-desc-item label="盘点员">{{dataForm.checkerName}}</e-desc-item>
        <e-desc-item label="复核员">{{dataForm.dcheckerName}}</e-desc-item>
        <e-desc-item label="备注说明" span="3">{{dataForm.notes}}</e-desc-item>
      </e-desc>
      <el-row style="margin-top: 20px">
        <el-form :inline="true" :model="dataForm1" @keyup.enter.native="queryData()">
          <el-form-item label="仓库名称" prop="warehouseId">
            <warehouse-component v-model="dataForm1.warehouseId" :warehouse-id="dataForm1.warehouseId" @warehouseSelected="warehouseChanged"/>
          </el-form-item>
          <el-form-item label="货架名称" prop="shelveId">
            <shelve-component ref="shelveCom" v-model="dataForm1.shelveId" :warehouse-id="dataForm1.warehouseId" :shelve-id="dataForm1.shelveId"/>
          </el-form-item>
          <el-form-item>
            <el-button @click="queryData()">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center">
          </el-table-column>
          <el-table-column
            prop="warehouseName"
            header-align="center"
            align="center"
            label="仓库名称">
          </el-table-column>
          <el-table-column
            prop="materialName"
            header-align="center"
            align="center"
            min-width="160"
            label="物品名称">
          </el-table-column>
          <el-table-column
            prop="specifications"
            header-align="center"
            align="center"
            min-width="160"
            label="规格">
          </el-table-column>
          <el-table-column
            prop="unitName"
            header-align="center"
            align="center"
            label="单位">
          </el-table-column>
          <el-table-column
            prop="cnt"
            header-align="center"
            align="center"
            label="仓库库存数量">
          </el-table-column>
          <el-table-column
            prop="inventoryCnt"
            header-align="center"
            align="center"
            label="数量">
          </el-table-column>
          <el-table-column
            prop="inventoryState"
            header-align="center"
            align="center"
            :formatter="formatState"
            label="盘库状态">
          </el-table-column>
          <el-table-column
            prop="materialState"
            header-align="center"
            align="center"
            :formatter="formatMs"
            label="物品状态">
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-row>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">返回</el-button>
    </span>
  </div>
  <!-- </el-dialog> -->
</template>

<script>
  import EDesc from '../common/e-desc'
  import EDescItem from '../common/e-desc-item'
  import uploadComponent from '../common/upload-component'
  import { dealStepData, dealStepLogs } from '@/api/util'
  import ApproveComponent from '../common/approve-component'
  import {getInventoryDetail} from '@/api/warehouse'
  import WarehouseComponent from '../common/warehouse-component'
  import ShelveComponent from '../common/shelve-component'
  export default {
    name: 'inventory-detail',
    components: {
      EDesc, EDescItem, uploadComponent, ApproveComponent, ShelveComponent, WarehouseComponent
    },
    data () {
      return {
        visible: false,
        flowVisible: false,
        isFlow: false,
        id: 0,
        dataForm: {},
        dataForm1: {},
        dataList: [],
        fileList: [],
        fileList1: [],
        stepList: [],
        logList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsMs: [
          {
            code: '0',
            value: '正常'
          },
          {
            code: '1',
            value: '盘点中'
          }
        ],
        optionsState: [
          {
            code: '0',
            value: '正常'
          },
          {
            code: '-1',
            value: '盘亏'
          },
          {
            code: '1',
            value: '盘盈'
          }
        ]
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      async init (id, businessType) {
        this.visible = true
        this.isFlow = !!(businessType && businessType !== '')
        this.id = id || 0
        this.dataForm = {}
        this.fileList = []
        this.fileList1 = []
        this.dataForm1 = {}
        // 获取详情
        this.queryData(businessType)
      },
      // 查询
      queryData (businessType) {
        this.pageIndex = 1
        this.stepList = []
        this.logList = []
        this.getDataList(businessType)
      },
      // 获取数据列表
      getDataList (businessType) {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'inventoryId': this.id,
          'warehouseId': this.dataForm1.warehouseId ? this.dataForm1.warehouseId : null,
          'shelveId': this.dataForm1.shelveId ? this.dataForm1.shelveId : null
        }
        getInventoryDetail(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
            if (this.dataList.length > 0) {
              let item = this.dataList[0]
              this.dataForm.inventoryCode = item.inventoryCode
              this.dataForm.checkerName = item.checkerName
              this.dataForm.dcheckerName = item.dcheckerName
              this.dataForm.notes = item.notes
              // 流程图展示
              if (item.workFlowBusinessExt) {
                this.flowVisible = true
                dealStepData(this.dataList[0].workFlowBusinessExt.workFlowProcessStepList, this.stepList)
                dealStepLogs(this.dataList[0].workFlowBusinessExt.processLogList, this.logList)
              }
              // 初始化审批Form
              this.showApproveForm(businessType, this.id)
            }
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 初始化审批Form
      showApproveForm (businessType, businessId) {
        if (this.isFlow) {
          this.$nextTick(() => {
            this.$refs.approve.init(businessType, businessId)
          })
        }
      },
      // 物品状态转换
      formatMs (row) {
        for (let i = 0; i < this.optionsMs.length; i++) {
          if (this.optionsMs[i].code === row.materialState) {
            return this.optionsMs[i].value
          }
        }
      },
      // 盘库状态转换
      formatState (row) {
        for (let i = 0; i < this.optionsState.length; i++) {
          if (this.optionsState[i].code === row.inventoryState) {
            return this.optionsState[i].value
          }
        }
      },
      // 审批完成
      approveFinished () {
        this.onChose()
        this.$emit('approveFinished')
      },
      warehouseChanged (item) {
        this.dataForm1.warehouseId = item
        this.$refs.shelveCom.init(item)
      }
    }
  }
</script>

<style scoped>
.my-line{
  border-bottom: 1px solid #c0c4cc;
  margin-bottom: 10px;
}
.title{
  padding: 10px 0 ;
}
</style>
