<!-- 库存管理 -->
<template>
    <div class="stock">
      <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>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="exportExcel()">导出Excel</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="-"
          header-align="center"
          align="center"
          label="编码">
        </el-table-column>
        <el-table-column
          prop="materialName"
          header-align="center"
          align="center"
          min-width="100"
          label="名称">
        </el-table-column>
        <el-table-column
            prop="categoryName"
            header-align="center"
            align="center"
            label="类别">
        </el-table-column>
        <el-table-column
          prop="-"
          header-align="center"
          align="center"
          label="状态">
        </el-table-column>
        <el-table-column
          prop="specifications"
          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="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="-"
          header-align="center"
          align="center"
          label="批次号">
        </el-table-column>
        <el-table-column
          prop="-"
          header-align="center"
          align="center"
          label="来源">
        </el-table-column>
        <el-table-column
          prop="shelveName"
          header-align="center"
          align="center"
          label="存放货架">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          :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:stock-mg-ctl:info')" type="text" size="small" @click="details(scope.row.materialId)">查看</el-button>
            <el-button v-if="isAuth('wh:stock-mg-ctl:update')" 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>
      <stock-details v-if="detailVisible" ref="details"/>
      <stock-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
    </div>
</template>

<script>
  import StockDetails from './stock-details'
  import StockAddOrUpdate from './stock-add-or-update'
  export default {
    name: 'stock',
    components: {StockAddOrUpdate, StockDetails},
    data () {
      return {
        dataForm: {
          materialName: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        detailVisible: false,
        addOrUpdateVisible: false
      }
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      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
          })
        }).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 () {
        this.$http({
          url: this.$http.adornUrl('/biz-service/stock-mg-ctl/exportedExcel'),
          method: 'get',
          params: this.$http.adornParams({
            'materialName': this.dataForm.materialName
          })
        }).then(({data}) => {
          if (data && data.code !== '200') {
            this.$message.error(data.msg || '系统错误，请联系管理员')
          }
        })
      },
      // 每页数
      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)
        })
      }
    }
  }
</script>

<style scoped>

</style>
