<template>
	<div class="">
    <el-dialog title="预付款冲抵" width="1170px" :visible.sync="yfcdState" center @close="close">
      <el-form class="cdForm row" label-width="90px">
        <el-form-item label="单号" prop="FrID">
          <el-input class="w180" @keyup.native="checkInteger(czMsg,'FrID')" v-model="czMsg.FrID"></el-input>
        </el-form-item>
        <el-form-item label="费用类型">
          <el-select filterable v-model='czMsg.CostTypeID' class="w120">
            <el-option label="不限" value=''></el-option>
            <el-option v-for="(item,index) in costList" :key="index" :label="item.Name" :value='item.ID'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="查询类型">
          <el-select filterable v-model='czMsg.IsDiJieSelect' class="w110">
            <el-option label="只看可冲抵" value='0'></el-option>
            <el-option label="查询所有" value='1'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="交易日期">
          <el-date-picker class="w220" v-model="czdateList" type="daterange" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期" size="samll">
          </el-date-picker>
        </el-form-item>
        <div class="row q-pt-sm">
          <el-button class="hollowFixedBtn" @click="czgetList" style="line-height: 30px;">查询</el-button>
          <!-- <el-button type="primary" class="normalBtn" @click="downloadczgetList" style="line-height: 30px;">导出</el-button> -->
        </div>
      </el-form>
      <p style="color: red;text-align: right;padding: 5px;">注：冲抵请勾选单据后点击下方确认冲抵</p>
      <el-table :data="czList" v-loading="yfcdLoading" ref="multipleTable" style="width: 100%;"
      @selection-change="handleSelectionChange"
      @current-change="handleCurrentChange"
      @select-all="toggleSelection()"
      row-key="FrID">
        <el-table-column type="selection" :selectable="selectable" :reserve-selection="true" width="55"/>
        </el-table-column>
        <el-table-column prop="FrID" label="单号" width="80"></el-table-column>
        <el-table-column prop="BranchName" label="公司"></el-table-column>
        <el-table-column prop="CostTypeName" label="费用类型" width="120"></el-table-column>
        <el-table-column prop="CurrencyName" label="币种" width="80"></el-table-column>
        <el-table-column prop="Rate" label="汇率" width="80"></el-table-column>
        <el-table-column prop="Money" label="金额" width="120"></el-table-column>
        <el-table-column prop="MatchMoney" label="冲抵金额" width="120"></el-table-column>
        <el-table-column prop="StatusStr" label="状态"></el-table-column>
        <el-table-column prop="" label="付款对象" width="150">
          <template slot-scope="scope">
            <el-tooltip v-if="scope.row.ClientTypeName" class="item" effect="dark"
                :content="scope.row.ClientTypeName+':'+scope.row.RemitterName+'('+scope.row.AccountNumber+')'" placement="top-start">
                <div>{{scope.row.ClientTypeName}}:{{scope.row.RemitterName}}</div>
              </el-tooltip>
              <template v-else>-</template>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作" width="150">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="预付款剩余余额转移至新单" placement="top-start">
              <span @click="matchTransfer(scope.row)" style="cursor: pointer; color:#33B3FF">余额转移</span>
            </el-tooltip>|
            <el-tooltip class="item" effect="dark" content="预付款退款" placement="top-start">
              <span @click="goUrlZD(scope.row)" style="cursor: pointer; color:#33B3FF">退款</span>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background @current-change="handleCurrentChange2" layout="total,prev, pager, next, jumper"
        :page-size="czMsg.pageSize" :total='total2'>
      </el-pagination>
      <div slot="footer" class="dialog-footer">
        <button class="hollowFixedBtn" @click="close">{{$t('pub.cancelBtn')}}</button>
        <button class="normalBtn" type="primary" @click="goUrlZD('')">
          确认冲抵
        </button>
      </div>
    </el-dialog>

    <el-dialog title="余额转移" width="400px" :visible.sync="IsMatchTransfer" center>
      <p style="color: #33B3FF; margin-bottom: 10px">单号{{matchMsg.TransferFrId}}：剩余余额
        {{matchMsg.TransferMoney}}{{matchMsg.CurrencyName}}将全部转移*</p>
      <p>转入单号：<el-input class="w180" @keyup.native="checkInteger(matchMsg,'JoinFrId')" v-model="matchMsg.JoinFrId">
        </el-input>
      </p>
      <br>
      <button v-loading="matchBtn" class="normalBtn" @click="sureMatchTransfer()"
        style="margin-left: 150px;margin-bottom: 10px">确认转移</button>
    </el-dialog>

	</div>
