<template>
  <div>
  <el-card class="box-card">
    <el-row :gutter="20">
      <el-col :span="9">
        <el-input clearable v-model="queryInfo.query" placeholder="请输入入库单号" prefix-icon="el-icon-search">
          <el-button slot="append" icon="el-icon-search" @click="getInStorehouseList"></el-button>
        </el-input>
      </el-col>
      <el-col :span="9">
        <el-button type="success" @click="check" icon = "el-icon-success">审核</el-button>
        <el-button type="danger" @click="cancel" icon = "el-icon-error">撤销审核</el-button>
        <el-button type="danger" @click="cancelInStorehouse" icon = "el-icon-error">撤销入库</el-button>

      </el-col>
    </el-row>
    <div class="main-content">
    <el-table :data="inStorehouseList.slice((queryInfo.pageNum-1)*queryInfo.pageSize,queryInfo.pageNum*queryInfo.pageSize)"
    style="width: 100%"
    stripe border fixed
    @selection-change="handleSelectionChange">
        <el-table-column
          fixed="left"
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column
          prop="inStorehouseId"
          label="入库单号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="locationId"
          label="货位编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="receivingId"
          label="接货单号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="createPerson"
          label="创建者"
          width="180">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="checkPerson"
          label="审批人"
          width="180">
        </el-table-column>
        <el-table-column
          prop="checkTime"
          label="审批时间"
          width="180">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          width="180">
        </el-table-column>
        <el-table-column
          prop="remarks"
          label="备注">
        </el-table-column>
      </el-table>
      <div class="pages">
     <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pageNum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
      </el-pagination>
    </div>
    </div>
    </el-card>

  </div>

</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      user: '',
      inStorehouseList: [],
      selectList: '',
      data: ''
    }
  },
  methods: {
    handleSizeChange (val) {
      this.queryInfo.pageSize = val
      this.getInStorehouseList()
    },

    handleCurrentChange (val) {
      this.queryInfo.pageNum = val
      this.getInStorehouseList()
    },

    handleSelectionChange (val) {
      this.selectList = val
    },

    async getInStorehouseList () {
      const { data: result } = await this.$http.get('/putIn/list', { params: this.queryInfo })
      if (result.status === 201) return this.$message.error('获取列表失败')
      this.inStorehouseList = result.data.rows
      this.total = result.data.total
    },

    check () {
      for (var i = 0; i < this.selectList.length; i++) {
        if (this.selectList[i].status === '待审核') {
          this.selectList[i].status = '已审核'
          this.selectList[i].checkPerson = this.user
          this.updataInStorehouse(this.selectList[i])
          this.$message.success('入库单' + this.selectList[i].inStorehouseId + '审核成功')
        } else if (this.selectList[i].status === '已审核') {
          this.$message.error('入库单' + this.selectList[i].inStorehouseId + '已审核')
        } else {
          this.$message.error('入库单' + this.selectList[i].inStorehouseId + '进行中')
        }
      }
    },

    cancel () {
      for (var i = 0; i < this.selectList.length; i++) {
        if (this.selectList[i].status === '已审核') {
          this.selectList[i].status = '待审核'
          this.updataInStorehouse(this.selectList[i])
          this.$message.success('入库单' + this.selectList[i].inStorehouseId + '撤销审核成功')
        } else if (this.selectList[i].status === '待审核') {
          this.$message.error('入库单' + this.selectList[i].inStorehouseId + '未审核')
        } else {
          this.$message.error('入库单' + this.selectList[i].inStorehouseId + '进行中')
        }
      }
    },

    async cancelInStorehouse () {
      for (var i = 0; i < this.selectList.length; i++) {
        if (this.selectList[i].status === '待审核') {
          this.deleteInStorehouse(this.selectList[i])
          this.$message.success('入库单' + this.selectList[i].inStorehouseId + this.data)
        } else {
          this.$message.error('入库单' + this.selectList[i].inStorehouseId + '进行中')
        }
      }
    },

    async updataInStorehouse (inStorehouse) {
      const { data: result } = await this.$http.put('/putIn/updataInStorehouse', inStorehouse)
      if (result.status !== 200) return this.$message.error('修改失败')
      this.getInStorehouseList()
    },

    async deleteInStorehouse (inStorehouse) {
      const { data: result } = await this.$http.delete(`/putIn/deleteInStorehouse/${inStorehouse.inStorehouseId}/${inStorehouse.receivingId}`)
      if (result.status !== 200) return this.$message.error('撤销失败')
      this.data = result.data
      this.getInStorehouseList()
    }

  },

  mounted () {
    this.getInStorehouseList()
    this.user = window.sessionStorage.getItem('token').substr(32)
  }
}
</script>

<style>

</style>
