<template>
  <div class="app-container">
    <div class="filter-container">

      <el-date-picker
        v-model="searchTime"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd">
      </el-date-picker>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
          搜索
        </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="!webType ? 'mini':''"
      border
      default-expand-all>
      <el-table-column
        v-if="infoType == 1"
        prop="materialCode"
        label="纸张编码">
      </el-table-column>
      <el-table-column
        v-if="infoType == 1"
        prop="materialName"
        label="纸张名称">
      </el-table-column>
      <el-table-column
        v-if="infoType == 2"
        prop="materialCode"
        label="辅料编码">
      </el-table-column>
      <el-table-column
        v-if="infoType == 2"
        prop="materialName"
        label="辅料名称">
      </el-table-column>
      <template v-if="webType">
        <el-table-column
          prop="verifyDate"
          label="操作时间">
          <template slot-scope="scope">
            <span v-if="scope.row.verifyDate !=null">{{scope.row.verifyDate | getTime}}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="stockBillCode"
          label="单据号">
            <template slot-scope="{row}">
              <el-link v-if='row.stockBillCode' @click.stop="runERP(row)" type="primary">{{row.stockBillCode}}</el-link>
            </template>
        </el-table-column>
        <el-table-column
          prop="fromStockRoomName"
          label="来源库房">
        </el-table-column>
        <el-table-column
          label="目标库房"
          prop="toStockRoomName"
        >
        </el-table-column>
        <el-table-column
          label="操作数量">
          <template slot-scope="{row}">
            {{row.stockQty+row.mtUnit}}
          </template>
        </el-table-column>
        <el-table-column
          label="期初数量">
          <template slot-scope="{row}">
            {{row.beforeQty+row.mtUnit}}
          </template>
        </el-table-column>
        <el-table-column
          label="期末数量">
          <template slot-scope="{row}">
            <el-button type="green" circle size="mini" class="iconfont" v-if="row.borrowFlag==1">&#xe62f;</el-button>
            <el-button type="warning" circle size="mini" class="iconfont" v-else-if="row.borrowFlag==-1">&#xe630;</el-button>
            &nbsp;
            {{row.afterQty+row.mtUnit}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作说明"
          prop="billMemo">
        </el-table-column>
      </template>
    </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 { getStockPaperTradeBillList } from '@/api/stock/paperInventory'
import {getStockRoomList} from '@/api/baseInfo/stockRoomList/stockRoomList.js'
export default {
  name: 'TradeBillList',
  props: {
    isChoose: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      webType: true,
      infoType: 1, // 1为纸张2为辅料
      idMaterial:0,
      idCompany:0,
      idStockRoom:0,
      materialCode: '',
      searchTime:[],
      search: {
        beginTime:'',
        endTime:'',
        keyWord: ''
      },
      form: {},
      options: [],

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

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

      listLoading: false,
      tableData: [],
      basePaperName: ''
    }
  },
  created() {
    this.infoType = this.$route.query.infoType
    if(this.infoType== 1){
      this.paperCode = this.$route.query.paperCode
    }else {
      this.materialCode = this.$route.query.materialCode
    }
    this.idMaterial = this.$route.query.idMaterial
    this.idCompany = this.$route.query.idCompany
    this.idStockRoom = this.$route.query.idStockRoom
    this.tempRoute = Object.assign({}, this.$route)
    this.setTagsViewTitle()
  },
  mounted() {
    this.getPaperInfo()
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
  },
  methods: {
    // 设置标签名称
    setTagsViewTitle() {
      const title = '账表流水'
      let route = null
      // console.log("tempRoute2",this.tempRoute);
      if (this.infoType == 1) {
        route = Object.assign({}, this.tempRoute, { title: `${this.paperCode}-${title}` })
      } else {
        route = Object.assign({}, this.tempRoute, { title: `${this.materialCode}-${title}` })
      }

      // console.log("route",route)
      this.$store.dispatch('tagsView/updateVisitedView', route)
    },
    // 获取列表
    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 page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var params = _that.search
      var data = {
        "idMaterial": _that.idMaterial,
        "idCompany": _that.idCompany,
        "idStockRoom": _that.idStockRoom,
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }
      // console.log(data);
      getStockPaperTradeBillList(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);
      getInfo(data).then(response => {
        console.log(response)
        _that.companyList = response.data
        _that.search.idCompany = _that.companyList[0].id
        _that.getPaperInfo()

      }).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

      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getPaperInfo()
    },
    // 搜索
    searchData() {
      this.listData.page = 1
      this.getPaperInfo()
    },


    runERP(row) {
      var _that = this
      var userAgent = navigator.userAgent
      if (userAgent.indexOf('DnyERPBrowser') > 0) {
        var data = {
          package: 'dyInventoryPlugin',
          runParam: 'Form:ShowModal:TfrmStockBill_Update_StockIn@0,' + row.idStockBill + ',View'
        }

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

    // 弹窗内选择操作
    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>