</template>
<script>
	export default {
    props:[""],
		components: {

		},
		data() {
			return {
        yfcdState: true,
        czMsg:{
          pageIndex: 1,
          pageSize: 8,
          FrID: "",
          CostTypeID: "",
          sTradeDate: "",
          eTradeDate: "",
          IsAdvanceFinance: 1,
          IsDiJieSelect: '0'
        },
        yfcdLoading: false,
        total2: 0,
        czList: [
          // {FrID:1},
          // {FrID:2}
        ],
        costList: [],
        czdateList: [],
        yfcheckList: [],
        yfHandmsg: {
          OtherType: 7,
          ReFinanceId: 0,
          Rate: 0,
          czState: true
        },
        yfOutBranchId: 0,
        matchMsg: {
          TransferFrId: 0,
          JoinFrId: 0,
          TransferMoney: 0,
          CurrencyName: ''
        },
        IsMatchTransfer: false,
        matchBtn: false,
      }
		},
		computed: {
		},
    mounted(){
      let userInfo = this.getLocalStorage();
      this.yfOutBranchId = userInfo.School_Id;
      this.yfcdState = true
      this.getCostTypeList()
      this.czgetList()
    },
		methods: {
      matchTransfer(item) {
        this.matchMsg.TransferFrId = item.FrID;
        this.matchMsg.TransferMoney = (item.Money - item.MatchMoney).toFixed(2);
        this.matchMsg.CurrencyName = item.CurrencyName;
        this.IsMatchTransfer = true;
      },
      sureMatchTransfer() {
        if (this.matchMsg.TransferFrId <= 0) {
          return this.$message.error("需转移单号有误");
        }
        if (this.matchMsg.JoinFrId <= 0) {
          return this.$message.error("转入单号有误");
        }
        this.matchBtn = true;
        this.apipost("Financial_post_SetMatchFinanceSurplusTransfer", this.matchMsg, res => {
            if (res.data.resultCode == 1) {
              this.matchBtn = false;
              this.$message.success(res.data.message);
              this.matchMsg.TransferFrId = 0;
              this.matchMsg.TransferMoney = 0;
              this.IsMatchTransfer = false;
              this.czgetList();
            } else {
              this.$message.error(res.data.message);
              this.matchBtn = false;
            }
          },
          err => {}
        );
      },
      goUrlZD(item) {
        let that = this;
        let yfcheckList
        if (item) {
          this.yfcheckList = []
          this.yfcheckList.push(item)
          that.yfHandmsg.OtherType = 65
        } else {
          that.yfHandmsg.OtherType = 7
        }
        yfcheckList = JSON.parse(JSON.stringify(this.yfcheckList));
        if (this.yfcheckList.length > 1) {
          this.Error('只能选择一项！');
          return;
        }
        if (this.yfcheckList.length == 0) {
          this.Error('请选择一项');
          return;
        }

        let arr = [];
        // var TCArr = [this.yfHandmsg.TCNUM];
        let orderObj = {
          OrderID: 0,
          OrderSource: 8,
          Obj: {}
          // TCIDList: TCArr
        };
        that.yfHandmsg.ReFinanceId = this.yfcheckList[0].FrID;
        that.yfHandmsg.Rate = this.yfcheckList[0].Rate;
        var msg = {}
        // msg = JSON.stringify(that.yfHandmsg);
        if (item) {
          msg = {
            OtherType: 65,
            ReFinanceId: that.yfHandmsg.ReFinanceId,
            czState: that.yfHandmsg.czState
          }
        } else {
          msg = {
            OtherType: 7,
            ReFinanceId: that.yfHandmsg.ReFinanceId,
            czState: that.yfHandmsg.czState
          }
        }

        let routeData = this.$router.resolve({
          path: "/financial/financalDocument/ChoiceAddFinancialDocuments",
          query: {
            czmsg: JSON.stringify(msg),
            orderObj: JSON.stringify(orderObj),
            companyID: this.yfOutBranchId,
            Type: 2,
            searchTitle: "分摊"
          }
        });
        window.open(routeData.href, "_blank");
        // this.close()
      },
      czgetList() {
        if (this.czdateList && this.czdateList.length > 0) {
          this.czMsg.sTradeDate = this.czdateList[0];
          this.czMsg.eTradeDate = this.czdateList[1];
        } else {
          this.czMsg.sTradeDate = "";
          this.czMsg.eTradeDate = "";
        }
        let msg = Object.assign({}, this.czMsg);
        if (msg.FrID == "") {
          msg.FrID = 0;
        }
        this.yfcdLoading = true
        this.apipost(
          "Financial_get_GetAdvanceFinancePageList",
          msg,
          res => {
            this.yfcdLoading = false
            if (res.data.resultCode === 1) {
              this.czList = res.data.data.pageData;
              this.total2 = res.data.data.count;
            } else {
              this.Error(res.data.message);
            }
          },
          null
        );
      },
      downloadczgetList(){
        if (this.czdateList && this.czdateList.length > 0) {
          this.czMsg.sTradeDate = this.czdateList[0];
          this.czMsg.eTradeDate = this.czdateList[1];
        } else {
          this.czMsg.sTradeDate = "";
          this.czMsg.eTradeDate = "";
        }

        let msg = Object.assign({}, this.czMsg);
        if (msg.FrID == "") {
          msg.FrID = 0;
        }
        msg.EmployeeId = this.getLocalStorage().EmployeeId
        this.yfcdLoading = true
        this.GetLocalFile("Financial_get_OutToAdvanceFinance", msg, "预付款冲抵" +
          ".xls");
        this.yfcdLoading = false
      },
      getCostTypeList() {
        this.apipost(
          "Financial_get_GetAdvanceCostTypeList", {},
          res => {
            if (res.data.resultCode === 1) {
              this.costList = res.data.data;
            } else {
              this.Error(res.data.message);
            }
          },
          null
        );
      },
      handleCurrentChange2(val) {
        this.czMsg.pageIndex = val;
        this.czgetList();
      },
      toggleSelection(rows) {
        return
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleCurrentChange(val) {
        this.$refs.multipleTable.clearSelection();
        this.yfcheckList =[val];
        let rows = [val]
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val){
        this.yfcheckList = val
      },
      selectable(row, index) {
        return true
        if ((this.yfcheckList.length>0&&this.czList[index].FrID==this.yfcheckList[0].FrID)||this.yfcheckList.length==0) {
          return true;
        } else {
          return false;
        }
      },
      close(){
        this.yfcdState=false
        this.$emit('close')
      }
    }
	}
</script>

<style lang="scss" scoped>

</style>
