<template>
  <div class="app-container">
    <div class="filter-container" style="margin-bottom: 10px">
      <el-date-picker
        size="mini"
        v-model="searchTime"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd">
      </el-date-picker>
      <el-input v-model='search.materialName' style="width: 200px" size="mini" clearable placeholder="物料名称"></el-input>
      <el-input v-model='search.billCode' style="width: 200px" size="mini" clearable placeholder="出库单号"></el-input>
      <el-input v-model='search.printBillCode' style="width: 200px" size="mini" clearable placeholder="施工单号"></el-input>
      <el-input v-model='search.relatedCompanyName' style="width: 200px" size="mini" clearable placeholder="往来单位"></el-input>
      <el-input v-model='search.deptName' style="width: 200px" size="mini" clearable placeholder="部门"></el-input>
      <el-button class="filter-item" type="primary" size="mini" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button size="mini" 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>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      size="mini"
      border
      @sort-change="changeSort"
      :summary-method="getSummaries"
      show-summary
      default-expand-all>
      <el-table-column
        width="130px"
        prop="paperCode"
        label="审核时间">
        <template slot-scope="{row}">
          <span v-if="row.stockBill.verifyTimeLong>0"> {{row.stockBill.verifyDate |getTime}}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="stockBill.deptName"
        label="部门">
      </el-table-column>
      <el-table-column label="往来单位" prop="stockBill.relatedCompanyName"  show-overflow-tooltip></el-table-column>
      <el-table-column
        prop="stockBill.billCode"
        width="130px"
        label="单据号">
        <template slot-scope="{row}">
          <el-link @click.stop="runERP(row)" type="primary">{{row.stockBill.billCode}}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="施工单号"
        width="100px">
        <template slot-scope="{row}">
          <el-link @click="handleDetail(row.stockBillItemM.idPrintBill)" style="color: #409EFF;cursor: pointer"
                   class="ellipsis">{{row.stockBillItemM.printBillCode}}</el-link>
        </template>
      </el-table-column>
        <el-table-column
          prop="stockBillItemM.materialName"
          show-overflow-tooltip
          width="170px"
          label="物料名称">
        </el-table-column>
      <el-table-column
        v-if="pageType == 1"
        prop="stockBillItemM.breedOriginPaperName"
        show-overflow-tooltip
        width="170px"
        label="施工单用纸">
      </el-table-column>
      <el-table-column
        v-if="pageType == 1"
        label="计量单位"
        width="60"
        prop="paperInfo.mtUnit"
      >
      </el-table-column>
      <el-table-column
        v-if="pageType == 2"
        label="计量单位"
        width="60"
        prop="materialInfo.mtUnit"
      >
      </el-table-column>
      <el-table-column
        prop="stockBillItemM.itemPurchaseQty"
        :label="pageType == 1?'数量(吨)':'数量(采购)'">
      </el-table-column>

        <el-table-column
          label="数量"
          prop="stockBillItemM.itemQty">
        </el-table-column>

        <el-table-column
          label="单价"
          prop="stockBillItemM.itemPrice">
        </el-table-column>
      <el-table-column
        prop="stockBillItemM.itemMoney"
        width="100"
        label="金额">
      </el-table-column>
      <el-table-column
        label="库位"
        show-overflow-tooltip
        prop="stockBillItemM.stockRoomName"
      >
      </el-table-column>
      <el-table-column
        label="机台"
        show-overflow-tooltip
        prop="stockBillItemM.machineInfoName">
      </el-table-column>

      <el-table-column
        label="制单人"
        prop="stockBill.createManUser">
      </el-table-column>
      <el-table-column
        label="审核人"
        prop="stockBill.verifyMan">
      </el-table-column>
      <el-table-column
        label="领料人"
        prop="stockBillItemM.referenceBillCreateMan">
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getPaperInfo" />

    <printbill-detail ref="printbillDetail"></printbill-detail>
  </div>
</template>

