<!-- 出入库管理 -->
<template>
  <div class="stock-order">
    <template v-if="!inboundVisible && !outboundVisible && !detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
        <el-form-item label="出入库类型">
          <el-select
            v-model="dataForm.recordType"
            @change="recordTypeChanged"
            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-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-form-item>
        <el-form-item>
          <el-button v-if="isAuth('wh:in:inbound')" type="primary" @click="inBound(0, false)">入库申请</el-button>
          <el-button v-if="isAuth('wh:on:outbound')" type="primary" @click="outBound()">出库申请</el-button>
  <!--        <el-button v-if="isAuth('wh:in-out-bound-ctl:outbound')" type="primary" @click="outBound()">模板出库</el-button>-->
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50">
        </el-table-column>
        <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="recordType"
          header-align="center"
          align="center"
          :formatter="formatRecordType"
          width="100"
          label="申请单类别">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          :formatter="formatState"
          width="100"
          label="申请状态">
        </el-table-column>
        <el-table-column
          prop="approver"
          header-align="center"
          align="center"
          width="120"
          :show-tooltip-when-overflow="true"
          label="当前审批员">
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          min-width="160"
          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"
          min-width="140"
          :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:inon:info')" type="text" size="small" @click="detailHandle(scope.row.id)">查看</el-button>
            <el-button v-if="false" type="text" size="small" @click="exportItem(scope.row.id)">导出</el-button>
            <el-button v-if="isAuth('wh:in:editor') && (Number(scope.row.state) === 0 || Number(scope.row.state) === 4)" type="text" size="small" @click="editBound(scope.row.id)">编辑</el-button>
            <el-button v-if="isAuth('wh:in:revoke') && (Number(scope.row.state) === 1 || Number(scope.row.state) === 2)" type="text" size="small" @click="cancelItem(scope.row)">撤回</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>
    </template>
    <!--    入库-->
    <stock-order-inbound v-if="inboundVisible" ref="inbound" @refreshDataList="getDataList" @onChose="onChose"></stock-order-inbound>
    <stock-order-outbound v-if="outboundVisible" ref="outbound" @refreshDataList="getDataList" @onChose="onChose"></stock-order-outbound>
    <detail v-if="detailVisible" ref="details" @onChose="onChose"/>
  </div>
</template>

<script>
  import UserComponent from '../common/user-component'
  import StockOrderInbound from './stock-order-inbound'
  import StockOrderOutbound from './stock-order-outbound'
  import Detail from './stock-order-detail'
  import { revokeInOutBound } from '@/api/warehouse'

  export default {
    name: 'stock-order',
    components: {StockOrderInbound, UserComponent, StockOrderOutbound, Detail},
    created () {
      this.optionsState = this.$store.state.common.approveStates
      this.queryData()
    },
    data () {
      return {
        inboundVisible: false,
        outboundVisible: false,
        detailVisible: false,
        dataForm: {
          userId: '',
          date: '',
          recordType: '0'
        },
        optionsState: [],
        recordTypeOptions: [
          {
            value: null,
            label: '请选择'
          },
          {
            value: '0',
            label: '入库'
          },
          {
            value: '1',
            label: '出库'
          }
        ],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: []
      }
    },
    methods: {
      onChose () {
        this.inboundVisible = false
        this.outboundVisible = false
        this.detailVisible = false
      },
      queryData () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/biz-service/in-out-bound-ctl/my-submit-inbound-apply'),
          method: 'get',
          params: this.$http.adornParams({
            'current': this.pageIndex,
            'size': this.pageSize,
            'userId': this.dataForm.userId ? this.dataForm.userId : '',
            'beginTime': this.dataForm.date ? this.dataForm.date[0] : null,
            'endTime': this.dataForm.date ? this.dataForm.date[1] : null,
            'recordType': this.dataForm.recordType,
            'recordCode': this.dataForm.recordCode
          })
        }).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
      },
      // 编辑
      editBound (id) {
        if (Number(this.dataForm.recordType) === 1) {
          // 出库
          this.outBound(id, false)
        } else {
          // 入库
          this.inBound(id, false)
        }
      },
      // 入库申请
      inBound (id, display) {
        this.inboundVisible = true
        this.$nextTick(() => {
          this.$refs.inbound.init(id, display)
        })
      },
      outBound (id) {
        this.outboundVisible = true
        this.$nextTick(() => {
          this.$refs.outbound.init(id)
        })
      },
      // 申请单类型
      formatRecordType (row) {
        if (!row.recordType) return ''
        if (row.recordType === '0') return '入库'
        if (row.recordType === '1') return '出库'
        return ''
      },
      // 转换属性“状态”
      formatState (row) {
        if (!row.state) return ''
        const item1 = this.optionsState.find((item) => item.code === row.state.toString())
        return item1 ? item1.value : ''
      },
      // 导出
      exportItem (id) {
        // todo
        this.$message.warning('功能暂未开放')
      },
      // 撤回
      async cancelItem (row) {
        if (row.state === '0') {
          this.$message.warning('不允许撤回，请先提交！')
          return
        }
        await revokeInOutBound({'id': row.id}).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      detailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.details.init(id)
        })
      },
      recordTypeChanged () {
        this.getDataList()
      }
    }
  }
</script>

<style scoped>

</style>
