<template>
  <div class="sellpacking">
    <el-form :model="form" inline label-width="80px">
      <el-form-item label="手机号码">
        <div class="form-width-ms flex">
          <el-input style="padding-left:0px;" ref="" oninput="this.value=this.value.replace(/\D/g,'')" pattern="[0-9]*" maxlength="11" v-model="form.mobilePhone" @change="memberOne" @blur="memberOne">
            <el-button size="mini" slot="append" icon="el-icon-search" @click="openMember"></el-button>
          </el-input>
          <el-button icon="el-icon-circle-plus-outline" type="text" @click="openAddMember"></el-button>

        </div>
      </el-form-item>
      <el-form-item label="会员名字">
        <div class="form-width-ms text-cyan text-bold">
          {{form.memberName}}
        </div>
      </el-form-item>
      <el-form-item label="促销名称:">
        <div class="form-width-ms text-red text-bold">
          {{form.promotionName}}
          <!-- <div v-if="form.promotionName!=''" @click="openPromotion( openPromotionDialog=true)"> {{form.promotionName}} </div>
          <el-button size="mini" type="text" @click="openPromotion( openPromotionDialog=true)" v-else>选择</el-button> -->
        </div>
      </el-form-item>
      <el-form-item label="促销总价:">
        <div class="form-width-ms text-red text-price">
          {{form.totalMoney}}
        </div>
      </el-form-item>
      <br>
      <el-form-item label="促销单价:">
        <div class="form-width-ms ">
          <span class="text-red" v-if="form.moneyType==1&&checkTableList.length==0">选择下列项目</span>
          <span v-else class="text-red text-price">{{form.saleMoney}}</span>
        </div>
      </el-form-item>
      <el-form-item label="购买套数:">
        <div class="form-width-ms text-cyan text-bold">
          <div style="width:100%;cursor: pointer;" v-if="!countShow" @click="editCount">{{form.count}}
          </div>
          <el-input class="form-width-xs" ref="countInput" v-else v-model="form.count" @blur="endEditCount()"></el-input>
        </div>
      </el-form-item>
      <el-form-item label="销售金额:">
        <div class="form-width-ms text-red text-price">
          {{form.amount}}
        </div>
      </el-form-item>
      <el-form-item label="待付金额">
        <div class="form-width-ms text-green text-price">
          {{form.debtMoney}}
        </div>
      </el-form-item>
      <br>
      <el-form-item label="适用对象:">
        <div class="form-width-ms">
          <span v-show="form.suitableObject==0">全部会员</span>
          <span v-show="form.suitableObject==1">生日会员</span>
          <span v-show="form.suitableObject==2">指定会员</span>
        </div>
      </el-form-item>
      <el-form-item label="限购套数:">
        <div class="form-width-ms">
          <span v-show="form.limitBuy==0">不限购</span>
          <span v-show="form.limitBuy==1"> 限购{{form.limitCount}}套</span>
        </div>
      </el-form-item>
      <el-form-item label="卡付折扣:">
        <div class="form-width-ms">
          <span v-show="form.cardBuy==0">不允许卡付</span>
          <span v-show="form.cardBuy==1">允许卡付
            <!-- <span v-show="form.cardLimit==1">可打折</span> <span v-show="form.cardLimit==0">不可打折</span> -->
          </span>
        </div>
      </el-form-item>
      <el-form-item label="开始时间:">
        <div>
          {{form.beginDate }}
        </div>
      </el-form-item>
      <br>
      <el-form-item label="结束时间:">
        <div class="form-width-ms">
          {{form.endDate }}
        </div>
      </el-form-item>
      <el-form-item label="购买备注:">
        <el-input size="small" type="textarea" :autosize="{ minRows: 1.5, maxRows: 2 }" v-model="form.description" style="max-width:600px;width:600px"></el-input>
      </el-form-item>
      <div class="border-radius-10 border   bg-white padding-xs" v-if="form.projectCount" style="width:920px;margin-left:20px">
        <el-table ref="regTable" @selection-change="handleSelectionChange" @row-click="rowClicks" :data="projectList" :header-cell-style="{background: 'linear-gradient(#6cb3ff, #1873d4)',color:'#eeeeee',}">
          <el-table-column type="selection" width="55"></el-table-column>
          <!-- :selectable="checkStatus" -->
          <el-table-column prop="projectNum" label="项目编码" align="center" min-width="120"></el-table-column>
          <el-table-column prop="projectName" label="项目名称" align="center" min-width="120"></el-table-column>
          <el-table-column prop="priceMember" label="标准价" align="center" min-width="120"></el-table-column>
          <el-table-column prop="required" label="必选" align="center" min-width="80">
            <template slot-scope="scope">{{scope.row.required==0?'是':"否"}}</template>
          </el-table-column>
          <el-table-column prop="times" label="次数" align="center" min-width="120"></el-table-column>
          <el-table-column prop="projectTotalMoney" label="总价值" align="center" min-width="120"></el-table-column>
        </el-table>
      </div>
      <br>
      <div class="flex justify-center align-start flex-wrap padding-top-xs padding-left margin-left" style="width:940px">
        <div style="width:610px" class="border-radius-10 border margin-right-xs  bg-white padding-xs" v-if="form.cardBuy==1">
          <el-table :data="rechargeAccountList" :summary-method="getSummaries" show-summary>
            <el-table-column align="center" :show-overflow-tooltip="true" prop="rechargeTypeName" label="名称" min-width="120"></el-table-column>
            <el-table-column show-overflow-tooltip min-width="70" align="center" prop="cardRestMoney" label="剩余金额"></el-table-column>
            <el-table-column align="center" prop="cardOver" label="可用金额" width="70"></el-table-column>
            <!-- <el-table-column v-if="form.cardLimit==1" align="center" prop="offsetMoney" label="可抵扣额" width="90"></el-table-column> -->
            <el-table-column align="center" prop="spendMoney" label="本次支付" width="100">
              <template slot-scope="scope">
                <div v-if="!scope.row.isShow" @click="openCard(scope.row,scope.$index)" class="form-width-xs">
                  <span>{{scope.row.spendMoney}}</span>
                  <i class="el-icon-edit text-cyan"></i>
                </div>
                <el-input v-focus class="form-input-wdith-xs" @change="endEditCard(scope.row)" @blur="endEditCard(scope.row)" v-else v-model="scope.row.spendMoney" type="number"></el-input>
              </template>
            </el-table-column>
            <!-- <el-table-column v-if="form.cardLimit==1" align="center" prop="discoutMoney" label="抵扣支付" width="90"> </el-table-column> -->
            <el-table-column align="center" prop="cardOvers" label="可用余额" width="90"></el-table-column>
            <el-table-column show-overflow-tooltip align="center" prop="storeName" label="所属门店" width="100"></el-table-column>
          </el-table>
          <!-- <div class="close">
            <span>合计</span>
            <span style="margin-right:110px;" v-if="form.cardLimit==1">¥{{spendMoneys}}</span>
            <span v-else style="margin-right:300px;">¥{{spendMoneys}}</span>
          </div> -->
        </div>
        <div style="width:300px" class="border-radius-10 border margin-right-xs  bg-white padding-xs">
          <el-table :data="payLists" :summary-method="getSummaries" show-summary>
            <el-table-column align="center" prop="payName" label="支付方式" min-width="100"></el-table-column>
            <el-table-column align="center" prop="payMoney" fixed="right" label="本次支付 ( ¥ )" width="120">
              <template slot-scope="scope">
                <div style="padding:0 10px" v-if="!scope.row.isShow" @click="openpay(scope.row,scope.$index)">
                  <span>{{scope.row.payMoney}}</span>
                  <i class="el-icon-edit text-cyan"></i>
                </div>
                <el-input v-else v-focus type="number" @blur="endEdit(scope.row)" v-model="scope.row.payMoney">
                </el-input>
              </template>
            </el-table-column>
          </el-table>
          <!-- <div class="close">
            <span>合计</span>
            <span>¥{{payMoneys}}</span>
          </div> -->
        </div>
      </div>
      <div class="flex justify-start align-start flex-wrap padding-top-xs padding-left margin-left">
        <staffsles ref="staffsles" @staffslesData="staffslesData"></staffsles>
        <admin ref="admin" @adminData="adminData"></admin>
        <manager ref="manager" @managerData="managerData"></manager>
      </div>
    </el-form>
    <div class="sellpackingDiv">
      <el-dialog :close-on-click-modal="false" v-dialogDrag title="促销列表" :visible.sync="sellListDialog" width="80%">
        <el-table :data="sellList">
          <el-table-column prop="isCancel" label="状态" align="center" width="80" fixed="left">
            <template slot-scope="scope">
              <el-tag size="medium" effect="dark" :type="scope.row.isCancel==0?'success':''||scope.row.isCancel==1?'primary':''">
                <span v-show="scope.row.isCancel==0">正常</span>
                <span v-show="scope.row.isCancel==1">作废</span>
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="docNum" label="促销单号" align="center" min-width="120"></el-table-column>
          <el-table-column prop="docDate" label="购买时间" align="center" min-width="160"></el-table-column>
          <el-table-column prop="promotionName" label="促销名称" align="center" min-width="80"></el-table-column>
          <el-table-column prop="promotionNum" label="促销编码" align="center" min-width="120"></el-table-column>
          <el-table-column prop="memberName" label="会员名称" align="center" min-width="80"></el-table-column>
          <el-table-column prop="mobilePhone" label="手机号码" align="center" min-width="120">
            <template slot-scope="scope">
              {{scope.row.mobilePhone | phone}}
            </template>
          </el-table-column>
          <el-table-column prop="price" label="促销单价" align="center" min-width="80"></el-table-column>
          <el-table-column prop="count" label="购买套数" align="center" min-width="80"></el-table-column>
          <el-table-column prop="amount" label="销售价格" align="center" min-width="80"></el-table-column>
          <el-table-column label="操作" align="center" width="160" fixed="right">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" @click="cancel(scope.row)" plain>作废</el-button>
              <el-button size="mini" type="success">打印</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" type="primary" @click="sellListDialog = false" plain>关闭</el-button>
        </div>
      </el-dialog>
      <el-dialog :close-on-click-modal="false" v-dialogDrag title="促销列表" :visible.sync="openPromotionDialog" width="50%">
        <el-table :data="promotionList" @row-click="rowClick" @row-dblclick="confirmPromotion(openPromotionDialog=false)">
          <el-table-column label="选择" width="80">
            <template slot-scope="scope">
              <el-radio v-model="radioPromotionValue" :label="scope.row.id" @change.native="scope.row"></el-radio>
            </template>
          </el-table-column>
          <el-table-column align="center" min-width="120" prop="promotionNum" label="促销编码"></el-table-column>
          <el-table-column align="center" min-width="100" prop="promotionName" label="促销编码"></el-table-column>
          <el-table-column align="center" min-width="160" prop="beginDate" label="开始时间"></el-table-column>
          <el-table-column align="center" min-width="160" prop="endDate" label="结束时间"></el-table-column>
          <el-table-column align="center" min-width="80" prop="totalMoney" label="总价金额"></el-table-column>
          <el-table-column align="center" min-width="80" prop="saleMoney" label="销售单价"></el-table-column>
          <el-table-column align="center" min-width="100" label="限购">
            <template slot-scope="scope">
              <div v-if="scope.row.limitBuy==0">不限购</div>
              <div v-else>
                限购{{scope.row.limitCount}}套
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" min-width="100" label="充值卡付">
            <template slot-scope="scope">
              <div v-if="scope.row.cardBuy==0">不允许</div>
              <div v-else>
                允许卡付
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" type="primary" @click="openPromotionDialog = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="confirmPromotion(openPromotionDialog=false)">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <div style="display: flex;justify-content: center;width:960px" class="padding-top margin-top">
      <el-button style="margin:0 15px" size="mini" type="info" @click="reset()">重置</el-button>
      <el-button style="margin:0 15px" size="mini" type="success" @click="openSellList(sellListDialog=true)">列表</el-button>
      <el-button style="margin:0 15px;" :disabled="form.projectCount>checkTableList.length" size="mini" type="primary" :loading="loadingShow" @click="submit">提交</el-button>
    </div>
    <el-dialog :close-on-click-modal="false" v-dialogDrag title="提示" :visible.sync="paymentcodeDialog" width="400px">
      <span style="font-size:14px; margin-left:10px;color:#999">本次支付需要验证支付密码</span>
      <el-form :model="paymentform">
        <el-form-item label="支付密码 : ">
          <el-input style="width:150px" size="small" v-model="paymentform.cashPassword" type="password" placeholder="会员的支付密码"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" plain @click="paymentcodeDialog = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="payment( paymentcodeDialog=false)">确 定</el-button>
      </span>
    </el-dialog>
    <member ref="member" @memberData="memberData"></member>
    <addmember ref="addmember"></addmember>
  </div>
