<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom: 0">
      <el-form :inline="true" size="mini">
        <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 v-model="search.idStockRoom">
            <el-option
              v-for="(item,index) in stockRoomList"
              :key="index"
              :value="item.id"
              :label="item.roomName"></el-option>
          </el-select>
        </el-form-item>
      <el-input size="mini" v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item" @keyup.enter.native="searchData"/>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
        <el-checkbox :true-label="1" :false-label="0" v-model="sumFlag" @change="searchData">过滤库存为0的数据</el-checkbox>
        <el-button
        v-if="type == 1"
        class="filter-item"
        style="margin-left: 10px;"
        size="mini"
        type="success"
        @click="chooseList">选择</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;"
      row-key="index"
      size='mini'
      border
      :row-style="customRowStyle"
      @selection-change="selectionChange"
      default-expand-all>
      <el-table-column
        type="selection"
        width="55"
        v-if="type == 1"
        :reserve-selection="true"
      ></el-table-column>
      <el-table-column
        prop="paperCode"
        width="100"
        label="纸张编码">
      </el-table-column>
      <el-table-column
        prop="paperName"
        width="260"
        label="纸张名称">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="priceType"-->
<!--        label="计价规则">-->
<!--      </el-table-column>-->
        <el-table-column
          prop="mtRule"
          label="规格">
        </el-table-column>
      <el-table-column
        label="库房"
        prop="stockRoomName">
      </el-table-column>
      <el-table-column
        label="库存数量"
        prop="stockQty">
      </el-table-column>

      <el-table-column
        label="预出库锁定量"
        prop="virtualQty">
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            启用
          </el-tag>
          <el-tag v-else-if="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" v-if="type !=1" fixed="left" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="webType">
              <el-button @click="chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
          </template>
          <template v-else>
            <el-button @click="ERPChoose(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>
        </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 {getInStockPaperInfoList, getStockPaperInfoList} from '@/api/stock/paperInventory'
import {getCompanyList} from "@/api/stock/checkStockPaper";
import {getStockRoomList} from "@/api/baseInfo/stockRoomList/stockRoomList";

const form = {
  idPaperType: '',
  idPaperBaseInfo: '',
  paperName: '',
  mtRule: '',
  mtSuttle: '',
  paperCode: '',
  mtModel: '',
  convertUnitByToMt: '',
  convertUnitMtToSe: '',
  byUnit: '吨',
  mtUnit: '令',
  seUnit: '张',
  barCode: '',
  remark: ''
}
export default {
  name: 'selectPaperInfo',
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      default: 0
    },
  },
  data() {
    return {
      webType: true,
      paperInfoVisible: false,
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,

      companyList: [], // 分厂列表
      stockRoomList: [], // 库房列表
      search: {
        idCompany: '',
        idStockRoom: 0,
        keyWord: '',
      },
      form: Object.assign({}, form),
      options: [],

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

      confirmdialog: false,
      listLoading: false,
      tableData: [],
      basePaperName: '',
      chooseInfoList: [],
      sumFlag: 1
    }
  },
  mounted() {
    if (this.$route.query.type === undefined) {
      this.webType = true
      this.search.voidFlag = -1
    } else {
      this.webType = false
      this.search.voidFlag = 0
    }
    this.getCompanyInfo()
    this.getStockRoomInfo()
  },
  methods: {
    // 获取列表
    getPaperInfo() {
      const _that = this
      console.log(_that.sumFlag);
      _that.listLoading = true
      if (_that.sumFlag) {
        _that.search.filterZeroPaper = 1
        var params = _that.search
        var page = {
          pageIndex: _that.listData.page,
          pageSize: _that.listData.pageSize
        }
        var data = {
          'params': JSON.stringify(params),
          'page': JSON.stringify(page)
        }
        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
        })
      } else {
        _that.search.filterZeroPaper = 0
        var params = _that.search
        var page = {
          pageIndex: _that.listData.page,
          pageSize: _that.listData.pageSize
        }
        var data = {
          'params': JSON.stringify(params),
          'page': JSON.stringify(page)
        }
        getStockPaperInfoList(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
        })
      }
    },
    // 获取分厂列表
    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

      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 获取库房列表
    getStockRoomInfo() {
      const _that = this
      // var data = {}
      var page = {
        pageIndex: 1,
        pageSize: 100
      }
      var data = {
        'page': JSON.stringify(page)
      }
      getStockRoomList(data).then(response => {
        console.log(response)
        var data = response.data
        var all = {
          roomName: '全部',
          id: 0
        }
        data.unshift(all)
        _that.stockRoomList = data
        _that.search.idStockRoom = data[0].id
        _that.getPaperInfo()
      }).catch(err => {
        _that.$message.error(err)
      })
    },

    // 表格样式
    customRowStyle({row, rowIndex}) {
      if (row.stockQty == 0) {
        return {
          // color: '#F56C6C'
          background: '#f7b1b1'
        }
      }
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },
    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },
    // 多选
    chooseList() {
      this.$emit('choosePaper', this.chooseInfoList)
    },
    selectionChange(val) {
      const _that = this
      _that.chooseInfoList = val
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }
  }
}
</script>

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