<!-- 盘点 -->
<template>
  <div class="stock-order">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
      <el-form-item label="盘点日期">
        <el-date-picker
          v-model="dataForm.date"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryData()">查询</el-button>
        <el-button v-if="isAuth('wh:inventory:save')" @click="inventoryAdd()"  type="primary">新增盘点</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="inventoryCode"
        header-align="center"
        align="center"
        min-width="140"
        :show-tooltip-when-overflow="true"
        label="编码">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        min-width="140"
        label="开始日期">
      </el-table-column>
      <el-table-column
        prop="-"
        header-align="center"
        align="center"
        min-width="140"
        label="结束日期">
      </el-table-column>
      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        :formatter="formatState"
        label="状态">
      </el-table-column>
      <el-table-column
        prop="checkerName"
        header-align="center"
        align="center"
        label="盘点员">
      </el-table-column>
      <el-table-column
        prop="dcheckerName"
        header-align="center"
        align="center"
        label="复核员">
      </el-table-column>
      <el-table-column
        prop="notes"
        header-align="center"
        align="center"
        width="120"
        :show-tooltip-when-overflow="true"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('wh:inventory:info') && scope.row.state === '1'" type="text" size="small" @click="addOrUpdateHandle(scope.row.inventoryId, true)">查看</el-button>
          <el-button v-if="isAuth('wh:inventory:start') && scope.row.state === '0'" type="text" size="small" @click="addOrUpdateHandle(scope.row.inventoryId, false)">开始盘点</el-button>
        </template>
      </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>
    <!--    入库-->
    <inventory-add v-if="inventoryAddVisible" ref="inventoryAdd" @refreshDataList="getDataList"></inventory-add>
    <inventory-detail v-if="inventoryDetailVisible" ref="inventoryDetail" @refreshDataList="getDataList"></inventory-detail>
  </div>
</template>

<script>
  import inventoryAdd from './inventory-add'
  import inventoryDetail from './inventory-detail'
  import {getInventoryList} from '@/api/warehouse'
  export default {
    name: 'stock-order',
    components: {inventoryAdd, inventoryDetail},
    data () {
      return {
        inventoryAddVisible: false,
        inventoryDetailVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        // 状态
        optionsState: [
          {
            code: '0',
            value: '正在进行中'
          },
          {
            code: '1',
            value: '完成'
          }
        ]
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'beginTime': this.dataForm.date ? this.dataForm.date[0] : null,
          'endTime': this.dataForm.date ? this.dataForm.date[1] : null
        }
        getInventoryList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } 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
      },
      // 盘点详情
      addOrUpdateHandle (id, display) {
        this.inventoryDetailVisible = true
        this.$nextTick(() => {
          this.$refs.inventoryDetail.init(id, display)
        })
      },
      // 新增盘点
      inventoryAdd () {
        this.inventoryAddVisible = true
        this.$nextTick(() => {
          this.$refs.inventoryAdd.init()
        })
      },
      // 转换属性“状态”
      formatState (row) {
        if (!row.state) return ''
        const item1 = this.optionsState.find((item) => item.code === row.state.toString())
        return item1 ? item1.value : ''
      }
    }
  }
</script>

<style scoped>

</style>
