<template>
  <el-dialog :close-on-click-modal="false" v-dialogDrag :title="title" :visible.sync="dialogVisible" width="80%" append-to-body :before-close="handleClose">
    <div style="height:60vh;width:100%">
      <el-form inline style="overflow: auto;height:57vh;">
        <el-form-item label="名字:">
          <div class="repayDiv">{{form.memberName}}
            <i v-if="form.sex==1" class="el-icon-male text-blue"></i>
            <i v-else class="el-icon-female text-pink"></i>
          </div>
        </el-form-item>
        <el-form-item label="手机号码:">
          <div class="repayDiv">{{form.mobilePhone}}</div>
        </el-form-item>
        <el-form-item label="总欠款:">
          <div class="repayDiv text-price text-red">{{form.debtMoney}}</div>
        </el-form-item>
        <el-form-item label="本次还款:">
          <div class="repayDiv text-price text-cyan">{{paymentMoney}}</div>
        </el-form-item>
        <el-form-item label="待付金额:">
          <div class="repayDiv text-price text-cyan">{{debtMoneys}}</div>
        </el-form-item>
        <el-form-item label="疗程付:">
          <div class="repayDiv text-price text-red">{{courseMoney}}</div>
        </el-form-item>
        <el-form-item label="充值付:">
          <div class="repayDiv text-price text-red">{{recMoney}}</div>
        </el-form-item>
        <el-form-item label="现付金额:">
          <div class="repayDiv text-price text-red">{{payMoney}}</div>
        </el-form-item>
        <el-form-item label="补单: ">
          <div class="rowformdivs ">
            <span style="cursor: pointer;" @click="openDate(recDateShow=true)" v-show="!recDateShow">{{form.additionDate }} </span>
            <el-button size="mini" type="text" @click="(recDateShow=true)" v-if="!recDateShow"><i class="el-icon-edit"></i>补单</el-button>
            <el-date-picker @blur='(recDateShow=false)' v-else v-focus ref="recDate" class="rowformdiv" v-model="form.additionDate" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="选择日期">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-alert title="选中列表进行还款" type="info" show-icon></el-alert>
        <el-table :data="list" @selection-change="selRoleChangeHand" :row-key="getRowKey" :expand-row-keys="expandRowKeys" :row-class-name="tableRowClassName" @row-click="handleRowClick" ref="multipleTable" highlight-current-row tooltip-effect="dark">
          <el-table-column type="selection" width="30" align="center"></el-table-column>
          <el-table-column type="expand" width="0">
            <template slot-scope="scope">
              <el-form inline class="margin-top-xs margin-left-xs">
                <el-form-item label='员工'>
                  <div class="flex align-center form-width-m">
                    <el-select filterable default-first-option clearable @clear="clear(scope.row,'staffList')" v-model="scope.row.staffLists" placeholder="请选择" @change="handleChange($event,scope.row,'staffList')">
                      <el-option v-for="(item,index) in options" :key="index" :label="item.label" :value="item.id">
                      </el-option>
                    </el-select>
                    <div class="el-icon-edit-outline text-xs text-cyan" @click.stop="openStaffRatio(scope.row,'staffList')"></div>
                  </div>
                </el-form-item>
                <el-form-item label='高管'>
                  <div class="flex align-center form-width-m">
                    <el-select filterable default-first-option clearable @clear="clear(scope.row,'managerList')" v-model="scope.row.managerLists" placeholder="请选择" @change="handleChange($event,scope.row,'managerList')">
                      <el-option v-for="(item,index) in managerOptions" :key="index" :label="item.label" :value="item.id">
                      </el-option>
                    </el-select>
                    <div class="el-icon-edit-outline text-xs text-cyan" @click.stop="openStaffRatio(scope.row,'managerList')"></div>
                  </div>
                </el-form-item>
                <el-form-item label='管理层'>
                  <div class="flex align-center form-width-m">
                    <el-select filterable default-first-option clearable @clear="clear(scope.row,'adminList')" v-model="scope.row.adminLists" placeholder="请选择" @change="handleChange($event,scope.row,'adminList')">
                      <el-option v-for="(item,index) in adminOptions" :key="index" :label="item.label" :value="item.id">
                      </el-option>
                    </el-select>
                    <div class="el-icon-edit-outline text-xs text-cyan" @click.stop="openStaffRatio(scope.row,'adminList')"></div>
                  </div>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="storeNum" label="门店编码" show-overflow-tooltip align="center" min-width="70"></el-table-column>
          <el-table-column prop="storeName" label="门店名称" show-overflow-tooltip align="center" min-width="80"></el-table-column>
          <el-table-column prop="docNum" show-overflow-tooltip label="单号" align="center" min-width="80"></el-table-column>
          <el-table-column prop="docDate" show-overflow-tooltip label="时间" align="center" min-width="100"></el-table-column>
          <el-table-column prop="docType" label="类型" align="center" width="70">
            <template slot-scope="scope">
              <div>{{scope.row.docType=='card_use'?'疗程':scope.row.docType=='promotion_card_use'?'促销':scope.row.liftType==1?'升降级':'正常充值'}}</div>
            </template>
          </el-table-column>
          <el-table-column prop="tradeName" show-overflow-tooltip label="名称" align="center" min-width="90"></el-table-column>
          <el-table-column prop="tradeMoney" label="成交额" align="center" width="75"></el-table-column>
          <el-table-column prop="debtMoney" label="欠款" align="center" width="75"></el-table-column>
          <el-table-column prop="paymentMoney" label="本次还款" align="center" width="75">
            <template slot-scope="scope">
              <div @click.stop>
                <div class="text-cyan" v-if="editDebtIndex!=scope.$index" @click="openEditPayMoney(scope.row,scope.$index,editDebtIndex=scope.$index)">
                  {{scope.row.paymentMoney}}
                  <i class="el-icon-edit text-cyan"></i>
                </div>
                <el-input v-focus type="number" class="form-input-width-xs" v-else @blur="endEdutPayMoney(scope.row,editDebtIndex=-1)" v-model="scope.row.paymentMoney"></el-input>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="courseMoney" label="疗程付" align="center" width="75"></el-table-column>
          <el-table-column prop="recMoney" label="充值付" align="center" width="75"></el-table-column>
          <el-table-column prop="repayMoney" label="现付" align="center" width="75"></el-table-column>
          <el-table-column v-if="headOffice" label="疗程还款" align="center" width="90">
            <template slot-scope="props">
              <!-- 疗程卡付列表 -->
              <div @click.stop="">
                <el-button size="mini" slot="reference" @click="openPro(props.row)">疗程卡包</el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column v-if="headOffice" label="充值还款" align="center" width="90">
            <template slot-scope="props">
              <!-- 充值卡付列表 -->
              <el-button size="mini" slot="reference" @click="openRec(props.row)">充值卡包</el-button>
              <!-- <div @click.stop="" v-if="props.row.docType!='card_recharge'">
              </div>
              <div v-else click.stop="" class="text-red">不能卡付</div> -->
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="90" v-if="headOffice">
            <template slot-scope="scope">
              <el-button @click.stop="cancel(scope.row)" size="mini" type="primary">取消欠款</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 现付列表 -->
        <el-form-item label="支付:">
          <div style="width:300px" class="padding-xs border border-radius-10 margin-top-xs">
            <el-table :data="payLists" :summary-method="getSummaries" show-summary>
              <el-table-column align="center" prop="payName" label="支付方式" min-width="100">
                <template slot-scope="scope"><span style="cursor: pointer;" @click="openEditMoney(scope.row,scope.$index)">{{scope.row.payName}}</span></template>
              </el-table-column>
              <el-table-column align="center" prop="payMoney" label="本次支付" min-width="80">
                <template slot-scope="scope">
                  <div class="form-input-width pointer text-cayn" v-if="payIndex!=scope.$index" @click="openEditMoney(scope.row,scope.$index,payIndex=scope.$index)" style="width:60px;cursor: pointer;margin-left:30px">
                    {{scope.row.payMoney}}
                    <i class="el-icon-edit text-cyan"></i>
                  </div>
                  <el-input v-focus class="form-input-width-xs" v-model="scope.row.payMoney" @blur="endEditMoney(scope.row,payIndex=-1)" type="number" v-else></el-input>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!-- 取消还款组件 -->
    <cancel ref="cancel" @cancelData="getData"></cancel>
    <rec ref="rec" @recData="recData"></rec>
    <pro ref="pro" @proData="proData"></pro>
    <staffratio ref="staffratio" @staffratioData="staffratioData"></staffratio>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" type="primary" plain @click="handleClose">取 消</el-button>
      <el-button size="mini" type="primary" :loading="loading" :disabled="multipleSelection.length==0" @click="confirms(loading=true)">确 定 </el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  ctlist,
  selectList,
  debtPage,
  addrepay,
  selectOne,
  addCancelDebtList,
  couAccountList,
  recAccountCashList,
   selectListType,
    QuerCompetent,
} from "@/api/eashier.js";
import cancel from "./components/cancel.vue";
import admin from "@/components/staffList/admin/index";
import manager from "@/components/staffList/manager/index";
import staffsles from "@/components/staffList/staff/index";
import rec from "./components/rec.vue";
import pro from "./components/pro.vue";
import staffratio from "@/components/staffList/staffratio/index";
export default {
  components: {
    cancel,
    staffsles,
    manager,
    admin,
    rec,
    pro,
    staffratio,
  },
  data() {
    return {
      headOffice: false, //是否总店
      staffList: [], //员工占比列表
      proList: [], //疗程支付列表
      recList: [], //充值支付列表
      proCopyList: [], //疗程支付列表初始值
      recCopyList: [], //充值支付列表初始值
      payIndex: -1, //现付列表>本次支付:下标
      editDebtIndex: -1, //欠款列表>待付金额:下标
      dialogVisible: false, //弹窗开关 //弹窗开关
      title: "还款", //弹窗标题
      expandRowKeys: [],
      list: [], //主数组
      payLists: [], //现付列表
      recDateShow: false, //是否补单 补单日期开关
      debtMoneys: 0, //总待付金额
      payMoney: 0, //总实付金额
      multipleSelection: [], //多选选中列表
      loading: false, //提交加载动画
      form: {}, //主对象
      rowIndex: -1, //欠款列表下标
      proIndex: -1, //疗程支付列表>支付次数:下标
      recIndex: -1, //充值支付列表>支付次数:下标
      recMoney: 0,
      courseMoney: 0,
      payMoney: 0,
      paymentMoney: 0,
      clickIndex: -1, //欠款列表>打开疗程/充值列表:下标
      options: [], //员工下拉选择列表
      adminOptions: [], //管理员下拉选择列表
      managerOptions: [], //高管下拉选择列表
      getRowKey(row) {
        return row.index; // 返回当前行id
      },
    };
  },
  methods: {
    openStaffRatio(row, text) {
      this.$refs.staffratio.show(row, text);
    },
    staffratioData(v) {
      console.log(v);
      this.list.forEach((item, index) => {
        if (index == v.index) {
          item[text] = v.list;
        }
      });
    },
    clear(row, text) {
      let form = { ...row };
      form[text] = [];
      console.log(form);
      this.list.splice(row.index, 1, form);
    },
    //打开充值卡付列表
    openPro(row) {
      let proList = [];
      this.list.forEach((item, index) => {
        console.log(item);
        item.proList.forEach((items) => {
          if (items.payNumber > 0) {
            items.courseRestCount = items.courseRestCount;
          } else {
            items.courseRestCount = items.courseRestCount - items.payNumber;
          }
        });
      });
      this.$refs.pro.show(row);
    },
    proData(v) {
      console.log(v);
      let form = JSON.parse(JSON.stringify(v));
      console.log(form);
      this.list.splice(form.index, 1, form);
      console.log(this.list);
      this.sumList();
      // this.restPro(form);
    },
    restPro(form) {
      if (form.courseMoney > 0) {
        var list = [];
        var listCopy = [];
        form.proList.forEach((item) => {
          item.courseConsumeCount = item.courseConsumeCount - item.payNumber;
          item.courseRestCount = item.courseRestCount - item.payNumber;
          list.push(item);
        });
        this.list.forEach((item) => {
          let proList = [];
          if (item.index != form.index) {
            item.proList.forEach((items, indexs) => {
              if (items.payNumber == 0) {
                list[indexs].payNumber = 0;
                list[indexs].payMoney = 0;
                items = list[indexs];
              }
              proList.push(items);
            });
            item.proList = proList;
          }
          listCopy.push(item);
        });
        this.list = listCopy;
        console.log(this.list);
      }
    },
    openRec(row) {
      this.$refs.rec.show(row);
    },
    recData(v) {
      let form = JSON.parse(JSON.stringify(v));
      this.list.splice(form.index, 1, form);
      this.sumList();
      // this.restRec(form);
    },
    restRec(form) {
      if (form.recMoney > 0) {
        var list = [];
        form.recList.forEach((item) => {
          item.cardRestMoney = item.cardRestMoney - item.payMoney;
          item.cardConsumeMoney = item.cardConsumeMoney - item.payMoney;
          list.push(item);
        });
        this.list.forEach((item) => {
          let recList = [];
          if (item.index != form.index) {
            item.recList.forEach((items, indexs) => {
              if (items.payMoney == 0) {
                list[indexs].payMoney = 0;
                items = list[indexs];
              }
              recList.push(items);
              console.log(recList);
            });
            item.recList = recList;
          }
        });
      }
    },
    //关闭弹窗前的回调方法
    handleClose() {
      this.form = Object.assign({});
      this.debtMoneys = 0;
      this.payMoney = 0;
      this.tradeMoney = 0;
      this.payLists = [];
      this.list = [];
      this.multipleSelection = [];
      this.staffList = [];
      this.dialogVisible = false;
    },
    //打开取消还款弹窗
    cancel(row) {
      delete row.adminList;
      delete row.entities;
      delete row.managerList;
      delete row.docDate;
      delete row.adminList;
      this.$refs.cancel.show(row);
    },

    //组件传值
    show(form) {
      Object.assign(this.$data, this.$options.data.call(this));
      this.form = { ...form };
      this.dialogVisible = true;
      this.headOffice =
        sessionStorage.getItem("headOffice") * 1 == 1 ? true : false;
      this.getData();
    },
    //获取会员疗程卡包 充值卡包
    getPage() {
      // 充值卡包
      recAccountCashList({
        memberId: this.form.id,
      }).then((res) => {
        if (res.code == "000000") {
          let recCopyList = [];
          res.rows.forEach((item) => {
            item.cardOver = item.cardRestMoney;
            item.payMoney = 0;
            if (item.cardRestMoney == item.cardConsumeMoney) {
              recCopyList.push(item);
            }
          });
          this.recCopyList = recCopyList;
        } else {
          this.$alert(res.message, "查询会员卡包", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
      // 疗程卡包
      couAccountList({ memberId: this.form.id }).then((res) => {
        if (res.code == "000000") {
          let proCopyList = [];
          res.rows.forEach((item) => {
            item.payMoney = 0;
            item.payNumber = 0;
            item.payMoney = (item.payNumber * item.price).toFixed(2) * 1;
            if (item.courseRestCount == item.courseConsumeCount) {
              proCopyList.push(item);
            }
          });
          this.proCopyList = proCopyList;
        } else {
          this.$alert(res.message, "查询会员疗程", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
    //获取数据
    async getData() {
      this.getPage();
      //获取员工列表
      selectList({ id: "" }).then((res) => {
        if (res.code == "000000") {
          res.rows.forEach((item) => {
            item.label = item.brandNumber + "-" + item.staffName;
            delete item.state;
            item.ratio = 100;
          });
          this.options = Array.from(new Set(res.rows));
        } else {
          this.$alert(res.message, "加载员工列表", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
      //获取员工列表
      selectListType({ type: 0 }).then((res) => {
        if (res.code == "000000") {
          res.rows.forEach((item) => {
            item.label = item.brandNumber + "-" + item.staffName;
            item.ratio = 100;
            delete item.state;
          });

          this.adminOptions = res.rows;
        } else {
          this.$alert(res.message, "加载员工提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
      //获取高管列表
      QuerCompetent({ type: 0, pageSize: 999 }).then((res) => {
        if (res.code == "000000") {
          res.pageInfo.list.forEach((item) => {
            item.label = item.brandNumber + "-" + item.staffName;
            item.ratio = 100;
            delete item.state;
          });
          this.managerOptions = res.pageInfo.list;
        } else {
          this.$alert(res.message, "加载高管提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
      //获取现付列表
      ctlist({ id: "" }).then((res) => {
        if (res.code == "000000") {
          res.rows.forEach((item) => {
            item.payMoney = 0;
            item.payName = item.collectionTypeName;
            item.payId = item.id;
          });
          this.payLists = res.rows;
        }
      });
      let form = {
        pageSize: 99,
        memberId: this.form.id,
        state: 0,
        storeId: this.form.storeId,
      };
      //获取欠款列表
      debtPage(form).then((res) => {
        if (res.code == "000000") {
          let list = [];
          res.pageInfo.list.forEach((element) => {
            element.yetMoney = element.tradeMoney - element.debtMoney;
            if (element.docType == "card_use") {
              element.type = "疗程";
            }
            if (element.docType == "card_recharge") {
              element.type = "充值";
            }
            element.entities.forEach((item) => {
              item.staffName = item.saleStaffName;
              item.id = item.saleStaffId;
              item.identityCard = item.saleIdentityCard;
              item.ratio = item.saleRatio;
              item.ratioShow = false;
              if (item.id) {
                item.staffNames = item.brandNumber + "--" + item.staffName;
              }
            });
            element.staffList = element.entities;
            selectOne({ type: 7, orderId: element.id }).then((res) => {
              if (res.code == "000000") {
                var managerList = [];
                var adminList = [];
                res.rows.forEach((item) => {
                  if (item.staffType == 0) {
                    item.staffNames = item.brandNumber + "--" + item.staffName;
                    managerList.push(item);
                  }
                  if (item.staffType == 1) {
                    item.staffNames = item.brandNumber + "--" + item.staffName;
                    adminList.push(item);
                  }
                });
                if (managerList.length == 0) {
                  managerList.push({ ratio: 100 });
                }
                if (adminList.length == 0) {
                  adminList.push({ ratio: 100 });
                }
                if (element.entities.length == 0) {
                  element.entities.push({ ratio: 100 });
                  element.staffList.push({ ratio: 100 });
                }
                element.staffList.forEach((items, index) => {
                  if (items.id) {
                    if (index == 0) {
                      element.staffLists =
                        items.brandNumber + "-" + items.staffName;
                    }
                  } else {
                    element.staffLists = "";
                  }
                });
                adminList.forEach((items, index) => {
                  if (items.id) {
                    if (index == 0) {
                      element.adminLists =
                        items.brandNumber + "-" + items.staffName;
                    }
                  } else {
                    element.adminLists = "";
                  }
                });
                managerList.forEach((items, index) => {
                  if (items.id) {
                    if (index == 0) {
                      element.managerLists =
                        items.brandNumber + "-" + items.staffName;
                    }
                  } else {
                    element.managerLists = "";
                  }
                });
                var form = {
                  debtId: element.id,
                  storeName: element.storeName,
                  storeNum: element.storeNum,
                  memberId: element.memberId,
                  memberName: element.memberName,
                  memberIdentityCard: element.memberIdentityCard,
                  mobilePhone: element.mobilePhone,
                  docId: element.docId,
                  docNum: element.docNum,
                  docDate: element.docDate,
                  docType: element.docType,
                  tradeId: element.tradeId,
                  tradeName: element.tradeName,
                  tradeMoney: element.tradeMoney,
                  yetMoney: element.yetMoney,
                  debtMoney: element.debtMoney,
                  accountId: element.accountId,
                  paymentMoney: element.debtMoney,
                  managerLists: element.managerLists,
                  adminLists: element.adminLists,
                  staffLists: element.staffLists,
                  recMoney: 0,
                  courseMoney: 0,
                  repayMoney: element.debtMoney,
                  entities: element.entities,
                  staffList: element.staffList,
                  salesAchievement: element.salesAchievement,
                  salesCommission: element.salesCommission,
                  cardSalesAchievement: element.cardSalesAchievement,
                  cardSalesCommission: element.cardSalesCommission,
                  typeRatio: element.typeRatio,
                  adminList: adminList,
                  managerList: managerList,
                  cancelDebtMxes: element.cancelDebtMxes,
                };
                var that = this;
                setTimeout(() => {
                  form.proList = that.proCopyList;
                  form.recList = that.recCopyList;
                  console.log(form);
                  list.push(form);
                }, 300);
              } else {
                this.$alert(res.message, "加载高管", {
                  confirmButtonText: "确定",
                  confirmButtonClass: "confirmbtnFalses",
                  type: "warning",
                  center: true,
                  callback: (action) => {},
                });
              }
            });
          });
          this.list = list;
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
    //返回当前行的下标
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    //确定返回方法
    confirms() {
      if (this.form.additionDate) {
        this.form.isAddition = 1;
      } else {
        this.form.isAddition = 0;
      }
      let payLists = [];
      let debtMoney = 0;
      this.payLists.forEach((item) => {
        if (item.payMoney > 0) {
          let items = {
            payId: item.payId,
            payName: item.payName,
            payMoney: item.payMoney,
          };
          payLists.push(items);
        }
      });
      delete this.form.moneys;
      this.form.paymentMoney = this.payMoney;
      this.form.payMoney = this.payMoney;
      let repayAccountListRequests = [];
      console.log(this.multipleSelection);
      this.multipleSelection.forEach((item) => {
        console.log(item);
        debtMoney += item.debtMoney;
        item.payMoney = item.paymentMoney;
        item.paymentMoney = item.paymentMoney;
        item.entities = [...item.staffList];
        item.proList.forEach((items) => {
          if (items.payNumber > 0 || items.payMoney > 0) {
            let pro = {
              accountName: items.courseProjectName,
              accountNum: items.courseProjectNum,
              payNumber: items.payNumber,
              payMoney: items.payMoney,
              accountType: 1,
              accountId: items.id,
              storeId: items.storeId,
              storeName: items.storeName,
            };
            repayAccountListRequests.push(pro);
          }
        });
        item.recList.forEach((items) => {
          if (items.payMoney > 0) {
            let rec = {
              accountName: items.rechargeTypeName,
              accountNum: items.rechargeTypeNum,
              payMoney: items.payMoney,
              accountType: 0,
              accountId: items.id,
              storeId: items.storeId,
              storeName: items.storeName,
            };
            repayAccountListRequests.push(rec);
          }
        });
        item.repayAccountListRequests = repayAccountListRequests;
        delete item.type;
        delete item.docDate;
        delete item.yetMoney;
      });
      this.form.debtMoney = debtMoney;
      if (this.debtMoneys != 0) {
        this.$message.error({
          message: "还存在欠款, 不能进行还款 !",
        });
        this.loading = false;
      } else {
        this.form.payLists = payLists;
        let repayTradeLists = [];
        this.multipleSelection.forEach((item) => {
          item.staffRequests = item.entities;
          let salesmens = [];
          item.managerList.forEach((item) => {
            if (item.id) {
              item.staffType = 0;

              salesmens.push(item);
            }
          });
          item.adminList.forEach((item) => {
            if (item.id) {
              item.staffType = 1;
              salesmens.push(item);
            }
          });
          item.salesmens = salesmens;
          repayTradeLists.push(item);
        });
        delete this.form.debtMoneys;
        this.form.repayTradeLists = repayTradeLists;
        addrepay(this.form).then((res) => {
          this.loading = false;
          if (res.code == "000000") {
            this.$message.success({
              message: res.message,
            });
            this.dialogVisible = false;
            this.$emit("repaymentData");
          } else {
            this.$alert(res.message, "提示", {
              confirmButtonText: "确定",
              confirmButtonClass: "confirmbtnFalses",
              type: "warning",
              center: true,
              callback: (action) => {},
            });
          }
        });
      }
    },

    //打开每张欠款单支付的金额
    openEditPayMoney(row, index) {
      row.paymentMoney = row.paymentMoney * 1;
    },
    //关闭每张欠款单支付的金额
    endEdutPayMoney(row) {
      row.paymentMoney = row.paymentMoney * 1;
      row.repayMoney =
        (row.paymentMoney - row.courseMoney - row.recMoney).toFixed(2) * 1;
      this.multipleSelection = this.noRepeat(this.multipleSelection);
      this.sumList();
    },
    //选择单据选中
    selRoleChangeHand(row) {
      this.multipleSelection = row;
      this.multipleSelection = this.noRepeat(this.multipleSelection);
      console.log(this.multipleSelection);
      this.multipleSelection.forEach((item) => {
        this.$nextTick(() => {
          this.expandRowKeys.push(item.index);
        });
        this.handleRowClick(item);
      });
      this.sumList(1);
    },
    //点击行触发，选中或不选中复选框
    handleRowClick(row) {
      this.rowIndex = row.index;
      this.$nextTick(() => {
        this.$refs.multipleTable.toggleRowExpansion(row);
      });
      this.sumList(1);
      var that = this;
      setTimeout(() => {
        this.$nextTick(() => {
          if (that.$refs.staffsles) {
            that.$refs.staffsles.show(row.entities);
            that.$refs.manager.show(row.managerList);
            that.$refs.admin.show(row.adminList);
          }
        });
      }, 200);
    },
    //打开修改现付金额
    openEditMoney(row, index) {
      if (this.form.numMoney != 0) {
        row.payMoney = (row.payMoney * 1 + this.debtMoneys).toFixed(2) * 1;
      }
    },
    //关闭修改现付金额
    endEditMoney(row) {
      var number = 0;
      if (row.payMoney) {
        row.payMoney = row.payMoney * 1;
      } else {
        row.payMoney = 0;
      }
      this.multipleSelection = this.noRepeat(this.multipleSelection);
      this.multipleSelection.forEach((item) => {
        number += item.repayMoney * 1;
      });
      let num = 0;
      this.payLists.forEach((item) => {
        num += item.payMoney * 1;
      });
      this.payMoney = num;
      this.debtMoneys = (this.debtMoneys - num).toFixed(2) * 1;
      if (this.payMoney != number) {
        this.debtMoneys = (number - this.payMoney).toFixed(2) * 1;
      } else {
        this.debtMoneys = 0;
      }
    },
    sumList() {
      let course = 0;
      let rec = 0;
      let pay = 0;
      var payMoney = 0;
      let paymentMoney = 0;
      this.multipleSelection = this.noRepeat(this.multipleSelection);
      this.multipleSelection.forEach((item) => {
        payMoney += item.repayMoney * 1;
        course += item.courseMoney * 1;
        rec += item.recMoney * 1;
        pay += item.repayMoney * 1;
        paymentMoney += item.paymentMoney * 1;
      });
      this.courseMoney = course.toFixed(2) * 1;
      this.recMoney = rec.toFixed(2) * 1;
      this.payMoney = pay.toFixed(2) * 1;
      this.paymentMoney = paymentMoney.toFixed(2) * 1;
      this.debtMoneys = 0;
      this.debtMoneys = payMoney;
    },
    //数组按对应字段合计返回方法
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (column.property == "payMoney") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += " ";
        } else {
          sums[index] = "--";
        }
      });

      return sums;
    },
  },
  mounted() {
    this.$nextTick(function () {
      this.$on("list", function () {
        //console.log('我是子组件方法');
      });
    });
  },
};
</script>

<style scoped>
.repayDiv {
  width: 100px;
}
.setting {
  border-bottom: #333 1px solid;
}
.setting .el-form-item {
  margin: 0;
  padding: 0;
}
.setting .el-form-item__content {
  line-height: 50px;
}
.setting i {
  display: none;
}
.el-table__expand-icon {
  display: none !important;
}
</style>