<script>
import { getStockBillItemMList } from '@/api/stock/paperWarehousingBill'
import PrintbillDetail from "@/components/printbillDetail/printbillDetail";
import {exportData} from "@/api/orderCharge/orderCollection";
import {formatMoney} from "@/filters";
export default {
  name: 'paperExwarehouseBill',
  components: {PrintbillDetail},
  props: {
    isChoose: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      webType: true,
      pageType: 1, // 1为纸张2为辅料
      orderByString: '',
      search: {
        beginTime: '',
        endTime: '',
        materialName: '',
        billCode: '',
        billStatus: 100,
        printBillCode: '',
        relatedCompanyName: '',
        deptName: ''
      },

      searchTime: [],
      form: {},
      options: [],

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

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

      listLoading: false,
      tableData: [],
      collection: {},
      basePaperName: ''
    }
  },
  mounted() {
    this.pageType = this.$route.fullPath.split('/').pop()
    this.getPaperInfo()
  },
  methods: {
    // 获取列表
    getPaperInfo() {
      const _that = this
      _that.listLoading = true
      if (_that.searchTime == null || _that.searchTime < 0) {
        _that.search.beginTime = ''
        _that.search.endTime = ''
      } else {
        _that.search.beginTime = _that.searchTime[0]
        _that.search.endTime = _that.searchTime[1]
      }
      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var idBIllDefine = null
      if(_that.pageType == 1){
        idBIllDefine = 8
      }else {
        idBIllDefine = 9
      }
      var data = {
        'idBIllDefine': idBIllDefine,
        'params': JSON.stringify(params),
        'page': JSON.stringify(page),
      }
      // console.log(data);
      getStockBillItemMList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        _that.collection = response.collection
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    getSummaries(param) {
      var _that = this
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return
        }
        if(_that.pageType == 1){
          if (column.label == '数量(吨)') {
            sums[index] = _that.collection.itemPurchaseQtySum
          } else if (column.label == '数量') {
            sums[index] = _that.collection.itemQtySum
          } else if (column.label == '金额') {
            sums[index] = formatMoney(_that.collection.itemMoneySum)
          } else {
            sums[index] = ''
          }
        }else {
          if (column.label == '金额') {
            sums[index] = formatMoney(_that.collection.itemMoneySum)
          } else if (column.label == '数量') {
            sums[index] = _that.collection.itemQtySum
          } else if (column.label == '数量(采购)') {
            sums[index] = _that.collection.itemPurchaseQtySum
          } else {
            sums[index] = ''
          }
        }

      })
      return sums
    },
    // 排序
    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()
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },

    // 导出
    exportBtn() {
      const _that = this
      var params = _that.search
      var exportName = null
      if (_that.pageType == 1) {
        params['idBillDefine'] = 8
        exportName = '纸张出库明细导出'
      } else {
        params['idBillDefine'] = 9
        exportName = '辅料出库明细导出'
      }

      var data = {
        code: 'StockOutItemM_Exporter',
        exportName: exportName,
        params: params
      }
      exportData(data).then(res =>{
        console.log(res);
        if(res.httpCode == 200){
          _that.$message.success('导出成功，请前至系统管理-我的任务中进行查看和下载')
        }
      })
    },

    runERP(row) {
      var _that = this
      var userAgent = navigator.userAgent
      var idBIllDefine = null
      if(_that.pageType == 1){
        idBIllDefine = 8
      }else {
        idBIllDefine = 9
      }
      if (userAgent.indexOf('DnyERPBrowser') > 0) {
        var data = {
          package: 'dyInventoryPlugin',
          runParam: 'Form:ShowModal:TfrmStockBill_Update_StockIn@'+idBIllDefine+',' + row.stockBill.id + ',View'
        }

        dyERP.sendRunForm(JSON.stringify(data))
      } else {
        _that.$message({
          message: '请使用开发商提供的ERP客户端',
          type: 'warning'
        })
      }
    },

    handleDetail(id) {
      const _that = this;
      _that.$refs.printbillDetail.idOrder = id;
      _that.$refs.printbillDetail.printVisible = true;
      _that.$refs.printbillDetail.getDetail()
    },
  }
}
</script>

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