<template>
  <div>
    <el-form inline style="min-height:50vh" label-width="80px">
      <div class="flex justify-between align-center">
        <div class="flex">
          <el-form-item label="顾客类型:">
            <div class="form-width-xs flex align-center">
              <el-switch style="display: inline-block" v-model="form.cashNumber" active-color="#13ce66" inactive-color="#ff4949" @change="sumMoney">
              </el-switch>
              <div class="padding-left-xs">{{form.cashNumber?'会员':'散客'}}</div>
            </div>
          </el-form-item>
          <el-form-item label="手机号码:">
            <el-input v-if="!form.cashNumber" maxlength="11" type="number" class="form-width-ms" v-model="form.mobilePhone"></el-input>
            <div v-else class="form-width text-cyan">
              <el-input maxlength="11" v-model="form.mobilePhone" @change="query" @blur="query">
                <el-button size="mini" slot="append" icon="el-icon-search" @click="openMember"></el-button>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item label="名字:">
            <el-input v-if="!form.cashNumber" class="form-width-ms border-only-bottom border" v-model="form.memberName"></el-input>
            <div v-else class="form-width-xs">
              {{form.memberName}}
              <i v-if="form.sex==1" class="el-icon-male text-blue"></i>
              <i v-if="form.sex==0" class="el-icon-female text-pink"></i>
            </div>
          </el-form-item>
          <!-- <el-form-item label="手工单号:">
            <el-input class="form-width-ms border-only-bottom border" v-model="form.orderNums"></el-input>
          </el-form-item> -->
          <el-form-item label="男客:">
            <el-input-number class="form-width-xs" size="small" v-model="form.man" :min="0" :max="10" label="男客数量"></el-input-number>
          </el-form-item>
          <el-form-item label="女客:">
            <el-input-number class="form-width-xs" size="small" v-model="form.woman" :min="0" :max="10" label="女客数量"></el-input-number>
          </el-form-item>
        </div>
        <div>
          <!-- <el-form-item label="单据日期:">
            <el-input class="form-width-ms border-only-bottom border" v-model="form.orderDate"></el-input>
          </el-form-item> -->
          <!-- <el-form-item label="单据号:">
            <el-input class="form-width-ms border-only-bottom border" v-model="form.orderNum"></el-input>
          </el-form-item> -->
        </div>
      </div>
      <el-table :data="list" border :row-class-name="rowClassName" stripe style="width: 100%" :header-cell-style=" {background: 'linear-gradient(#6cb3ff, #1873d4)' , color: '#eeeeee' , }">
        <el-table-column align="center" prop="projectNum" label="编码" min-width="80" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" prop="projectName" label="名称" min-width="100" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" prop="transactionPrice" label="价格" min-width="70" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>
              <span class="text-red text-price">{{scope.row.transactionPrice }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="numberTimes" label="数量" min-width="89" show-overflow-tooltip>
          <!-- <template slot-scope="scope">
            <el-input-number style="width:80px" v-model="scope.row.numberTimes" size="mini" :min="1" :max="10" label="数量"></el-input-number>
          </template> -->
        </el-table-column>
        <el-table-column align="center" prop="amount" label="总价格" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>
              <span class="text-red text-price">{{scope.row.amount }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="noDiscount" label="卡付打折" min-width="70" show-overflow-tooltip>
          <template slot-scope="scope">
            {{
            scope.row.noDiscount == 0 ? "是"  :  scope.row.noDiscount == 1?'否':"未设"
          }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="renew" label="加钟" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-switch disabled v-model="scope.row.renew==1?true:false" @change="renewChange($event,scope.row,scope.$index)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="isClock" label="点牌" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-switch disabled v-model="scope.row.isClock==1?true:false">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="projectNum" label="时长" min-width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <div>
              <i class="text-green text-bold el-icon-timer"></i>
              <span>{{scope.row.servicingTime}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="serviceTechnicianOneStaffName" label="员工姓名" min-width="75" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" prop="staffName" label="推销员工" min-width="75" show-overflow-tooltip></el-table-column>
        <el-table-column align="center" prop="price" label="备注" min-width="90" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input v-model="scope.row.beiz"></el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="疗程付款" v-if="form.cashNumber">
          <el-table-column align="center" prop="courseProjectName" label="疗程付款" min-width="100" show-overflow-tooltip v-if="form.cashNumber">
            <template slot-scope="scope">
              <div @click="openPro(scope.row,scope.$index)">
                <span class="text-red">{{scope.row.courseProjectName?scope.row.courseProjectName:'点击选择'}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="courseConsumeCount" label="次数" min-width="70"></el-table-column>
        </el-table-column>
        <el-table-column align="center" label="充值卡付" v-if="form.cashNumber">
          <el-table-column align="center" prop="rechargeTypeName" label="充值卡款" min-width="100" show-overflow-tooltip v-if="form.cashNumber">
            <template slot-scope="scope">
              <div @click="openRec(scope.row,scope.$index)">
                <span class="text-red">{{scope.row.rechargeTypeName ?(scope.row.rechargeTypeName + ' / 折扣'+scope.row.discount+'%'):'点击选择'}}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="cardConsumeMoney" label="可用金额" min-width="80" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="spendMoney" label="本次支付" min-width="80" show-overflow-tooltip>
            <template slot-scope="scope">
              <div class="form-width-xl">
                <div v-if="spendIndex!=scope.$index" class="text-red" @click="openSpendMoney(scope.row,scope.$index)">
                  {{scope.row.spendMoney}}
                  <i class="el-icon-edit text-cyan"></i>
                </div>
                <el-input style="width:80%" v-model="scope.row.spendMoney" type="number" v-focus v-else @blur="endSpendMoney(scope.row,scope.$index)" @change="endSpendMoney(scope.row,scope.$index)"></el-input>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="cardGiftMoney" label="赠送可用" min-width="80" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="giftSpendMoney" label="赠送支付" min-width="80" show-overflow-tooltip></el-table-column>
        </el-table-column>
      </el-table>
      <!-- <el-form-item label="原价:">
        <div class="text-red text-price">0.00</div>
      </el-form-item>
      <el-form-item label="折前:">
        <div class="text-red text-price">0.00</div>
      </el-form-item>
      <el-form-item label="折后:">
        <div class="text-red text-price">0.00</div>
      </el-form-item>
      <el-form-item label="消费备注:">
      </el-form-item> -->
      <el-form-item label="应收:">
        <div class="text-red text-price">{{form.amount | money}}</div>
      </el-form-item>
    </el-form>
    <div class="bing-foot  flex justify-between align-center">
      <div class="padding-left">现付</div>
      <div class="flex align-center padding-right">
        <span style="margin-top:5px" class="text-red text-bold text-price text-xl margin-right">{{form.payMoney | money}}</span>
        <el-button type="primary" @click="openPay">去结算</el-button>
      </div>
    </div>
    <el-drawer title="现付收银" :visible.sync="openDrawer" direction="rtl">
      <div class="padding-left-xs text-xs text-bold">
        待付金额 <span class="text-red text-price">{{form.debtMoney | money}}</span>
        <span class="padding-left-xs text-xs text-bold"><span class="padding-right">现付</span> <span class="text-red text-price">{{form.collectionCashAmount | money}}</span> </span>
      </div>
      <el-table class="margin-top" :data="payLists" :summary-method="getSummaries" show-summary>
        <el-table-column align="center" prop="payName" label="支付方式" min-width="100">
          <template slot-scope="scope">
            <div class="padding-xs">{{scope.row.payName}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="payMoney" fixed="right" label="本次支付" width="150">
          <template slot-scope="scope">
            <div class="form-input-width" v-if="editIndex!= scope.$index" @click="editPay(scope.row, scope.$index)">
              {{ scope.row.payMoney}}
              <i class="el-icon-edit text-cyan"></i>
            </div>
            <el-input v-else v-focus size="samll" @blur="endEdit(scope.row, scope.$index)" @change="endEdit(scope.row, scope.$index)" v-model="scope.row.payMoney" class="form-input-width-xs" type="number"></el-input>
          </template>
        </el-table-column>
      </el-table>
      <div class="text-center margin-top">
        <el-button type="primary" @click="confirm">确认收银</el-button>
      </div>
    </el-drawer>
    <member ref="member" @memberData="memberData"></member>
    <project ref="project" @projectData="projectData"></project>
    <reccard ref="reccard" @recTypeData="recTypeData"></reccard>
  </div>
</template>

<script>
import { ctlist, storeMemberOne } from "@/api/eashier.js";
import { addServiceOrders } from "@/api/api.js";
import project from "@/components/project/index";
import member from "@/components/member/index";
import reccard from "@/components/recharge/index";
export default {
  components: {
    member,
    project,
    reccard,
  },
  data() {
    return {
      form: {
        orderNum: 1011001,
        woman: 0,
        man: 0,
        number: 1,
        amount: 0,
        collectionCashAmount: 0,
        payMoney: 0,
        debtMoney: 0,
        cashNumber: true,
      },
      openDrawer: false,
      editIndex: -1,
      rowIndex: -1,
      spendIndex: -1,
      payLists: [], //现付列表
      list: [], //主数组
      useRecharge:
        JSON.parse(sessionStorage.getItem("sessionStorageData")).useRecharge *
        1,
      balance:
        JSON.parse(sessionStorage.getItem("sessionStorageData")).balance * 1 +
        0.01,
    };
  },
  methods: {
    // 本次支付显示输入框
    openSpendMoney(row, index) {
      if (row.rechargeTypeId) {
        this.spendIndex = index;
        let form = this.toSpend(row, index);
        this.list.splice(index, 1, form);
        this.sumSpend();
      }
    },
    toSpend(item, index) {
      let form = { ...item };
      item.cashAmount =
        (
          item.collectionCashAmount +
          item.collectionIntegralAmount +
          item.collectionRechargeAmount
        ).toFixed(2) * 1;
      let debtMoney = (item.amount - item.cashAmount).toFixed(2) * 1;
      let cardMoney = (form.offsetMoney + form.offsetGiftMoney).toFixed(2) * 1;
      if (debtMoney > 0) {
        if (cardMoney < debtMoney) {
          form.spendMoney = form.cardOver;
          form.giftSpendMoney = form.giftConsumeMoney;
          form.discoutMoney = form.offsetMoney;
          form.discoutGiftMoney = form.offsetGiftMoney;
        } else {
          if (form.type == 2 || form.type == 0) {
            if (this.useRecharge == 2) {
              //先用充值再用赠送
              if (form.noDiscount == 0) {
                if (form.offsetMoney > debtMoney) {
                  form.spendMoney =
                    ((debtMoney * form.discount) / 100).toFixed(2) * 1;
                  form.discoutMoney = debtMoney;
                } else {
                  form.spendMoney = form.cardOver;
                  form.discoutMoney =
                    (form.cardOver * (form.discount / 100)).toFixed(2) * 1;

                  form.giftSpendMoney =
                    (
                      (debtMoney - form.discoutMoney) *
                      (form.discountFreeAmount / 100)
                    ).toFixed(2) * 1;
                  form.discoutGiftMoney =
                    (debtMoney - form.discoutMoney).toFixed(2) * 1;
                }
              } else {
                console.log("不打折");
                if (form.offsetMoney > debtMoney) {
                  form.spendMoney = debtMoney;
                  form.discoutMoney =
                    ((debtMoney * form.discount) / 100).toFixed(2) * 1;
                } else {
                  form.spendMoney = form.cardOver;
                  form.discoutMoney = form.cardOver.toFixed(2) * 1;
                  form.giftSpendMoney =
                    (debtMoney - form.discoutMoney).toFixed(2) * 1;
                  form.discoutGiftMoney =
                    (debtMoney - form.discoutMoney).toFixed(2) * 1;
                }
              }
            } else {
              //按比例使用
              let sunRatio =
                (form.cardRestMoney + form.cardGiftOvers).toFixed(2) * 1;
              let recRatio = (form.cardRestMoney / sunRatio).toFixed(2) * 1;
              let recGiftRatio = (form.cardGiftOvers / sunRatio).toFixed(2) * 1;
              if (form.offsetMoney > debtMoney * recRatio) {
                if (form.noDiscount == 0) {
                  form.spendMoney =
                    (debtMoney * recRatio * (form.discount / 100)).toFixed(2) *
                    1;
                  form.discoutMoney = (debtMoney * recRatio).toFixed(2) * 1;
                  form.giftSpendMoney =
                    (
                      debtMoney *
                      recGiftRatio *
                      (form.discountFreeAmount / 100)
                    ).toFixed(2) * 1;
                  form.discoutGiftMoney =
                    (debtMoney * recGiftRatio).toFixed(2) * 1;
                } else {
                  form.spendMoney = (debtMoney * recRatio).toFixed(2) * 1;
                  form.discoutMoney = (debtMoney * recRatio).toFixed(2) * 1;
                  form.giftSpendMoney =
                    (debtMoney * recGiftRatio).toFixed(2) * 1;
                  form.discoutGiftMoney =
                    (debtMoney * recGiftRatio).toFixed(2) * 1;
                }
              } else {
                form.spendMoney = form.cardOver;
                form.discoutMoney =
                  (form.cardOver / (form.discount / 100)).toFixed(2) * 1;
                form.giftSpendMoney =
                  debtMoney * recRatio -
                  form.discoutMoney +
                  (debtMoney * recGiftRatio).toFixed(2) * 1;
                form.discoutGiftMoney =
                  (
                    debtMoney * recRatio -
                    form.discoutMoney +
                    (debtMoney * recGiftRatio) / (form.discountFreeAmount / 100)
                  ).toFixed(2) * 1;
              }
            }
            if (form.spendMoney == 0) {
              form.giftSpendMoney = 0;
              form.discoutMoney = 0;
              form.discoutGiftMoney = 0;
            }
          } else {
            if (form.noDiscount == 0) {
              // 可以打折
              form.spendMoney =
                ((debtMoney * form.discount) / 100).toFixed(2) * 1;
            } else {
              // 不可以打折
              form.spendMoney = debtMoney;
            }
            form.discoutMoney = debtMoney;
          }
        }
        form.collectionRechargeAmount =
          (form.discoutGiftMoney + form.discoutMoney).toFixed(2) * 1;
      } else {
        form.spendMoney = 0;
        form.collectionRechargeAmount = 0;
        form.giftSpendMoney = 0;
      }
      return form;
    },
    sumSpend() {
      let payMoney = 0;
      this.list.forEach((item) => {
        console.log(item);
        if (item.courseAccountId == null) {
          if (item.spendMoney) {
            item.collectionRechargeAmount =
              (item.discoutGiftMoney + item.discoutMoney).toFixed(2) * 1;
          } else {
            item.collectionRechargeAmount = 0;
          }
          item.cashAmount =
            (
              item.collectionCashAmount +
              item.collectionRechargeAmount +
              item.collectionIntegralAmount
            ).toFixed(2) * 1;
          item.collectionAmount =
            (
              item.collectionCashAmount +
              item.spendMoney +
              item.collectionIntegralAmount
            ).toFixed(2) * 1;
        } else {
          item.cashAmount = item.amount;
          item.collectionAmount = item.amount;
        }
        console.log(item.collectionRechargeAmount);
        console.log(item.cashAmount);
        payMoney += (item.amount - item.cashAmount).toFixed(2) * 1;
      });
      this.form.payMoney = payMoney;
    },
    endSpendMoney(row, index) {
      console.log(row.spendMoney);
      let form = { ...row };
      form.spendMoney = form.spendMoney ? form.spendMoney * 1 : 0;
      form.giftSpendMoney = form.giftSpendMoney ? form.giftSpendMoney * 1 : 0;
      if (form.noDiscount == 0) {
        form.discoutMoney =
          ((form.spendMoney / form.discount) * 100).toFixed(2) * 1;
        if (form.type == 2) {
          form.discoutGiftMoney =
            ((form.giftSpendMoney / form.discountFreeAmount) * 100).toFixed(2) *
            1;
        } else {
          form.discoutGiftMoney = form.giftSpendMoney;
        }
      } else {
        form.discoutMoney = form.spendMoney;
        form.discoutGiftMoney = form.giftSpendMoney;
      }
      console.log(form);
      this.spendIndex = -1;
      this.list.splice(index, 1, form);
      this.sumSpend();
    },
    //打开选择会员弹窗
    openMember() {
      this.cashNumber = true;
      this.$refs.member.show();
    },
    //选中会员返回方法
    memberData(v) {
      let form = { ...this.form };
      form.mobilePhone = v.mobilePhone;
      form.memberName = v.memberName;
      form.memberId = v.id;
      form.id = v.id;
      form.sex = v.sex;
      form.cashNumber = true;
      this.form = form;
    },
    openPro(row, index) {
      this.rowIndex = index;
      this.$refs.project.show(this.form, row.projectId);
    },
    projectData(v) {
      let form = { ...this.list[this.rowIndex] };
      form.courseProjectName = v.courseProjectName;
      form.transactionPrice = v.price;
      form.amount = (v.price * form.numberTimes).toFixed(2) * 1;
      form.courseConsumeCount = 1;
      form.courseAccountId = v.id;
      this.list.splice(this.rowIndex, 1, form);
      this.sumSpend();
    },
    openRec(row, index) {
      if (row.courseAccountId == null) {
        this.rowIndex = index;
        this.$refs.reccard.show(this.form);
      } else {
        this.$message.warning({ message: "已选择疗程付" });
      }
    },
    recTypeData(v) {
      let form = { ...this.list[this.rowIndex] };
      this.list.forEach((item) => {
        if (item.spendMoney > 0 && item.rechargeTypeId == v.id) {
          v.cardRestMoney = v.cardRestMoney - item.spendMoney;
          v.cardConsumeMoney = v.cardConsumeMoney - item.spendMoney;
          v.cardGiftMoney = v.cardGiftMoney - item.giftSpendMoney;
          v.giftConsumeMoney = v.giftConsumeMoney - item.giftSpendMoney;
        }
      });
      v.cardGiftOvers = 0;
      v.spendMoney = 0;
      v.giftSpendMoney = 0;
      v.discoutMoney = 0;
      v.discoutGiftMoney = 0;
      v.collectionRechargeAmount = 0;
      if (v.type == 0) {
        v.offsetMoney = v.cardConsumeMoney;
        v.offsetGiftMoney = v.giftConsumeMoney;
      }
      if (v.type == 1) {
        if (form.noDiscount == 0) {
          v.offsetMoney = v.cardConsumeMoney;
          v.offsetGiftMoney = 0;
        } else {
          v.offsetMoney =
            (v.cardConsumeMoney / (v.discount / 100)).toFixed(2) * 1;
          v.offsetGiftMoney = 0;
        }
      }
      if (v.type == 2) {
        if (form.noDiscount == 0) {
          v.offsetMoney = v.cardConsumeMoney;
          v.offsetGiftMoney = 0;
        } else {
          v.offsetMoney =
            (v.cardConsumeMoney / (v.discount / 100)).toFixed(2) * 1;
          v.offsetGiftMoney =
            (v.giftConsumeMoney / (v.discountFreeAmount / 100)).toFixed(2) * 1;
        }
      }
      form.offsetMoney = v.offsetMoney;
      form.offsetGiftMoney = v.offsetGiftMoney;
      form.rechargeTypeNum = v.rechargeTypeNum;
      form.spendMoney = 0;
      form.rechargeTypeId = v.id;
      form.rechargeTypeName = v.rechargeTypeName;
      form.cardRestMoney = v.cardRestMoney;
      form.cardConsumeMoney = v.cardConsumeMoney;
      form.discount = v.discount;
      form.cardGiftMoney = v.cardGiftMoney;
      form.discountFreeAmount = v.discountFreeAmount;
      form.giftConsumeMoney = v.giftConsumeMoney;
      form.endTime = v.endTime;
      form.discoutGiftMoney = 0;
      form.discoutMoney = 0;
      form.giftSpendMoney = 0;
      form.type = v.type;
      let row = this.toSpend(form, this.rowIndex);
      form = { ...form, ...row };
      this.list.splice(this.rowIndex, 1, form);
      this.sumSpend();
    },
    //查询方法
    query() {
      storeMemberOne({
        mobilePhone: this.form.mobilePhone,
        storeId: sessionStorage.getItem("parentId"),
      }).then((res) => {
        if (res.code == "000000") {
          this.form = { ...this.form, ...res.data };
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => { },
          });
        }
      });
    },
    //确定返回方法
    confirm() {
      if (this.form.debtMoney > 0 || this.form.debtMoney < 0.5) {
        let payLists = [];
        this.payLists.forEach((item) => {
          if (item.payMoney > 0) {
            payLists.push(item);
          }
        });
        this.list.forEach((item) => {
          item.memberName = this.form.memberName;
          item.mobilePhone = this.form.mobilePhone;
          if (this.form.memberId) {
            item.memberId = this.form.memberId;
            item.sex = this.form.sex;
          }
          if (item.courseAccountId != null) {
          } else {
            item.collectionCashAmount =
              (
                item.amount -
                item.collectionIntegralAmount -
                item.collectionRechargeAmount
              ).toFixed(2) * 1;
          }
          item.collectionAmount =
            (
              item.collectionCashAmount +
              item.collectionIntegralAmount +
              item.collectionRechargeAmount
            ).toFixed(2) * 1;
          item.cashAmount = item.collectionAmount;
        });
        this.list[0].payLists = payLists;
        this.addCash();
      } else {
        this.$message.warning({ message: "请支付全部的金额才能收银 !" });
      }
    },
    addCash() {
      let list = [];

      this.list.forEach((item) => {
        let rechargeAccountList = [];
        let rechargeAccountLists = [];
        if (this.form.cashNumber == true) {
          item.cashNumber = 1;
        } else {
          item.cashNumber = 0;
        }
        if (item.isClock == true) {
          item.isClock = 1;
        } else {
          item.isClock = 0;
        }
        if (item.spendMoney > 0) {
          let form = {
            id: item.rechargeTypeId,
            rechargeTypeNum: item.rechargeTypeNum,
            rechargeTypeName: item.rechargeTypeName,
            discount: item.discount,
            discountFreeAmount: item.discountFreeAmount,
            type: item.type,
            spendMoney: item.spendMoney,
            cardConsumeMoney: item.cardConsumeMoney,
            cardRestMoney: item.cardRestMoney,
            endTime: item.endTime,
            giftConsumeMoney: item.giftConsumeMoney,
            cardGiftMoney: item.cardGiftMoney,
            giftSpendMoney: item.giftSpendMoney,
          };
          rechargeAccountList.push(form);
        }
        item.rechargeAccountList = rechargeAccountList;
        // item.rechargeAccountLists.forEach((items) => {
        //   if (items.spendMoney == items.spendMoney) {
        //     rechargeAccountList.push(items);
        //   }
        // });
        // item.rechargeAccountList = rechargeAccountList;
        list.push(item);
      });

      addServiceOrders(list).then((res) => {
        if (res.code == "000000") {
          this.$message.success({ message: "收银成功 !" });
          this.openDrawer = false;
          this.form = {
            orderNum: 1011001,
            sex2: 0,
            number: 0,
            amount: 0,
            collectionCashAmount: 0,
            payMoney: 0,
            debtMoney: 0,
          };
          this.list = [];
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => { },
          });
        }
      });
    },
    editPay(row, index) {
      this.editIndex = index;
      let form = { ...row };
      form.payMoney = this.form.debtMoney;
      this.payLists.splice(index, 1, form);
    },
    endEdit(row, index) {
      if (!row.payMoney) {
        row.payMoney = 0;
      } else {
        row.payMoney = row.payMoney * 1;
      }
      this.editIndex = -1;
      this.sumPay();
    },
    sumPay() {
      let payMoney = 0;
      this.payLists.forEach((item) => {
        payMoney += item.payMoney;
      });
      this.form.collectionCashAmount = payMoney.toFixed(2) * 1;
      this.form.debtMoney =
        (this.form.payMoney - this.form.collectionCashAmount).toFixed(2) * 1;
      console.log(this.form);
    },
    openPay() {
      this.openDrawer = true;
      this.sumPay();
    },
    getCtList() {
      ctlist({ id: "" }).then((res) => {
        if (res.code == "000000") {
          var list = [];
          res.rows.forEach((item) => {
            let items = {
              payId: item.id,
              payMoney: 0,
              payName: item.collectionTypeName,
            };
            list.push(items);
          });
          this.payLists = list;
        } else {
          this.$alert("现付列表" + res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => { },
          });
        }
      });
    },
    //组件传值
    show(list) {
      this.list = [...list];
      this.list.forEach((item) => {
        item.collectionRechargeAmount = 0;
        item.collectionIntegralAmount = 0;
        item.cashAmount = 0;
        item.collectionAmount = 0;
        item.collectionCashAmount = 0;
      });
      this.getCtList();
      this.sumMoney();
    },

    rowClassName({ row, rowIndex }) {
      row.index = rowIndex;
    },
    sumMoney() {
      console.log(this.form.cashNumber);
      let amount = 0;
      let payMoney = 0;
      this.list.forEach((item) => {
        if (this.form.cashNumber == true) {
          item.amount = (item.priceMember * item.numberTimes).toFixed(2) * 1;
          item.transactionPrice = item.priceMember;
        } else {
          item.transactionPrice = item.price;
          item.amount = (item.price * item.numberTimes).toFixed(2) * 1;
        }
        amount += item.amount * 1;
        payMoney += item.amount;
      });
      this.form.amount = amount.toFixed(2) * 1;
      this.form.payMoney = payMoney.toFixed(2) * 1;
      this.form.debtMoney = payMoney.toFixed(2) * 1;
      this.form.collectionCashAmount = "0.00";
    },
    //指定列求和
    //数组按对应字段合计返回方法
    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 {
              sums[index] = "--";
            }
          }, 0);
          sums[index] += " ";
        } else {
          sums[index] = "--";
        }
      });
      return sums;
    },
  },
  created() {
    var scrollDiv = document.getElementsByClassName("bing-foot");
    scrollDiv.scrollTop = scrollDiv.scrollHeight;
    this.getCtList();
  },
};
</script>

<style scoped>
.bing-foot {
  height: 100px;
  width: 100%;
  background: rgb(255, 246, 228);
  line-height: 100px;
}
</style>
