<!-- 库存管理 -->
<template>
    <div class="stock">
      <template v-if="!detailVisible && !addOrUpdateVisible && !noticeChangeAttachVisible">
        <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item label="名称">
            <el-input v-model="dataForm.materialName" placeholder="请输入名称" clearable/>
          </el-form-item>
          <el-form-item label="类别">
            <el-cascader
              v-model="dataForm.materialTypeId"
              placeholder="请输入"
              :options="optionsCate"
              :props="{ expandTrigger: 'hover' }">
            </el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
<!--            <el-button v-if="isAuth('wh:stockmanagement:exported')" type="primary" @click="exportExcel()">导出Excel</el-button>-->
            <el-button v-if="isAuth('wh:stockmanagement:noteChangeConfig')" type="primary" @click="setNoticeChangeHandel()">库存更改通知设置</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="materialCode"
            header-align="center"
            align="center"
            min-width="180"
            :show-tooltip-when-overflow="true"
            label="编码">
          </el-table-column>
          <el-table-column
            prop="categoryName"
            header-align="center"
            align="center"
            label="类别">
          </el-table-column>
          <el-table-column
            prop="materialName"
            header-align="center"
            align="center"
            min-width="180"
            :show-overflow-tooltip="true"
            label="名称">
          </el-table-column>
          <el-table-column
            prop="specifications"
            header-align="center"
            align="center"
            width="140"
            :show-overflow-tooltip="true"
            label="规格">
          </el-table-column>
          <el-table-column
            prop="isInventory"
            header-align="center"
            align="center"
            :formatter="formatState"
            label="状态">
          </el-table-column>
          <el-table-column
            prop="lockCnt"
            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="safeQuantity"
            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="shelveName"
            header-align="center"
            align="center"
            width="140"
            :show-overflow-tooltip="true"
            label="存放货架">
          </el-table-column>
          <el-table-column
            prop="notes"
            header-align="center"
            align="center"
            width="160"
            :show-overflow-tooltip="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:stockmanagement:info')" type="text" size="small" @click="details(scope.row.materialId)">查看</el-button>
              <el-button v-if="isAuth('wh:stockmanagement:editor')" type="text" size="small" @click="addOrUpdateHandle(scope.row.materialId)">编辑</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-details v-if="detailVisible" ref="details" @onChose="onChose"/>
      <stock-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @onChose="onChose"/>
      <notice-change-setting v-if="noticeChangeAttachVisible" ref="noticeChangeSetting" @onChose="onChose"/>
    </div>
</template>

<script>
  import StockDetails from './stock-details'
  import StockAddOrUpdate from './stock-add-or-update'
  import NoticeChangeSetting from './stock-notice-change-setting'
  import Vue from 'vue'
export default {
    name: 'stock',
    components: {
      StockAddOrUpdate,
      StockDetails,
      NoticeChangeSetting
    },
    data () {
      return {
        dataForm: {
          materialName: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        detailVisible: false,
        addOrUpdateVisible: false,
        noticeChangeAttachVisible: false,
        optionsCate: [] // 类别
      }
    },
    activated () {
      this.getCate()
      this.getDataList()
    },
    methods: {
      onChose () {
        this.addOrUpdateVisible = false
        this.detailVisible = false
        this.noticeChangeAttachVisible = false
      },
      // 获取类别
      getCate () {
        this.$http({
          url: this.$http.adornUrl('/biz-service/stock-mg-ctl/listCategory'),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.optionsCate = data.data
          }
        })
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/biz-service/stock-mg-ctl/list'),
          method: 'get',
          params: this.$http.adornParams({
            'current': this.pageIndex,
            'size': this.pageSize,
            'materialName': this.dataForm.materialName,
            'materialTypeId': this.dataForm.materialTypeId ? this.dataForm.materialTypeId[0] : ''
          })
        }).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
        })
      },
      exportExcel () {
        let url = this.$http.adornUrl('/biz-service/stock-mg-ctl/exportedExcel')
        this.$http({
          url: url,
          method: 'get',
          params: this.$http.adornParams({
            'materialName': this.dataForm.materialName
          })
        }).then(({data}) => {
          if (data && data.code && data.code !== '200') {
            this.$message.error(data.msg || '系统错误，请联系管理员')
          } else {
            window.open(url + '?_token=' + Vue.cookie.get('token') + '&materialName=' + this.dataForm.materialName, '_blank')
          }
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      details (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.details.init(id)
        })
      },
      // 新增、修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 转换属性“状态”
      formatState (row) {
        if (row.isInventory && Number(row.isInventory) === 1 && row.lockCnt && Number(row.lockCnt) > 0) {
          return '盘点中'
        } else if (row.isInventory && Number(row.isInventory) === 1) {
          return '盘点中'
        } else if (row.lockCnt && Number(row.lockCnt) > 0) {
          return '锁定中'
        } else {
          return '正常'
        }
      },
      // 变更通知人设置
      setNoticeChangeHandel () {
        this.noticeChangeAttachVisible = true
        this.$nextTick(() => {
          this.$refs.noticeChangeSetting.init()
        })
      }
    }
  }
</script>

<style scoped>

</style>
