<template>
  <el-dialog
    :title="!id ? '新增': display ? '查看' : '开始盘点'"
    width="70%"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
      <el-row class="my-row">
        <el-col :span="8">
          <el-form-item label="盘点编码" prop="inventoryCode">
            <el-input v-model="dataForm.inventoryCode" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding-left: 20px">
          <el-form-item label="盘点员" prop="checkerName">
            <el-input v-model="dataForm.checkerName" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding-left: 20px">
          <el-form-item label="复核员" prop="dcheckerName">
            <el-input v-model="dataForm.dcheckerName" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="my-row">
        <el-form-item label="备注说明">
            <el-input v-model="dataForm.notes" :disabled="true"></el-input>
          </el-form-item>
      </el-row>
      <el-row>
        <el-form :inline="true" :model="dataForm1" @keyup.enter.native="queryData()">
          <el-form-item label="仓库名称" prop="warehouseId">
            <el-select
              v-model="dataForm1.warehouseId"
              filterable
              remote
              :remote-method="remoteWarehouse"
              placeholder="请选择">
              <el-option
                v-for="item in optionsWh"
                :key="item.code"
                :label="item.value"
                :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="货架名称" prop="shelveId">
            <el-select
              v-model="dataForm1.shelveId"
              filterable
              remote
              :remote-method="remoteShelve"
              placeholder="请选择">
              <el-option
                v-for="item in optionsShelve"
                :key="item.code"
                :label="item.value"
                :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="queryData()">查询</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <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"
          label="物品名称">
        </el-table-column>
        <el-table-column
          prop="specifications"
          header-align="center"
          align="center"
          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"
          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-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {getInventoryDetail, getWarehouseList, getShelveList} from '@/api/warehouse'
  export default {
    name: 'inventory-detail',
    data () {
      return {
        visible: false,
        display: false,
        dataList: [],
        optionsWh: [],
        optionsShelve: [],
        id: 0,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        dataForm: {},
        dataForm1: {},
        dataRule: {
        }
      }
    },
    methods: {
      async init (id, display) {
        this.visible = true
        this.id = id || 0
        this.display = display
        this.dataForm = {}
        this.dataForm1 = {}
        // 获取详情
        this.queryData()
      },
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        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
            }
          } 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
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/biz-service/in-out-bound-ctl/inbound`),
              method: 'post',
              data: this.$http.adornData({
                'amount': this.dataForm.amount,
                'applicant': this.dataForm.applicant,
                'approver': this.dataForm.approver,
                'batchNumber': this.dataForm.batchNumber,
                'cnt': this.dataForm.cnt,
                'materialId': this.dataForm.materialId,
                'materialName': this.dataForm.materialName,
                'notes': this.dataForm.notes,
                'price': this.dataForm.price,
                'orgId': this.dataForm.orgId,
                'recordType': this.dataForm.recordType,
                'source': this.dataForm.source,
                'state': this.dataForm.state,
                'templateItemPlanId': this.dataForm.templateItemPlanId,
                'usage': this.dataForm.usage,
                'userId': this.dataForm.userId,
                'creatorId': this.$store.state.user.id
              })
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 仓库列表
      async remoteWarehouse (query) {
        let warehouseName = query || null
        await getWarehouseList({'warehouseName': warehouseName}).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm1 = {}
            this.optionsWh = []
            data.data.records.forEach((item) => {
              this.optionsWh.push({
                code: item.warehouseId,
                value: item.warehouseName
              })
            })
          }
        })
      },
      // 货架列表
      async remoteShelve (query) {
        let name = query || null
        let warehouseId = this.dataForm1.warehouseId || null
        await getShelveList({'name': name, 'warehouseId': warehouseId}).then(({data}) => {
          if (data && data.code === '200') {
            this.optionsShelve = []
            data.data.records.forEach((item) => {
              this.optionsShelve.push({
                code: item.shelveId,
                value: item.name
              })
            })
          }
        })
      }
    }
  }
</script>

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