<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom: 0">
      <el-form :inline="true">
        <el-form-item label="分厂:">
          <el-select v-model="search.idCompany">
            <el-option
              v-for="(item,index) in companyList"
              :key="index"
              :value="item.id"
              :label="item.companyName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库房:">
          <el-select filterable multiple v-model="idStockRooms">
            <el-option
              v-for="(item,index) in stockRoomList"
              :key="index"
              :value="item.id"
              :label="item.roomName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字:">
          <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item"/>
        </el-form-item>

        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
          搜索
        </el-button>
        <el-button class="filter-item" icon="el-icon-document" type="warning" @click="exportBtn">导出</el-button>

        <div class="top-right-btn">
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <el-button size="mini" circle icon="el-icon-refresh" @click="getPaperInfo"/>
          </el-tooltip>
        </div>
      </el-form>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      :size="!webType ? 'mini':''"
      border
      @sort-change="changeSort"
      default-expand-all>
      <el-table-column
        prop="paperCode"
        label="纸张编码">
        <template slot-scope="{row}">
          <el-link type="primary"
                   @click="$router.push({path:'/stock/tradeBillList/'+row.id ,query:{infoType:1,paperCode:row.paperCode,idMaterial:row.id,idCompany:search.idCompany,idStockRoom:row.idStockRoom}})">
            {{ row.paperCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="paperName"
        sortable="custom"
        label="纸张名称">
      </el-table-column>
      <el-table-column
        prop="stockRoomName"
        width="130"
        label="库房">
      </el-table-column>
      <el-table-column
        prop="mtSuttle"
        width="50"
        label="克重">
      </el-table-column>
      <el-table-column
        prop="mtRule"
        width="100"
        label="规格">
      </el-table-column>
      <el-table-column
        label="品牌"
        prop="mtModel"
      >
      </el-table-column>
      <el-table-column
        prop="paperTypeName"
        label="纸张类型名称">
      </el-table-column>

      <el-table-column
        label="库存数量"
        prop="stockQty"
        width="100px">
      </el-table-column>
      <el-table-column
        label="库存单价"
        prop="stockAvgPrice"
        width="100px">
      </el-table-column>

      <el-table-column
        label="总金额"
        width="200px">
        <template slot-scope="{row}">
          {{ (row.stockAvgPrice * row.stockQty).toFixed(4) }}
        </template>
      </el-table-column>
    </el-table>
        <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getPaperInfo" />
  </div>
</template>

<script>
import {getCompanyList,getInStockPaperInfoList} from '@/api/stock/paperInventory'
import {getStockRoomList} from '@/api/baseInfo/stockRoomList/stockRoomList.js'
import {getCheckStockPaperInfoList} from "@/api/stock/checkStockPaper";
import {exportData} from "@/api/orderCharge/orderCollection";

export default {
  name: 'paperInventoryInRoom',
  props: {
    isChoose: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      webType: true,
      orderByString: '',
      idStockRooms: [],
      search: {
        idCompany: '',
        // idStockRoom: 0,
        idStockRooms:'',
        keyWord: '',
        filterZeroPaper: 1
      },
      form: {},
      options: [],

      companyList: [], // 分厂列表
      stockRoomList: [], // 库房列表

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },

      listLoading: false,
      tableData: [],
      basePaperName: ''
    }
  },
  mounted() {
    this.getCompanyInfo()

    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }

  },
  methods: {
    // 获取列表
    getPaperInfo() {
      const _that = this
      _that.listLoading = true
      _that.search.idStockRooms = _that.idStockRooms.join(',')
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var orderBy = _that.orderByString
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page),
        'orderBy': orderBy,
      }
      // console.log(data);
      getInStockPaperInfoList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },
    // 排序
    changeSort(val) {
      // console.log(val)
      var _that = this
      if (val.order == 'ascending') {
        _that.orderByString = val.prop + ',' + 'asc'
      } else if (val.order == 'descending') {
        _that.orderByString = val.prop + ',' + 'desc'
      } else {
        _that.orderByString = ''
      }
      _that.getPaperInfo()
    },
    // 获取分厂列表
    getCompanyInfo() {
      const _that = this
      var data = {
      }
      // console.log(data);
      getCompanyList(data).then(response => {
        console.log(response)
        _that.companyList = response.data
        _that.search.idCompany = _that.companyList[0].id
        _that.getStockRoomInfo()
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 获取库房列表
    getStockRoomInfo() {
      const _that = this
      var page = {
        pageIndex: 1,
        pageSize: 100
      }
      var data = {
        'page': JSON.stringify(page)
      }
      getStockRoomList(data).then(response => {
        console.log(response)
        var data = response.data
        _that.stockRoomList = data
        // _that.search.idStockRoom = _that.stockRoomList[0].id
        _that.getPaperInfo()

      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },
    // 导出
    exportBtn() {
      const _that = this
      var data = {
        code: 'InStockPaperInfo_Exporter',
        exportName: '',
        params: _that.search
      }
      exportData(data).then(res => {
        console.log(res);
        if (res.httpCode == 200) {
          _that.$message.success('导出成功，请前至系统管理-我的任务中进行查看和下载')
        }
      })
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  margin-bottom: 15px;
}
</style>
