<template>
	<div class="eibs-tab">
		<c-col :span="24">
			<c-col :span="12">
				<el-form-item label="业务金额" prop="liaccv.concur">
					<c-col :span="8">
						<c-select v-model="model.liaccv.concur" disabled style="width: 95%" dbCode="curtxt">
						</c-select>
					</c-col>
					<c-col :span="16">
						<c-input-currency :disabled="true" v-model="model.liaccv.newamt" :currency="model.liaccv.concur"></c-input-currency>
					</c-col>
				</el-form-item>
			</c-col>

			<c-col :span="12">
				<el-form-item label="应收比例" prop="liaccv.cshpct">
					<div style="width:100%;display:inline-block">
            <c-col :span="22">
              <c-input-currency :precision=2 v-model="model.liaccv.cshpct" :disabled="!isAdjustCshpct" customModifykey="liaccv.cshpct" @change="customAddModify(model.liaccv, 'cshpct')"></c-input-currency>
            </c-col>
            <c-col :span="1" style="margin-left:5px">
              <span>%</span>
            </c-col>
					</div>
				</el-form-item>
			</c-col>
		</c-col>

		<c-col :span="24">
			<c-col :span="12">
				<el-form-item label="应付保证金金额" prop="liaccv.concur">
					<c-col :span="8">
						<c-select v-model="model.liaccv.concur" dbCode="curtxt" disabled style="width: 95%" placeholder="">
						</c-select>
					</c-col>
					<c-col :span="16">
						<c-input-currency :disabled="true" v-model="model.liaccv.pctresamt" :currency="model.liaccv.concur"></c-input-currency>
					</c-col>
				</el-form-item>
			</c-col>

			<c-col :span="12">
				<el-form-item label="实收比例" prop="liaccv.relcshpct">
					<div style="width:100%;display:inline-block">
            <c-col :span="22">
              <c-input-currency :precision=2 :disabled="true" v-model="model.liaccv.relcshpct"></c-input-currency>
            </c-col>
            <c-col :span="2" style="padding-left:5px">
              <span>%</span>
            </c-col>
					</div>
				</el-form-item>
			</c-col>
		</c-col>

		<c-col :span="24">
      <c-col :span="12">
				<el-form-item label="折算金额总和">
						<c-col :span="8">
							<c-select v-model="model.liaccv.concur" dbCode="curtxt" disabled style="width: 95%">
							</c-select>
						</c-col>
						<c-col :span="16">
							<c-input-currency v-model="model.liaccv.totcovamt" :disabled="true" :currency="model.liaccv.concur"></c-input-currency>
						</c-col>
				</el-form-item>
			</c-col>
      <c-col :span="12">
      <el-form-item label="" prop="liaccv.gleflg">
          <el-form-item v-if="isAdjustCcv || model.liaccv.glebut==='Y'" prop="liaccv.gleflg" class="checkbox-left" label-width="0px">
            <c-checkbox true-label="Y" false-label="N" :disabled="xxxsetFlag || (model.dnfmod && model.dnfmod.ccvtyp ==='C') || (model.dnfmodVo && model.dnfmodVo.ccvtyp ==='C')" v-model="model.liaccv.gleflg" @change="customAddModify(model.liaccv, 'gleflg')">过账</c-checkbox>
          </el-form-item>
          <c-button size="small" v-if="model.liaccv.addbut==='Y'" type="primary" @click="onLiaccvAdd">
            添加
          </c-button>
          <c-checkbox v-if="this.root.trnName==='giteng' || this.root.trnName==='niteng' || this.root.trnName==='liteng' || this.root.trnName==='diteng'" 
                true-label="X" false-label="" v-model="model.liaccv.chgcurflg" class="checkbox-left">保证金货币调整
          </c-checkbox>
      </el-form-item>
      </c-col>
		</c-col>
		<!-- 添加的弹框 -->
		<el-dialog  v-dialogDrag title="保证金列表" :visible.sync="dialogTableVisible" width="60%" v-if="dialogTableVisible">
      <c-table :list="model.liaccv.stmTableData" :columnsConfig="columns" :paginationShow="true" :total="model.liaccv.stmTableData.length" @row-dblclick="pickDepItem">
				<el-table-column prop="op" label="OP" width="80" fixed="right">
					<template slot-scope="scope">
            <!-- <span>{{ ['amt'].includes(item.prop) ? moneyFormat(scope.row[item.prop], scope.row.cur) : scope.row[item.prop] }}</span> -->
						<c-button style="margin-left: 0" size="small" type="primary" @click="pickDepItem(scope.row)">
							选择
						</c-button>
					</template>
				</el-table-column>
			</c-table>
		</el-dialog>
    <!-- 适用于业务交易需要选取保证金 -->
    <div v-if="!isAdjustCcv">
      <div>
        <c-col :span="24">
          <c-table :border="true" :list="model.liaccv.liaccvg" :total="model.liaccv.liaccvg.length" style="width:100%,text-align:center">
            <el-table-column prop="ownref" label="保证金编号" min-width="180px">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'liaccv.liaccvg.' + scope.$index + '.ownref'">
                  <span>{{scope.row.ownref}}</span>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="cur" label="币种" min-width="110px">
              <template slot-scope="scope">
                <span>{{scope.row.cur}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="oldamt" label="初始余额" min-width="110px">
              <template slot-scope="scope">
                <span>{{moneyFormat(scope.row.oldamt, scope.row.cur)}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="amt" label="变动金额" min-width="110px">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'liaccv.liaccvg.' + scope.$index + '.amt'">
                  <c-input-currency v-if="isAdjustAmt && scope.row.oldamt > 0" v-model="scope.row.amt" :currency="scope.row.cur" @handleChange="adjustLiaccvg(scope.row, 'amt')" />
                  <span v-else>{{moneyFormat(scope.row.amt, scope.row.cur)}}</span>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="acc" label="保证金账号" min-width="190px">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'liaccv.liaccvg.' + scope.$index + '.acc'">
                  <span>{{scope.row.acc}}</span>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="rat" label="汇率" min-width="150px">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'liaccv.liaccvg.' + scope.$index + '.rat'">
                  <c-input-currency :precision="6" v-if="scope.row.concur !== scope.row.cur" v-model="scope.row.rat" @handleChange="adjustLiaccvg(scope.row, 'rat')" />
                  <span v-else>{{scope.row.rat}}</span>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="concur" label="业务币种" min-width="110px"></el-table-column>
            <el-table-column prop="covamt" v-if="!xxxsetFlag" label="折算后金额" min-width="110px">
              <template slot-scope="scope">
                <span>{{moneyFormat(scope.row.covamt, scope.row.concur)}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="acccovamt" v-if="xxxsetFlag" label="使用金额" min-width="150px">
              <template slot-scope="scope">
                <c-input-currency v-if="scope.row.oldamt > 0" v-model="scope.row.acccovamt" :currency="scope.row.concur" @handleChange="adjustLiaccvg(scope.row, 'acccovamt')" />
                <span v-else>{{moneyFormat(scope.row.acccovamt, scope.row.concur)}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="150px" fixed="right" align="center">
              <template slot-scope="scope">
                <c-button style="margin-right: 10px" size="small" type="primary" @click="call_8023(scope.row)">余额查询</c-button>
                <c-button
                  style="margin-left: 0"
                  type="primary"
                  size="small"
                  @click="onLiaccvDel(scope.$index, scope.row)"
                  v-if="scope.row.fcdinr!==null && scope.row.fcdinr!=='' && model.liaccv.addbut==='Y'"
                >
                  删除</c-button
                >
              </template>
            </el-table-column>
          </c-table>
        </c-col>
      </div>
    </div>
    <!-- 适用于保证金调整业务 -->
    <div v-if="isAdjustCcv">
      <div>
        <c-col :span="24">
          <c-table highlight-current-row @row-click="handleRowClick" :row-class-name="tableRowClassName" :border="true" :list="model.liaccv.liaccvg" :total="model.liaccv.liaccvg.length" style="width:100%,text-align:center">
            <el-table-column prop="ccdflg" label="调整标识" width="110px" type="index">
              <template slot-scope="scope">
                <c-checkbox :disabled="scope.row.ccdflgbut !== 'Y'" true-label="X" false-label="" v-model="scope.row.ccdflg" @change="checkCCv(scope.row)" style="width:100%"></c-checkbox>
              </template>
            </el-table-column>
            <el-table-column prop="ownref" label="保证金编号" width="180px">
              <template slot-scope="scope">
                <span>{{scope.row.ownref}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="cur" label="币种" width="110px">
              <template slot-scope="scope">
                <c-select :isFixed="true" v-if="scope.row.newFlag==='Y'" v-model="scope.row.cur" dbCode="acccur" @handleChange="adjustLiaccvg(scope.row, 'cur')">
                </c-select>
                <span v-else>{{scope.row.cur}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="oldamt" label="初始余额" width="110px">
              <template slot-scope="scope">
                <span>{{moneyFormat(scope.row.oldamt, scope.row.cur)}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="amt" label="变动金额" width="110px">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'liaccv.liaccvg.' + scope.$index + '.amt'">
                  <c-input-currency v-model="scope.row.amt" @handleChange="adjustLiaccvg(scope.row, 'amt')" :currency="scope.row.cur" :disabled="scope.row.ccdflg === 'X'"/>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="acc" label="保证金账号" width="190px">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'liaccv.liaccvg.' + scope.$index + '.acc'">
                  <c-input v-if="scope.row.newFlag==='Y'" v-model="scope.row.acc" @handleChange="adjustLiaccvg(scope.row, 'acc')" />
                  <span v-else>{{scope.row.acc}}</span>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="rat" label="汇率" width="150px">
              <template slot-scope="scope">
                <el-form-item label-width="0" :prop="'liaccv.liaccvg.' + scope.$index + '.rat'">
                  <c-input-currency :precision=6 v-if="scope.row.concur !== scope.row.cur" v-model="scope.row.rat" @handleChange="adjustLiaccvg(scope.row, 'rat')" />
                  <span v-else>{{scope.row.rat}}</span>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="concur" label="业务币种" width="110px"></el-table-column>
            <el-table-column prop="covamt" label="折算后金额" width="110px">
              <template slot-scope="scope">
                <span>{{moneyFormat(scope.row.covamt, scope.row.concur)}}</span>
              </template>
            </el-table-column>
            <el-table-column width="150px" fixed="right" align="center">
              <template slot-scope="scope" slot="header">
                <c-button circle style="padding: 4px" class="el-icon-plus" size="mini" @click="addLine(scope.$index, scope.row)">
                </c-button>
                <c-button circle style="padding: 4px"  class="el-icon-minus" size="mini" @click="deleteLine(scope.$index, scope.row)"></c-button>
              </template>
              <template slot-scope="scope">
                <c-button style="margin-right: 10px" size="small" type="primary" @click="call_8023(scope.row)">余额查询</c-button>
              </template>
            </el-table-column>
          </c-table>
        </c-col>
      </div>
    </div>
	</div>
</template>
<script>
import commonDepend from "~/mixin/commonDepend.js";
import Dialog from "./dialog.vue";
import { cloneDeep } from 'lodash'
import event from '../event'

export default {
  inject: ["root"],
  mixins: [commonDepend, event],
	props: ["model", "codes", "isAdjustCcv", "isAdjustCshpct", "isAdjustAmt", "xxxsetFlag"],
  components: { Dialog },
  data() {
    return {
      dialogTableVisible: false,
      columns: [
        {
          label: "编号",
          prop: "ownref",
          width: 150
        },
        {
          label: "用户",
          prop: "ownusr"
        },
        {
          label: "申请人",
          prop: "aplnam",
          width: 180
        },
        {
          label: "机构号",
          prop: "extact",
          width: 100
        },
        {
          label: "账户",
          prop: "engact",
          width: 150
        },
        {
          label: "币种",
          prop: "cur"
        },
        {
          label: "金额",
          prop: "amt"
        },
        {
          label: "开立日期",
          prop: "opndat",
          width: 100
        },
        {
          label: "标志",
          prop: "gleflg"
        },
        {
          label: "备注",
          prop: "reason"
        }
      ],
      currentSelection: {},
      relrowDisabled: true,
      dialog: {},
      index: 0,
      currentLiaccvgIndex: -1,
      activeNames: [''],
      accountlist: [],
    };
  },
  methods: {
    async checkCCv (row) {
      this.model.liaccv.liaccvg.map((item) => {
        if (item.ccdflg === 'X' && row.index !== item.index) {
          item.ccdflg = ''
        }
      })
      await this.$emit('currentChange', row.ccdflg === 'X' ? row : null);
      this.processLiaccv();
    },
    async onLiaccvAdd() {
			this.liaccvGetFcdList()
      this.dialogTableVisible = true;
    },
    async pickDepItem(row) {
			this.liaccvPick(row)
      this.dialogTableVisible = false;
    },
    async onLiaccvDel(idx, row) {
			this.liaccvDel(idx, row)
    },
    handleAdd() {
      let newRow = {
        ccdflg: "",
        ownref: "",
        cur: "",
        oldamt: 0,
        amt: 0,
        acc: "",
        rat: 0,
        concur: "",
        covamt: 0,
        acccovamt: 0
      };
      this.model.liaccv.liaccvg.push(newRow);
		},
    tableRowClassName({row, rowIndex}){
      row.index = rowIndex;
    },
    handleRowClick(row) {
      this.currentLiaccvgIndex = row.index;
    },
    addLine(index, liaccvg) {
      let newRow = {
        ccdflg: "",
        ownref: "",
        cur: "",
        oldamt: 0,
        amt: 0,
        acc: "",
        rat: 0,
        concur: this.model.liaccv.concur,
        covamt: 0,
        newFlag: "Y",
        modifySet: []
      };
      let start = 0;
      if (this.model.liaccv.liaccvg) {
        start = this.model.liaccv.liaccvg.length;
      }
      this.model.liaccv.liaccvg.splice(start, 0, newRow);
    },
    deleteLine() {
      console.log("选中的保证金下标", this.currentLiaccvgIndex);
      if (this.currentLiaccvgIndex === -1) {
        this.$notify.error({
          title: "错误",
          message: "请选择一条数据删除!"
        });
        return;
      }
      let liaccvg = this.model.liaccv.liaccvg[this.currentLiaccvgIndex];
      if (liaccvg.oldamt !== 0) {
        // 原始计算数据不能删除
        this.$notify.error({
          title: "错误",
          message: "原始计算数据,该记录无法删除!"
        });
        return;
      }
      this.model.liaccv.liaccvg.splice(this.currentLiaccvgIndex, 1);
      this.currentLiaccvgIndex = -1;
      this.processLiaccv();
    },
  }
};
</script>
<style scoped lang="less">
::v-deep .el-table th > .cell {
  padding: 0 14px;
}
</style>