</template>

<script>
import {
  ctlist,
  recAccountList,
  storeMemberOne,
  checkCashWord,
} from "@/api/eashier.js";
import { partten } from "@/../partten/index.js";
import {
  addDocPromotion,
  docPromotionPage,
  cancelDocPromotion,
} from "@/api/home.js";
import { promotionPage } from "@/api/promotion.js";
import member from "@/components/member/index";
import admin from "@/components/staffList/admin/index";
import manager from "@/components/staffList/manager/index";
import staffsles from "@/components/staffList/staff/index";
import addmember from "@/components/member/edit/index";

export default {
  inject: ["reload"],
  components: {
    staffsles,
    manager,
    admin,
    member,
    addmember,
  },
  data() {
    return {
      openPromotionDialog: false, //促销列表弹窗开关
      paymentcodeDialog: false, //会员填写密码弹窗开关
      sellListDialog: false, //促销单据列表弹窗开关
      countShow: false, //购买套数修改输入框开关
      loadingShow: false, //提交等待动画
      radioPromotionValue: "", //单选促销项目值
      radioPromotionValueList: {}, //单选促销项目对象
      sessionStorageData: {}, //门店参数集合
      checkTableList: [],
      paymentform: {
        //支付密码
        cashPassword: null,
      },
      projectList: [],
      form: {
        //主表单
        cardBuy: 1,
        memberName: "",
        description: "",
        count: 1,
        amount: null,
        collectionRechargeAmount: 0,
        collectionCashAmount: 0,
        manageStaffName: "",
      },
      copyForm: {}, //复制留存表单
      rechargeAccountList: [], //卡付列表
      recCopyList: [], //复制卡付列表
      promotionList: [], //促销列表
      payLists: [], //现付列表
      sellList: [], //促销单据
      payMoneys: 0, //现付总金额
      spendMoneys: 0, //卡付总金额
      vip: {}, //会员信息
      sell: {}, //提交表单
      staffLists: [{ ratio: 100 }], //员工占比初始化
      managerLists: [{ ratio: 100 }], //员工占比初始化
      adminList: [{ ratio: 100 }], //员工占比初始化
    };
  },
  methods: {
    openAddMember() {
      this.$refs.addmember.show("添加");
    },
    checkStatus(row) {
      if (row.status == 1) {
        return true;
      }
      return false;
    },
    handleSelectionChange(val, row) {
      this.checkTableList = this.noRepeat(val);
      this.projectList.forEach((item) => {
        if (item.required == 0) {
          this.$nextTick(() => {
            this.$refs["regTable"].toggleRowSelection(item, true);
          });
          this.checkTableList.push(item);
        }
      });
      this.checkTableList = this.noRepeat(this.checkTableList);
      console.log(this.checkTableList.length);
      console.log(this.form.projectCount);
      if (this.checkTableList.length > this.form.projectCount) {
        this.$message.warning({ message: "请根据提示选择" });
        this.checkTableList.forEach((item) => {
          this.$nextTick(() => {
            this.$refs["regTable"].toggleRowSelection(item, false);
          });
        });
      }
      if (this.form.moneyType == 1) {
        this.sumTotal();
      }
    },
    sumTotal() {
      let money = 0;
      let form = { ...this.form };
      this.checkTableList.forEach((item) => {
        money += item.projectTotalMoney;
      });
      console.log(this.checkTableList);
      console.log(money);
      form.amount = money.toFixed(2) * 1;
      form.saleMoney = form.amount;
      form.debtMoney = form.amount;
      this.form = JSON.parse(JSON.stringify(form));
      this.resetPay();
    },
    //重置支付
    resetPay() {
      this.payLists.forEach((item) => {
        item.payMoney = 0;
      });
      this.rechargeAccountList = this.recCopyList;
      this.sumMoney();
    },
    handleSelectionAllChange(data) {
      // 过滤当前表格数据
      this.projectList.forEach((v, index) => {
        if (index >= this.form.projectCount) {
          // 大于设置的条数，取消选择
          this.$refs["regTable"].toggleRowSelection(v, false);
        }
      });
    },
    rowClicks(row) {
      // 单击行，设置选中
      if (row.required == 1) {
        const check = this.checkTableList.find((v) => {
          return v.id == row.id;
        });
        this.checkTableList.push(row);
        this.checkTableList = this.noRepeat(this.checkTableList);
        console.log(this.checkTableList);
        console.log(this.checkTableList.length);
        if (this.checkTableList.length > this.form.projectCount) {
          this.$refs["regTable"].toggleRowSelection(row, false);
          this.$message.warning(`最多只能选${this.form.projectCount}条！`);
        } else {
          if (!check && this.checkTableList.length > this.form.projectCount) {
            this.$message.warning(`最多只能选${this.form.projectCount}条！`);
            // this.projectList.forEach((item) => {
            //   if (item.id == row.id) {
            //     item.status = 0;
            //   }
            // });
            return;
          }
          this.$refs["regTable"].toggleRowSelection(row);
          // this.projectList.forEach((item) => {
          //   if (item.id == row.id) {
          //     item.status = 1;
          //   }
          // });
        }
      } else {
        this.$message.warning(`必选项目！`);
      }
    },

    //打开选择会员弹窗
    openMember() {
      this.$refs.member.show();
    },
    //选中会员返回方法
    memberData(v) {
      this.form.mobilePhone = v.mobilePhone;
      this.memberOne();
    },
    //触发员工占比组件
    toStaffRatio() {
      var that = this;
      setTimeout(() => {
        that.$refs.staffsles.show(that.staffLists);
        that.$refs.manager.show(that.managerLists);
        that.$refs.admin.show(that.adminList);
      }, 500);
    },
    //员工占比返回方法
    staffslesData(v) {
      this.staffLists = [...v];
    },
    //高管占比返回方法
    managerData(v) {
      this.managerLists = [...v];
    },
    //管理员占比返回方法
    adminData(v) {
      this.adminList = [...v];
    },
    //填写密码提交
    payment() {
      checkCashWord(this.paymentform).then((res) => {
        if (res.data == true) {
          addDocPromotion(this.sell).then((res) => {
            if (res.code == "000000") {
              this.$message.success({ message: "购买 , " + res.message });
              this.reset();
            } else {
              this.$message.error({ message: "购买 , " + res.message });
            }
          });
        } else {
          this.$message.error({ message: "密码错误 !" });
        }
      });
    },

    //重置清空
    reset(number) {
      this.form = { ...this.copyForm };
      this.payLists = [];
      this.staffLists = [{ ratio: 100 }];
      this.managerLists = [{ ratio: 100 }];
      this.adminList = [{ ratio: 100 }];
      this.rechargeAccountList = [];
      this.payMoneys = 0;
      this.spendMoneys = 0;
      console.log(number);
      if (number) {
        this.$router.push({ path: "/home", query: {} });
        this.reload();
      } else {
        this.ctlist();
        this.toStaffRatio();
      }
    },
    //提交信息
    submit() {
      setTimeout(() => {
        this.loadingShow = false;
      }, 3000);
      this.loadingShow = true;
      let payLists = [];
      this.payLists.forEach((item) => {
        delete item.isShow;
        if (item.payMoney > 0) {
          payLists.push(item);
        }
      });
      this.form.payLists = payLists;
      let rechargeAccountList = [];
      this.rechargeAccountList.forEach((item) => {
        if (item.spendMoney > 0) {
          rechargeAccountList.push(item);
        }
      });
      this.form.rechargeAccountList = rechargeAccountList;
      this.form.staffLists = this.staffLists;
      let salesmens = [];
      this.managerLists.forEach((item) => {
        if (item.id) {
          item.staffType = 0;
          salesmens.push(item);
        }
      });
      this.adminList.forEach((item) => {
        if (item.id) {
          item.staffType = 1;
          salesmens.push(item);
        }
      });
      this.form.salesmens = salesmens;
      this.sell = {
        storeId: this.form.storeId,
        storeName: this.form.storeName,
        memberName: this.form.memberName,
        memberId: this.form.memberId,
        mobilePhone: this.form.mobilePhone,
        promotionId: this.form.id,
        promotionNum: this.form.promotionNum,
        promotionName: this.form.promotionName,
        price: this.form.saleMoney * 1,
        count: this.form.count,
        amount: this.form.amount,
        debtMoney: this.form.debtMoney,
        salesmens: this.form.salesmens,
        projectCount: this.form.projectCount,
        projectTotal: this.form.projectTotal,
        collectionCashAmount: this.form.collectionCashAmount,
        collectionRechargeAmount: this.form.collectionRechargeAmount,
        salesCommission: this.form.salesCommission * 1,
        cardSalesCommission: this.form.cardSalesCommission * 1,
        payLists: this.form.payLists,
        staffLists: this.form.staffLists,
        rechargeAccountList: this.form.rechargeAccountList,
        moneyType: this.form.moneyType,
      };
      if (this.form.debtMoney > 0) {
        this.sell.isDebt = 1;
      } else {
        this.sell.isDebt = 0;
      }
      let checkTableList = this.noRepeat(this.checkTableList);
      this.sell.promotionProjects = checkTableList;
      addDocPromotion(this.sell).then((res) => {
        this.loadingShow = false;
        if (res.code == "000000") {
          this.$message.success({ message: res.message });
          setTimeout(() => {
            this.reset(1);
          }, 1000);
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
    //查询会员信息
    memberOne() {
      var number = partten.phoneNum;
      if (!number.test(this.form.mobilePhone)) {
        this.$message.info({ message: "请输入正确的手机号 ! " });
      } else {
        this.rechargeAccountList = [];
        storeMemberOne({
          mobilePhone: this.form.mobilePhone,
        }).then((res) => {
          //console.log(res);
          if (res.code == "000000") {
            this.vip = res.data;
            this.form.memberName = res.data.memberName;
            this.form.memberId = res.data.id;
            this.form.integral = res.data.integral;
            this.form.consumeIntegral = res.data.consumeIntegral;
            this.form.integralMoney = res.data.integralMoney;
            let form = { ...this.form };
            this.form = JSON.parse(JSON.stringify(form));
            var ids = { memberId: res.data.id };
            recAccountList(ids).then((res) => {
              var rechargeAccountList = [];
              res.rows.forEach((item) => {
                item.isShow = false;
                item.isShows = false;
                item.spendMoney = 0.0;
                item.discoutMoney = 0.0;
                item.cardOver = item.cardConsumeMoney;
                //+ item.giftConsumeMoney;
                item.cardOvers = item.cardConsumeMoney;
                //+ item.giftConsumeMoney;
                if (this.form.cardBuy == 1) {
                  if (item.type == 1) {
                    item.offsetMoney =
                      item.cardConsumeMoney / (item.discount / 100);
                    item.offsetMoney = Math.floor(item.offsetMoney * 100) / 100;
                    item.offsetMoney = this.getDecimal(item.offsetMoney);
                  } else {
                    item.offsetMoney = item.cardConsumeMoney;
                    //item.giftConsumeMoney +
                  }
                } else {
                  item.offsetMoney = item.cardConsumeMoney;
                  //item.giftConsumeMoney+
                }
                rechargeAccountList.push(item);
              });
              this.rechargeAccountList = rechargeAccountList;
              this.recCopyList = rechargeAccountList;
            });
          } else {
            this.$message.warning({ message: res.message });
          }
        });
      }
    },
    //打开修改购买套数
    editCount() {
      if (this.form.promotionName != "") {
        this.countShow = true;
        this.$nextTick(() => {
          this.$refs.countInput.focus();
          this.$refs.countInput.select();
        });
      }
    },
    //关闭修改购买套数
    endEditCount() {
      if (this.form.limitBuy == 1) {
        if (this.form.count * 1 <= this.form.limitCount * 1) {
          this.form.count = this.form.count * 1;
        } else {
          this.$message.info({
            message: "每人限购" + this.form.limitCount + "套",
          });
          this.form.count = this.form.limitCount;
        }
      } else {
        this.countShow = false;
      }
      this.form.amount =
        (this.form.count * 1 * this.form.saleMoney).toFixed(2) * 1;
      this.resetPay();
    },

    //打开修改卡付金额
    openCard(row, index) {
      if (this.form.memberName) {
        if (row.cardOver <= this.form.debtMoney) {
          row.spendMoney = row.cardOver;
        } else {
          row.spendMoney = this.form.debtMoney;
        }
        row.isShow = true;
        row.spendMoney = row.spendMoney.toFixed(2) * 1;
      } else {
        this.$message.warning({ message: "请先输入会员手机号!" });
      }
    },
    //关闭修改卡付金额
    endEditCard(row) {
      if (row.spendMoney * 1 > row.cardOver) {
        this.$message.warning({ message: "支付金额不能大于可用金额!" });
      } else {
        row.spendMoney = row.spendMoney * 1;
        row.isShow = false;
        if (row.spendMoney * 1 >= 0) {
          row.discoutMoney = (row.spendMoney / (row.discount / 100)).toFixed(2);
          row.cardOvers = row.cardOver - row.spendMoney;
        }
      }
      this.sumMoney();
    },
    //打开修改现付金额\
    openpay(row, index) {
      if (this.form.memberName) {
        row.isShow = true;
        row.payMoney = (this.form.debtMoney + row.payMoney).toFixed(2) * 1;
      } else {
        this.$message.info({ message: "请先输入会员手机号 !" });
      }
    },
    //关闭修改现付金额\
    endEdit(row) {
      if (row.payMoney) {
        row.payMoney = row.payMoney * 1;
      } else {
        row.payMoney = 0;
      }
      row.isShow = false;
      this.sumMoney();
    },
    //打开促销项目单选列表
    openPromotion() {
      promotionPage({ pushState: 1 }).then((res) => {
        //console.log(res);
        if (res.code == "000000") {
          let promotionList = [];
          res.pageInfo.list.forEach((item) => {
            item.beginDates = item.beginDate.replace(/\-/g, "/");
            item.endDates = item.endDate.replace(/\-/g, "/");

            if (
              item.endDates > partten.dates &&
              partten.dates > item.beginDates &&
              item.state != 6
            ) {
              promotionList.push(item);
            }
          });
          this.promotionList = promotionList;
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
    //点击选中
    rowClick(row) {
      this.radioPromotionValue = row.id;
      this.radioPromotionValueList = row;
    },
    //选中促销项目确定方法
    confirmPromotion() {
      this.form = Object.assign(this.form, this.radioPromotionValueList);
      this.form.amount = this.form.saleMoney * 1 * this.form.count * 1;
    },
    //打开促销列表
    openSellList() {
      this.getDocPage();
    },
    //获取促销单据列表
    getDocPage() {
      docPromotionPage({ id: "" }).then((res) => {
        if (res.code == "000000") {
          this.sellList = res.pageInfo.list;
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
    //作废单据
    cancel(row) {
      cancelDocPromotion({ id: row.id }).then((res) => {
        if (res.code == "000000") {
          this.$message.success({ message: res.message });
          this.getDocPage();
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
    //获取现付列表
    ctlist() {
      ctlist({ id: "" }).then((res) => {
        let payLists = [];
        res.rows.forEach((item) => {
          let items = {
            payName: item.collectionTypeName,
            payId: item.id,
            payMoney: 0,
            isShow: false,
          };
          payLists.push(items);
        });
        this.payLists = payLists;
      });
    },
    //合计支付金额
    sumMoney() {
      let form = JSON.parse(JSON.stringify(this.form));
      let payMoney = 0;
      let spendMoney = 0;
      let discoutMoney = 0;
      this.payLists.forEach((item) => {
        payMoney += item.payMoney * 1;
      });
      this.payMoneys = payMoney;
      form.collectionCashAmount = payMoney;
      this.rechargeAccountList.forEach((item) => {
        spendMoney += item.spendMoney * 1;
        if (form.cardLimit == 0) {
          discoutMoney += item.spendMoney * 1;
        } else {
          discoutMoney += item.discoutMoney * 1;
        }
      });
      this.spendMoneys = discoutMoney.toFixed(2) * 1;
      this.payMoneys = payMoney.toFixed(2) * 1;
      form.collectionRechargeAmount = spendMoney.toFixed(2) * 1;
      form.debtMoney =
        (form.amount - this.payMoneys - this.spendMoneys).toFixed(2) * 1;
      this.form = JSON.parse(JSON.stringify(form));
    },
    // 保留两位小数
    getDecimal(num) {
      //判断是否有小数点
      var s = num.toString().indexOf(".");
      if (s == -1) {
        //是整数
        return (
          (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,") + ".00"
        );
      } else {
        //是小数
        var arr = num.toString().split(".");
        if (arr.length > 1 && arr[1].length < 2) {
          //一位小数
          return (
            (arr[0] || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,") +
            "." +
            arr[1] +
            "0"
          );
        } else {
          //两位小数
          return (
            (arr[0] || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, "$1,") +
            "." +
            arr[1]
          );
        }
      }
    },
    //指定列求和
    //数组按对应字段合计返回方法
    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 == "spendMoney") {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              sums[index] = "--";
            }
          }, 0);
          sums[index] += " ";
        } else 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() {
    this.form = JSON.parse(JSON.stringify({}));
    if (this.$route.query.promotionName != undefined) {
      this.form = Object.assign(this.$route.query);
      console.log(this.$route.query);
      this.form.count = 1;
      if (this.form.moneyType == 1) {
        this.form.amount = 0;
        this.form.debtMoney = 0;
        this.form.saleMoney = 0;
      } else {
        this.form.amount = this.form.saleMoney * 1;
        this.form.debtMoney = this.form.amount;
      }
      this.projectList = this.form.projectList;
      this.projectList.forEach((item) => {
        if (item.required == 0) {
          item.status = 1;
          this.$nextTick(() => {
            this.$refs["regTable"].toggleRowSelection(item, true);
          });
          this.checkTableList.push(item);
        } else {
          item.status = 0;
        }
      });
      this.copyForm = { ...this.form };
      this.sessionStorageData = JSON.parse(
        sessionStorage.getItem("sessionStorageData")
      );
      console.log(this.form);
    }
    this.ctlist();
    this.toStaffRatio();
    //  进入home组件的那一刻就会打印
    // console.log("哎呀看见我了");
    // console.log("----------activated--------");
  },
};
</script>

<style>
.sellpacking {
  width: 100%;
  /* height: 100%; */
  margin-bottom: 60px;
}

.sellpacking .el-collapse-item {
  width: 600px;
}
.sellpacking .close {
  display: flex;
  justify-content: space-between;
  padding-right: 50px;
}
.sellpackingDiv .el-radio__label {
  display: none;
}
</style>
