<!-- 出入库记录 -->
<template>
  <div class="stock-order">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
      <el-form-item label="出入库类型">
        <el-select v-model="dataForm.recordType" placeholder="请选择">
          <el-option
            v-for="item in recordTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="编码">
        <el-input v-model="dataForm.recordCode" placeholder="编码" clearable></el-input>
      </el-form-item>
      <el-form-item label="申请人">
        <user-component v-model="dataForm.userId"/>
      </el-form-item>
      <el-form-item label="入库日期">
        <el-date-picker
          v-model="dataForm.date"
          value-format="yyyy-MM-dd"
          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="false" @click="exportData()" 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="recordCode"
        header-align="center"
        align="center"
        min-width="160"
        :show-tooltip-when-overflow="true"
        label="编码">
      </el-table-column>
      <el-table-column
        prop="materialName"
        header-align="center"
        align="center"
        min-width="160"
        :show-tooltip-when-overflow="true"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="recordType"
        header-align="center"
        align="center"
        :formatter="formatRecordType"
        min-width="100"
        label="出入库类型">
      </el-table-column>
      <el-table-column
        prop="specifications"
        header-align="center"
        align="center"
        min-width="160"
        :show-tooltip-when-overflow="true"
        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="price"
        header-align="center"
        align="center"
        label="单价">
      </el-table-column>
      <el-table-column
        prop="amount"
        header-align="center"
        align="center"
        label="金额">
      </el-table-column>
      <el-table-column
        prop="batchNumber"
        header-align="center"
        align="center"
        min-width="120"
        :show-tooltip-when-overflow="true"
        label="批次号">
      </el-table-column>
      <el-table-column
        prop="source"
        header-align="center"
        align="center"
        min-width="160"
        :show-tooltip-when-overflow="true"
        label="来源">
      </el-table-column>
      <el-table-column
        prop="recordTime"
        header-align="center"
        align="center"
        min-width="160"
        label="出入库时间">
      </el-table-column>
      <el-table-column
        prop="applicant"
        header-align="center"
        align="center"
        label="申请人">
      </el-table-column>
      <el-table-column
        prop="approver"
        header-align="center"
        align="center"
        label="操作人">
      </el-table-column>
      <el-table-column
        prop="notes"
        header-align="center"
        align="center"
        min-width="160"
        :show-overflow-tooltip="true"
        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>
  </div>
</template>

<script>
  import UserComponent from '../common/user-component'
  import { getRecordList } from '@/api/warehouse'
  export default {
    name: 'stock-order',
    components: {UserComponent},
    created () {
      this.queryData()
    },
    data () {
      return {
        inboundVisible: false,
        dataForm: {
          number: '',
          name: '',
          date: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        recordTypeOptions: [
          {
            value: null,
            label: '请选择'
          },
          {
            value: '0',
            label: '入库'
          },
          {
            value: '1',
            label: '出库'
          }
        ]
      }
    },
    methods: {
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'beginTime': this.dataForm.date[0],
          'endTime': this.dataForm.date[1],
          'recordType': this.dataForm.recordType ? this.dataForm.recordType : null,
          'recordCode': this.dataForm.recordCode ? this.dataForm.recordCode : null,
          'userId': this.dataForm.userId ? this.dataForm.userId : null
        }
        getRecordList(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
      },
      // 导出
      exportData () {
        // todo
      },
      // 申请单类型
      formatRecordType (row) {
        if (!row.recordType) return ''
        if (row.recordType === '0') return '入库'
        if (row.recordType === '1') return '出库'
        return ''
      }
    }
  }
</script>

<style scoped>

</style>
