<!--订单操作-->
<template>
  <div>
    <div class="q-mb-sm text-accent">
      <span class="text-primary" v-if="mode === 1">班级：</span>
      <span v-if="mode === 1">{{ saveObj.ClassName }}</span>
      <!-- <span>
        <span class="q-ml-md text-primary">课程：</span>
        {{ saveObj.CourseName }}
      </span> -->
    </div>
    <div class="row q-mb-md">
      <div class="col-10 flex">
        <div v-for="(item, index) in stuData.StuList" :key="`stu_`+index">
          <q-chip icon="person" class="primary">{{ item.StuName }}
            <span v-if="item.IsRenewGuest === 1">(续费)</span>
          </q-chip>
        </div>
      </div>
      <div class="col-2" v-if="mode == 1">
        <q-toggle v-model="OrderMsg.IsChaBan" :true-value="1" :false-value="0" label="插班报入" />
      </div>
    </div>
    <template v-if="OrderMsg.OrderCourseList&&OrderMsg.OrderCourseList.length>0">
      <div class="q-mb-sm text-accent q-mr-lg" style="margin-bottom:24px;">
        <div class="column items-center " style="width:100%;text-align:center;border:1px solid #f5f6f7;">
          <table class="sticky-tow-column-table sticky-two-header-table" ref="PlanTable" style="border-collapse:collapse;margin-top:0;width:100%;border: 1 solid #000000;">
            <thead>
              <tr>
                <th v-if="mode==1">班级</th>
                <th >课程</th>
                <th v-if="mode==1">老师</th>
                <th v-if="mode==1">上课时间</th>
                <th v-if="mode!=1">所属科目</th>
                <th v-if="mode!=1">售价</th>
                <th v-if="mode!=1">价格类型</th>
                <th v-if="mode==2">教材费</th>
                <th v-if="mode==2">课件费</th>
              </tr>
           </thead>
          <tr v-for="(item, index) in OrderMsg.OrderCourseList" :key="index" style="height:40px;">
            <td v-if="mode==1"  > {{item.ClassName}} </td>
                <td> {{item.CourseName}}   </td>
                <td v-if="mode==1" > {{item.TeacherName}} </td>
                <td  v-if="mode==1" > {{item.OpenTime}} </td>
                <td  v-if="mode!=1" >{{item.CourseSubjectName}}</td>
                <td  v-if="mode!=1" >{{item.SellPrice}}</td>
                <td  v-if="mode!=1" >{{item.SellPriceType==1?"课程总价":"课时单价"}}</td>
                <td v-if="mode==2" >{{item.TextbookFee}}</td>
                <td  v-if="mode==2" >{{item.CoursewareFee}}</td>
          </tr>
            </table>
        </div>
     </div>
    </template>
    <!-- 一个约课、班课 -->
    <div class="column items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <q-input filled stack-label maxlength="3" :dense="false" @keyup.native="checkInteger(OrderMsg, 'GuestNum')"
          v-model="OrderMsg.GuestNum" @input="calcPrice()" label="人数" :rules="[val => !!val || '请填写人数']" disable
          class="col-6 q-py-sm" />
        <template v-if="OrderMsg.IsChaBan == 1">
          <q-select filled option-value="CourseId" :disable="modityOrderType == 2" option-label="CourseName"
            ref="CourseId" v-model="OrderMsg.CourseId" :options="CourseList" emit-value map-options
            class="col-6 q-py-sm" :rules="[val => !!val || '请选择课程']" label="选择课程" @input="calcPrice()" />
          <q-input filled stack-label :dense="false" v-model="OrderMsg.StartClassHours" :disable="modityOrderType == 2"
            @keyup.native="checkInteger(OrderMsg, 'StartClassHours')" ref="StartClassHours" @input="calcPrice()"
            class="col-6 q-py-sm" label="起始课时" />
          <q-input filled v-model="OrderMsg.EffectTime" ref="EffectTime" :rules="[val => !!val || '请填生效时间']"
            class="col-6 q-py-sm" :disable="modityOrderType == 2" mask="date" label="生效时间">
            <template v-slot:append>
              <q-icon name="event" class="cursor-pointer">
                <q-popup-proxy ref="qDateProxy1" transition-show="scale" transition-hide="scale">
                  <q-date v-model="OrderMsg.EffectTime" @input="() => $refs.qDateProxy1.hide()" />
                </q-popup-proxy>
              </q-icon>
            </template>
          </q-input>
          <q-select filled option-value="OrderId" option-label="CourseName" class="col-6 q-py-sm"
            :disable="modityOrderType == 2" v-model="OrderMsg.UpOrderId" :options="beforeOrderList" emit-value
            map-options label="前置订单">
            <template v-slot:option="{
                itemProps,
                itemEvents,
                opt,
                selected,
                toggleOption
              }">
              <q-item v-bind="itemProps" v-on="itemEvents">
                <q-item-section>
                  <q-item-label><span style="color:blue">{{ opt.OrderId }}</span>
                    {{ opt.CourseName }} {{ opt.ClassName }}
                    {{ getStudent(opt.GuestList) }}</q-item-label>
                </q-item-section>
              </q-item>
            </template>
          </q-select>
        </template>
        <q-input filled stack-label :dense="false" v-model="OrderMsg.Class_Price" :disable="true" class="col-6 q-py-sm"
          label="单价(课程售价)" style="display:none;" />
        <template v-if="modityOrderType == 1">
          <q-input filled stack-label :dense="false" v-model="OrderMsg.Unit_Price"
            :disable="IsShowUpPrice == true ? false : true" @blur="calcPrice()" class="col-6 q-py-sm" label="成交单价"
            :rules="[val => !!val || '请填成交单价']" />
          <q-toggle v-model="IsShowUpPrice" label="高于定价收生" class="q-mb-md" style="display:none;" />
        </template>
        <template v-if="modityOrderType == 2">
          <q-input filled stack-label :dense="false" v-model="OrderMsg.Unit_Price" :disable="true" @input="calcPrice()"
            class="col-6 q-py-sm" label="成交单价" :rules="[val => !!val || '请填成交单价']" />
        </template>
        <q-input v-if="false" filled stack-label :dense="false" v-model="OrderMsg.DiscountMoney" :disable="true"
          class="col-6 q-py-sm" label="优惠金额" />
        <template v-if="OrderMsg.Unit_PriceType==2">
          <q-input filled stack-label :dense="false" type="number" :min="1" @input="oneTotalCourseFee(1)"
            v-model="OrderMsg.TotalClassHours" class="col-6 q-py-sm" label="总课时数" />
        </template>
        <q-input v-if="mode!=1" filled stack-label :dense="false" type="number" :min="0" @input="oneTotalCourseFee(0)"
          v-model="OrderMsg.TextbookFee" class="col-6 q-py-sm" label="教材费" />
        <q-input v-if="mode!=1" filled stack-label :dense="false" type="number" :min="0" @input="oneTotalCourseFee(0)"
          v-model="OrderMsg.CoursewareFee" class="col-6 q-py-sm" label="课件费" />
        <template v-if="modityOrderType == 3">
          <q-input filled stack-label :dense="false" v-model="OrderMsg.Unit_Price" @input="calcPrice()"
            class="col-6 q-py-sm" label="成交单价" :rules="[val => !!val || '请填成交单价']" />
        </template>
        <q-input filled stack-label :disable="modityOrderType != 3" :dense="false" maxlength="10"
          @keyup.native="checkPrice(OrderMsg, 'PreferPrice')" v-model="OrderMsg.PreferPrice" class="col-6 q-py-sm"
          label="应收" />
        <q-select v-show="false" v-if="mode == 2" standout="bg-primary text-white" option-value="SId"
          option-label="SName" v-model="OrderMsg.ScrollSchoolId" :options="newSchoolList" emit-value map-options
          class="col-6 q-py-sm" label="校区" />
        <q-select style="display:none;" :disable="modityOrderType == 2" standout="bg-primary text-white"
          option-value="Id" option-label="Name" v-model="OrderMsg.OrderSource" :options="SourceEnumList" emit-value
          map-options class="col-6 q-py-sm" label="客人来源" />
        <q-select v-if="false" :disable="
            modityOrderType == 2 ||
              OrderMsg.JoinType == 3 ||
              CourseConsultantDisable
          " v-model="OrderMsg.CourseConsultantId" :options="EmployeeList2" filled use-input label="课程顾问"
          option-label="EmployeeName" option-value="Id" ref="EmployeeName2" class="col-6 q-py-sm" emit-value map-options
          @filter="filterFn2">
          <template v-slot:no-option>
            <q-item>
              <q-item-section class="text-grey">
                未找到相关数据
              </q-item-section>
            </q-item>
          </template>
        </q-select>
        <q-input :disable="modityOrderType == 2" v-if="OrderMsg.OrderSource == 3" filled stack-label :dense="false"
          maxlength="50" v-model="OrderMsg.GeneralOccupation" class="col-6 q-py-sm" label="一般同行" />
        <q-input :disable="modityOrderType == 2" v-if="OrderMsg.OrderSource == 7" filled stack-label :dense="false"
          maxlength="50" v-model="OrderMsg.EduOccupation" class="col-6 q-py-sm" label="教育同行" />
        <q-input :disable="modityOrderType == 2" v-if="OrderMsg.IsLessPrice == 1" @input="calcPrice()"
          @keyup.native="checkPrice(OrderMsg, 'PerLessMoney')" maxlength="10" filled stack-label :dense="false"
          v-model="OrderMsg.PerLessMoney" class="col-6 q-py-sm" label="少价金额(每人)"
          :hint="'总少价金额：' + OrderMsg.PerLessMoney * OrderMsg.GuestNum" />
        <q-input disable v-if="OrderMsg.EnterId > 0" filled stack-label v-model="EnterName" class="col-6 q-py-sm"
          label="市场人员" />

        <q-select v-if="mode!=1" v-model="OrderMsg.HelpEnterId" :options="AssistList" filled use-input label="协助老师"
          option-label="EmployeeName" option-value="Id" ref="EmployeeName2" class="col-6 q-py-sm" emit-value map-options
          @filter="filterAssisFn">
          <template v-slot:no-option>
            <q-item>
              <q-item-section class="text-grey">
                未找到相关数据
              </q-item-section>
            </q-item>
          </template>
        </q-select>
        <q-input filled stack-label :dense="false" v-model="OrderMsg.SaleRemark" type="textarea" class="col-12 q-py-sm"
          label="备注" />
      </div>
    </div>
  
    <div class="dialog-out-close" @click="closeEditOrder"
      style="height:40px !important;border-top-left-radius: 4px !important;border-bottom-left-radius: 4px !important;">
      <q-icon name="iconfont icon-jujue1" size="26px" />
    </div>
    <q-dialog v-model="inception">
      <q-card v-if="inceptionData">
        <q-card-section>
          <div class="text-h6">提示</div>
        </q-card-section>
        <q-card-section class="q-pt-none">
          {{ inceptionData.Message }}
        </q-card-section>
        <q-card-actions align="right" class="text-primary">
          <q-btn flat label="取消" size="sm" v-close-popup />
          <q-btn label="立即查看" size="sm" color="accent q-px-md" style="font-weight:400 !important" @click="goorder" />
        </q-card-actions>
      </q-card>
    </q-dialog>
  </div>
</template>
<script>
  import {

    getOrderSourceEnumList, //获取订单来源 枚举
    setClassOrder, //修改订单
    SetClassOrderBatch, //多个约课订单
    queryChaClassInfo,
    GetSelectClassOrderList //获取前置下拉
  } from "../../../../api/sale/sale";
  import {
    GetStudentAssistList,
    GetVisitorReserveTeacherList
  } from "../../../../api/sale/sale"; //获取协助人员
  import {
    queryEmployee
  } from "../../../../api/users/user"; //获取员工
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      },
      mode: {
        type: Number, //1:约课，2：班课,3-Vip
        default: 1
      },
      //订单类型
      orderType: {
        type: Number,
        default: 1
      },
      //修改订单类型
      modityOrderType: {
        type: Number,
        default: 0 //1-销售修改，2-OP修改，3-总经理修改
      },
      stuData: {
        //学生是否续费
        type: Object,
        default: null
      },
      schoolList: {
        type: Array,
        default: () => []
      },
      // 已选的多选课程
      selectedCourseList: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        courseInformationList: [], //存储多个课程
        AssistName: "", //协助老师名称
        IsShowEditOrder: true,
        OrderMsg: {
          ClassId: 0, //班级编号
          GuestNum: 0, //人数
          B2CRatio: 0,
          B2CReNewRatio: 0,
          Unit_Price: 0,
          PreferPrice: 0, //应收
          OrderSource: 0,
          SaleRemark: "",
          Class_Price: 0, //单价
          Unit_PriceType: 1, //价格类型：1-总课时费，2-课时单价  2024-08-26 add by:W
          TotalClassHours: 0, //总课时数 2024-08-26 add by:W
          CoursewareFee: 0, //教材费
          TextbookFee: 0, //课件费
          OrderId: 0,
          OrderType: 0, //订单类型(1-班级课程订单，2-留学就业订单)
          SourceId: 0, //来源编号
          HelpEnterId: 0, //协助老师编号
          GeneralOccupation: "", //一般同行
          EduOccupation: "", //教育同行
          IsLessPrice: 0, //是否少价 0 不少 1 少
          LessPrice: 0, //少价金额
          PerLessMoney: 0, //每人少价金额
          OrderNature: 1, //订单性质
          OldPreferPrice: 0, //原实际应收
          IsChaBan: 0, //是否插班报入(1-是)
          CourseId: "", //课程编号
          StartClassHours: 0, //已上课时
          EffectTime: "", //生效时间
          UpOrderId: 0, //前置订单编号
          JoinType: 1, //订单报入类型
          DiscountMoney: 0, //优惠金额
          PerDiscountMoney: 0, //每人优惠金额
          CourseConsultantId: 0, //课程顾问
          CustomerId: 0, //同行
          OrderIdentify: 2, //标识参数，  1产品下单   2客户转订单
          StuIds: "",
          EnterId: 0, //市场人员
          //  NormalCommission:'',//正常返佣金额
          //  RenewCommission:"",// 预计返佣金额
          ScrollSchoolId: -1,
        },
        newSchoolList: [],
        EnterName: "", //市场人员
        Unit_PriceRemark: "", //单价规则
        IsShowUpPrice: false, //是否显示高于定价
        UnitPrice: 0,
        SourceEnumList: [], //订单来源
        EmployeeList: [], //教师列表
        AllemployeeList: [], //所有教师列表
        EmployeeList2: [], //课程顾问列表
        AllemployeeList2: [], //所有课程顾问列表

        AssistList: [], //协助人员列表
        AllAssistList: [], //所有协助人员列表
        CourseList: [], //课程列表
        courseObj: {}, //选择的课程
        beforeOrderList: [], //前置订单数据
        HelpEnterDisable: false, //是否可选择协助教师
        CourseConsultantDisable: false, //是否可选择课程顾问
        inception: false,
        inceptionData: null,
        OrderCourseList: [], //选择的课程信息
      };
    },
    computed: {

    },
    watch: {

    },
    created() {
      if (this.saveObj && this.saveObj.CourseId) {
        // this.OrderMsg.CourseId = this.saveObj.CourseId;
      }
      this.getOrderSEList();
      this.getEmployee(0);
      this.getEmployee(2);
      this.initConfig();
      this.getSelectClass();
    },
    mounted() {
      this.newSchoolList = [];
      this.newSchoolList = JSON.parse(JSON.stringify(this.schoolList));
      this.newSchoolList.unshift({
        SName: "不限",
        SId: -1
      });
      this.initData();
      this.getVisitorReserveTeacherList();
      this.OrderMsg.EnterId = this.stuData.EnterId;
      this.EnterName = this.stuData.EnterName;

      // 判断是否续费协助老师赋值
      if (this.stuData.StuList[0].IsRenewGuest != 1) {
        this.OrderMsg.HelpEnterId = 0;//this.stuData.StuList[0].STTeacherId;
        this.AssistName = this.stuData.StuList[0].STTeacherName
      }

      if (this.selectedCourseList.length > 1) {
        this.selectedCourseList.forEach(item => {
          let dataObj = {
            CourseName: "", //课程名称
            ClassId: 0, //班级编号
            GuestNum: 1, //人数
            B2CRatio: 0,
            B2CReNewRatio: 0,
            Unit_Price: 0,
            PreferPrice: 0, //应收
            OrderSource: 0,
            SaleRemark: "",
            Class_Price: 0, //单价
            Unit_PriceType: 1, //价格类型：1-总课时费，2-课时单价  2024-08-26 add by:W
            TotalClassHours: 0, //总课时数 2024-08-26 add by:W
            CoursewareFee: 0, //教材费
            TextbookFee: 0, //课件费
            OrderId: 0,
            OrderType: 0, //订单类型(1-班级课程订单，2-留学就业订单)
            SourceId: 0, //来源编号
            HelpEnterId: 0, //协助老师编号
            GeneralOccupation: "", //一般同行
            EduOccupation: "", //教育同行
            IsLessPrice: 0, //是否少价 0 不少 1 少
            LessPrice: 0, //少价金额
            PerLessMoney: 0, //每人少价金额
            OrderNature: 1, //订单性质
            OldPreferPrice: 0, //原实际应收
            IsChaBan: 0, //是否插班报入(1-是)
            CourseId: "", //课程编号
            StartClassHours: 0, //已上课时
            EffectTime: "", //生效时间
            UpOrderId: 0, //前置订单编号
            JoinType: 1, //订单报入类型
            DiscountMoney: 0, //优惠金额
            PerDiscountMoney: 0, //每人优惠金额
            CourseConsultantId: 0, //课程顾问
            CustomerId: 0, //同行
            OrderIdentify: 2, //标识参数，  1产品下单   2客户转订单
            StuIds: "",
            EnterId: 0, //市场人员
            //  NormalCommission:'',//正常返佣金额
            //  RenewCommission:"",// 预计返佣金额
            ScrollSchoolId: -1,
            OrderType: 1
          }
          if (item.CourseName) {
            dataObj.CourseName = item.CourseName
          }
          if (item.ClassId) {
            dataObj.ClassId = item.ClassId;
          }
          if (item.SellPrice) {
            dataObj.Unit_Price = item.SellPrice;
            dataObj.Class_Price = item.SellPrice;
          }
          if (item.SellPriceType) {
            dataObj.Unit_PriceType = item.SellPriceType;
          }
          if (item.TotalClassHours) {
            dataObj.TotalClassHours = item.TotalClassHours;
          }
          if (item.CoursewareFee) {
            dataObj.CoursewareFee = item.CoursewareFee;
          }
          if (item.TextbookFee) {
            dataObj.TextbookFee = item.TextbookFee;
          }
          if (item.SourceId) {
            dataObj.SourceId = item.SourceId;
          }
          if (item.CourseId) {
            dataObj.CourseId = item.CourseId;
          } else {
            dataObj.CourseId = "";
          }
          if (item.B2CRatio) {
            dataObj.B2CRatio = item.B2CRatio;
          }
          if (item.B2CReNewRatio) {
            dataObj.B2CReNewRatio = item.B2CReNewRatio;
          }
          if (this.stuData.RenewNum > 0) {
            dataObj.SaleRemark = `首次报名优惠比例${item.B2CRatio ??
          0}%;续费优惠比例${item.B2CReNewRatio ?? 0}%`;
          } else {
            dataObj.SaleRemark = `首次报名优惠比例${item.B2CRatio ??
            0}%`;
          }
          this.courseInformationList.push(dataObj)
        });
        this.totalCourseFee(1)


      }
    },
    methods: {

      //单个课程计算
      oneTotalCourseFee(type) {
        if (this.OrderMsg.Unit_PriceType == 2) {
          if (type == 1) {
            this.OrderMsg.TextbookFee = Number((this.OrderMsg.TotalClassHours * this.OrderMsg.Unit_Price * this.OrderMsg
              .GuestNum) * (20 / 100)).toFixed(2);
          }
          this.OrderMsg.PreferPrice = this.OrderMsg.GuestNum * this.OrderMsg.Unit_Price * this.OrderMsg
            .TotalClassHours + Number(this.OrderMsg.TextbookFee) + Number(this.OrderMsg.CoursewareFee)
        }
      },

      // 多个课程计算
      totalCourseFee(type) {
        this.courseInformationList.forEach((item) => {
          if (this.stuData.StuList[0].IsRenewGuest == 1) {
            item.DiscountMoney = this.accAdd(
              item.DiscountMoney,
              this.accMul(
                item.Class_Price,
                item.B2CReNewRatio / 100
              )
            );
            if (item.Unit_PriceType == 1) {
              item.PreferPrice = item.GuestNum * item.Unit_Price
            } else if (item.Unit_PriceType == 2) {
              if (type == 1) {
                item.TextbookFee = Number((item.TotalClassHours * item.Unit_Price * item.GuestNum) * (20 / 100))
                  .toFixed(2);
              }
              item.PreferPrice = (item.GuestNum * item.Unit_Price * item.TotalClassHours) + Number(item
                .TextbookFee) + Number(item.CoursewareFee)
            }

          } else {
            item.DiscountMoney = this.accAdd(
              item.DiscountMoney,
              this.accMul(item.Class_Price, item.B2CRatio / 100)
            );
            if (item.Unit_PriceType == 1) {
              item.PreferPrice = item.GuestNum * item.Unit_Price
            } else if (item.Unit_PriceType == 2) {
              item.TextbookFee = Number((item.TotalClassHours * item.Unit_Price * item.GuestNum) * (20 / 100))
                .toFixed(2);
              item.PreferPrice = item.GuestNum * item.Unit_Price * item.TotalClassHours + Number(item.TextbookFee) +
                Number(item.CoursewareFee)
            }
            //item.PreferPrice = item.GuestNum*item.Unit_Price
          }
        })
      },

    
       //获取协助人员
       getVisitorReserveTeacherList() {
        this.AssistList=[];
        this.AllAssistList=[];
        const ids = this.stuData.StuList.map(e => e.StuId).toString();
        GetVisitorReserveTeacherList({
          StuId: ids
        }).then(res => {
          if (res.Code == 1) {
            res.Data.map(e => {
                this.AssistList .push({
                  Id: e.TeacherId,
                  EmployeeName: e.TeacherName
                });
                this.AllAssistList.push({
                  Id: e.TeacherId,
                  EmployeeName: e.TeacherName
                });
                
                this.HelpEnterDisable = true;
            });
            if (this.stuData.StuList[0].IsRenewGuest == 1&&res.Data&&res.Data.length>0) {
                  this.OrderMsg.HelpEnterId = res.Data[0].TeacherId;
                }
            this.getQueryEmployee();
          }
          else{
            this.getQueryEmployee();
          }
        });
      },

      getQueryEmployee(){
        if (this.AssistList.length == 0) {
          var qMsg = {
            IsLeave: 1,
            UserRole: 0,
            AccountType:2,
          };
          queryEmployee(qMsg).then(res => {
            if (res.Code == 1) {
              res.Data.map(e => {
                this.AssistList.push({
                  Id: e.Id,
                  EmployeeName: e.EmployeeName
                });
                this.AllAssistList.push({
                  Id: e.Id,
                  EmployeeName: e.EmployeeName
                });
              });
            }
          });
        }
      },
      //获取协助人员
      getAssistList() {
        const ids = this.stuData.StuList.map(e => e.StuId).toString();
        GetStudentAssistList({
          StuId: ids
        }).then(res => {
          if (res.Code == 1) {
            res.Data.map(e => {
              if (e.AssistType == 4) {
                this.AssistList.push({
                  Id: e.AssistId,
                  EmployeeName: e.AssistName
                });
                this.AllAssistList.push({
                  Id: e.AssistId,
                  EmployeeName: e.AssistName
                });
                if (this.stuData.StuList[0].IsRenewGuest == 1) {
                  this.OrderMsg.HelpEnterId = e.AssistId;
                  this.AssistName = e.AssistName
                }
                this.HelpEnterDisable = true;
              }
              if (e.AssistType == 2) {
                this.OrderMsg.CourseConsultantId = e.AssistId;
                this.CourseConsultantDisable = true;
                if (this.selectedCourseList.length > 1) {
                  this.courseInformationList.forEach(item => {
                    return item.CourseConsultantId = e.AssistId
                  })
                }
              }
            });
          }
        });

        if (this.AssistList.length == 0) {
          var qMsg = {
            IsLeave: 1,
            UserRole: 0,
            AccountType:2,
          };
          queryEmployee(qMsg).then(res => {
            if (res.Code == 1) {
              res.Data.map(e => {
                this.AssistList.push({
                  Id: e.Id,
                  EmployeeName: e.EmployeeName
                });
                this.AllAssistList.push({
                  Id: e.Id,
                  EmployeeName: e.EmployeeName
                });
              });
            }
          });
        }
      },
      //获取班级课程信息
      initConfig() {
        var qMsg = {
          ClassId: 0
        };
        if (this.saveObj.ClassId) {
          qMsg.ClassId = this.saveObj.ClassId;
        }
        queryChaClassInfo(qMsg).then(res => {
          if (res.Code == 1) {
            this.CourseList = res.Data.otherCourse;
            if (this.saveObj && this.saveObj.OrderId > 0) {} else {
              this.OrderMsg.StartClassHours = res.Data.finishHours;
            }
            if (this.OrderMsg && this.OrderMsg.CourseId) {
              this.courseObj = this.CourseList.find(
                x => x.CourseId == this.OrderMsg.CourseId
              );
            }
            this.calcPrice();
          }
        });
      },
      //计算课程单价和应收金额
      calcPrice() {
        this.OrderMsg.GuestNum = this.stuData.StuList.length;
        var guestNum = 0;
        if (this.OrderMsg.GuestNum && this.OrderMsg.GuestNum > 0) {
          guestNum = Number(this.OrderMsg.GuestNum);
        }
        if (this.saveObj.SellPriceType == 2) {
          this.OrderMsg.TextbookFee = ((Number(this.OrderMsg.TotalClassHours) * Number(this.OrderMsg.Unit_Price) *
            Number(this.OrderMsg.GuestNum)) * (20 / 100)).toFixed(2);
        }
        let temp = this.CourseList.find(
          x => x.CourseId == this.OrderMsg.CourseId
        );
        if (temp) {
          this.courseObj = temp;
          var tempDiscountMoney = 0; //优惠金额
          // var tempSaleRemark = `直客首次报名优惠比例${temp.B2CRatio ?? 0}%`; //备注
          //插班课时单价
          var classHourPrice = temp.SellPrice / this.courseObj.ClassHours;
          var chaBanPrice = Number(
            classHourPrice *
            (this.courseObj.ClassHours - this.OrderMsg.StartClassHours)
          ).toFixed(2);
          if (guestNum == 1) {
            if (temp.CoursePriceList && temp.CoursePriceList.length > 0) {
              if (temp.CoursePriceList[0].PriceType == 0) {
                if (this.OrderMsg.IsChaBan == 1) {
                  tempDiscountMoney =
                    (chaBanPrice * temp.CoursePriceList[0].PriceMoney) / 100;
                } else {
                  tempDiscountMoney =
                    (temp.SellPrice * temp.CoursePriceList[0].PriceMoney) / 100;
                }
                // tempSaleRemark = "单人报名优惠" + temp.CoursePriceList[0].PriceMoney + "%";
              } else {
                tempDiscountMoney = item.CoursePriceList[0].PriceMoney;
                // tempSaleRemark = "单人报名优惠" + temp.CoursePriceList[0].PriceMoney;
              }
            }
          }
          if (guestNum > 1) {
            if (temp.CoursePriceList && temp.CoursePriceList.length > 0) {
              if (temp.CoursePriceList[1].PriceType == 0) {
                // tempSaleRemark = "双人报名优惠" + temp.CoursePriceList[1].PriceMoney + "%";
                if (this.OrderMsg.IsChaBan == 1) {
                  tempDiscountMoney =
                    (chaBanPrice * temp.CoursePriceList[1].PriceMoney) / 100;
                } else {
                  tempDiscountMoney =
                    (temp.SellPrice * temp.CoursePriceList[1].PriceMoney) / 100;
                }
              } else {
                tempDiscountMoney = item.CoursePriceList[1].PriceMoney;
                // tempSaleRemark = "双人报名优惠" + temp.CoursePriceList[1].PriceMoney;
              }
            }
          }
          var perDiscountMoney = Number(tempDiscountMoney).toFixed(2);
          this.OrderMsg.PerDiscountMoney = perDiscountMoney;
          //计算每人优惠和总优惠
          // if (this.OrderMsg.IsChaBan == 1) {
          //   this.OrderMsg.Unit_Price = chaBanPrice;
          //   this.UnitPrice = chaBanPrice;
          // } else {
          //   this.OrderMsg.Unit_Price = temp.SellPrice;
          //   this.UnitPrice = temp.SellPrice;
          // }
          // this.OrderMsg.SaleRemark = tempSaleRemark;
        } else {
          if (this.saveObj.ClassId) {
            this.OrderMsg.ClassId = this.saveObj.ClassId;
          }
          if (this.saveObj.Unit_Price) {
            this.OrderMsg.Unit_Price = this.saveObj.Unit_Price;
            this.UnitPrice = this.saveObj.Unit_Price;
          }
          if (this.saveObj.SourceId) {
            this.OrderMsg.SourceId = this.saveObj.SourceId;
          }
          this.courseObj = {};
        }

        /**计算少价---开始*/
        var tempPerLessMoney = 0;
        if (this.OrderMsg.PerLessMoney && this.OrderMsg.PerLessMoney > 0) {
          tempPerLessMoney = this.OrderMsg.PerLessMoney;
        }
        this.OrderMsg.LessPrice = tempPerLessMoney * guestNum;
        /**计算少价----结束*/
        var unit_price = 0;
        if (this.OrderMsg.Unit_Price && this.OrderMsg.Unit_Price > 0) {
          unit_price = Number(this.OrderMsg.Unit_Price);
        }
        var str = "";
        //计算应收价格
        if (this.OrderMsg.IsChaBan == 1) {
          var newPreferPrice = 0;
          if (
            this.courseObj &&
            this.courseObj.CourseId &&
            this.courseObj.CourseId > 0
          ) {
            if (this.OrderMsg.Unit_PriceType == 1) {
              newPreferPrice = chaBanPrice * guestNum
            } else if (this.OrderMsg.Unit_PriceType == 2) {
              newPreferPrice = chaBanPrice * guestNum * this.OrderMsg.TotalClassHours
            }
          }
          console.log("我进来了1");
          this.OrderMsg.PreferPrice =
            Number(newPreferPrice).toFixed(2) - this.OrderMsg.LessPrice;
        } else {

          if (this.OrderMsg.Unit_PriceType == 1) {
            console.log("guestNum", guestNum);
            console.log("unit_price", unit_price);
            console.log("this.OrderMsg.LessPrice", this.OrderMsg.LessPrice);
            this.OrderMsg.PreferPrice =
              Number(guestNum * unit_price).toFixed(2) - this.OrderMsg.LessPrice;
            console.log("我进来了2");
          } else if (this.OrderMsg.Unit_PriceType == 2) {
            this.OrderMsg.PreferPrice =
              Number((guestNum * unit_price * this.OrderMsg.TotalClassHours) + Number(this.OrderMsg.TextbookFee) +
                Number(this.OrderMsg.CoursewareFee)).toFixed(2) - this.OrderMsg.LessPrice;
            console.log("我进来了3");
          }
        }
        //留学就业订单
        if (this.OrderMsg.OrderType == 2) {
          this.OrderMsg.OldPreferPrice = Number(guestNum * unit_price).toFixed(2);
        }



        this.OrderMsg.DiscountMoney = 0;

        this.stuData.StuList.map(e => {
          if (e.IsRenewGuest == 0) {
            this.OrderMsg.DiscountMoney = this.accAdd(
              this.OrderMsg.DiscountMoney,
              this.accMul(this.OrderMsg.Class_Price, this.OrderMsg.B2CRatio / 100)
            );
          } else {
            this.OrderMsg.DiscountMoney = this.accAdd(
              this.OrderMsg.DiscountMoney,
              this.accMul(
                this.OrderMsg.Class_Price,
                this.OrderMsg.B2CReNewRatio / 100
              )
            );
          }
        });
      },
      // 初始化
      initData() {
        this.OrderMsg.HelpEnterId = 0;
        this.OrderMsg.GeneralOccupation = "";
        this.OrderMsg.EduOccupation = "";
        this.OrderMsg.IsLessPrice = 0;
        this.OrderMsg.LessPrice = 0;
        this.OrderMsg.PerLessMoney = 0;
        this.OrderMsg.OrderNature = 0;
        this.OrderMsg.OldPreferPrice = 0;
        this.OrderMsg.StartClassHours = 0;
        this.OrderMsg.IsChaBan = 0;
        this.OrderMsg.EffectTime = "";
        this.OrderMsg.UpOrderId = 0;
        this.OrderMsg.JoinType = 1;
        this.OrderMsg.DiscountMoney = 0;
        this.OrderMsg.PerDiscountMoney = 0;
        this.OrderMsg.OrderType = this.orderType;
        this.OrderMsg.B2CRatio = 0;
        this.OrderMsg.B2CReNewRatio = 0;
        this.OrderMsg.CustomerId = 0;
        this.OrderMsg.CourseConsultantId = 0;
        this.OrderMsg.TotalClassHours = 0;
        this.OrderMsg.Unit_PriceType = 1;
        this.OrderMsg.OrderCourseList = [];
        if (this.OrderMsg.OrderType == 2) {
          this.OrderMsg.OrderNature = 1;
        }
        console.log("saveObj", this.saveObj);
        if (this.saveObj) {
          if (this.saveObj.ClassId) {
            this.OrderMsg.ClassId = this.saveObj.ClassId;
          }
          if (this.saveObj.Unit_Price) {
            this.OrderMsg.Unit_Price = this.saveObj.Unit_Price;
            this.OrderMsg.Class_Price = this.saveObj.Unit_Price;
            this.UnitPrice = this.saveObj.Unit_Price;
          }
          if (this.saveObj.SourceId) {
            this.OrderMsg.SourceId = this.saveObj.SourceId;
          }
          if (this.saveObj.SellPriceType) {
            this.OrderMsg.Unit_PriceType = this.saveObj.SellPriceType;
          }
          if (this.saveObj.TotalClassHours) {
            this.OrderMsg.TotalClassHours = this.saveObj.TotalClassHours;
          }

          if (this.saveObj.TextbookFee) {
            this.OrderMsg.TextbookFee = this.saveObj.TextbookFee;
          }
          if (this.saveObj.CoursewareFee) {
            this.OrderMsg.CoursewareFee = this.saveObj.CoursewareFee;
          }
          if (this.saveObj.CourseId) {
            this.OrderMsg.CourseId = this.saveObj.CourseId;
          } else {
            this.OrderMsg.CourseId = "";
          }
          if (this.saveObj.B2CRatio) {
            this.OrderMsg.B2CRatio = this.saveObj.B2CRatio;
          }
          if (this.saveObj.B2CReNewRatio) {
            this.OrderMsg.B2CReNewRatio = this.saveObj.B2CReNewRatio;
          }
          if (this.saveObj.OrderCourseList && this.saveObj.OrderCourseList.length > 0) {
            this.OrderMsg.OrderCourseList = JSON.parse(JSON.stringify(this.saveObj.OrderCourseList));
          }
          if (this.selectedCourseList.length > 1) {
            return

          }
        }
        this.IsShowEditOrder = true;

        this.calcPrice();
        if (this.stuData.RenewNum > 0) {
          this.OrderMsg.SaleRemark = `首次报名优惠比例${this.saveObj.B2CRatio ??
          0}%;续费优惠比例${this.saveObj.B2CReNewRatio ?? 0}%`; //备注
        } else {
          this.OrderMsg.SaleRemark = `首次报名优惠比例${this.saveObj.B2CRatio ??
          0}%`; //备注})
        }
      },
      //筛选员工
      filterFn(val, update) {
        update(() => {
          if (val === "") {
            this.EmployeeList = JSON.parse(JSON.stringify(this.AllemployeeList));
          } else {
            const needle = val.toLowerCase();
            this.EmployeeList = this.AllemployeeList.filter(
              v => v.EmployeeName.toLowerCase().indexOf(needle) > -1
            );
          }
        });
      },
      //筛选协助老师
      filterAssisFn(val, update) {
        update(() => {
          if (val === "") {
            this.AssistList = JSON.parse(JSON.stringify(this.AllAssistList));
          } else {
            const needle = val.toLowerCase();
            this.AssistList = this.AllAssistList.filter(
              v => v.EmployeeName.toLowerCase().indexOf(needle) > -1
            );
          }
        });
      },
      //筛选课程顾问
      filterFn2(val, update) {
        update(() => {
          if (val === "") {
            this.EmployeeList2 = JSON.parse(
              JSON.stringify(this.AllemployeeList2)
            );
          } else {
            const needle = val.toLowerCase();
            this.EmployeeList2 = this.AllemployeeList2.filter(
              v => v.EmployeeName.toLowerCase().indexOf(needle) > -1
            );
          }
        });
      },
      //获取业务员
      getEmployee(n) {
        var qMsg = {
          IsLeave: 1,
          UserRole: n
        };
        queryEmployee(qMsg).then(res => {
          if (res.Code == 1) {
            if (n == 0) {
              //教师
              this.EmployeeList = res.Data;
              this.AllemployeeList = res.Data;
              let obj = {
                Id: 0,
                EmployeeName: "不限"
              };
              this.EmployeeList.unshift(obj);
            }
            if (n == 2) {
              //课程顾问
              this.EmployeeList2 = res.Data;
              this.AllemployeeList2 = res.Data;
              let obj = {
                Id: 0,
                EmployeeName: "不限"
              };
              this.EmployeeList2.unshift(obj);
            }
          }
        });
      },
      //关闭弹窗
      closeEditOrder() {
        this.IsShowEditOrder = false;
        this.$emit("close");
      },
      //获取订单来源
      getOrderSEList() {
        getOrderSourceEnumList({}).then(res => {
          if (res.Code == 1) {
            var tempArray = res.Data;
            this.SourceEnumList = tempArray;
            this.OrderMsg.OrderSource = tempArray[0].Id;
            if (this.selectedCourseList.length > 0) {
              this.courseInformationList.forEach(item => {
                return item.OrderSource = this.OrderMsg.OrderSource
              })
            }
          }
        });
      },
      //修改订单
      saveOrderInfo() {
        // if(this.selectedCourseList.length>1){
        //   this.multipleAppointmentsFun()
        // }else{
        //   this.singleAppointmentsFun()
        // }
        this.singleAppointmentsFun()
      },
      // 多个约课
      multipleAppointmentsFun() {
        //插班验证课程和生效时间
        this.courseInformationList.forEach(item => {
          item.StuIds = this.stuData.StuList.map(e => e.StuId).toString();
          item.HelpEnterId = this.OrderMsg.HelpEnterId
          item.CustomerId = this.stuData.CustomerId;
        })
        let Msg = {
          OrderList: this.courseInformationList
        }
        SetClassOrderBatch(Msg)
          .then(res => {
            this.$emit("cancelloading"); //取消按钮的加载
            if (res.Code == 1) {
              this.$q.notify({
                icon: "iconfont icon-chenggong",
                color: "accent",
                timeout: 2000,
                message: "操作成功!",
                position: "top"
              });
              //调用父页面成功方法
              this.$emit("success");
              this.IsShowEditOrder = false;
            } else {
              this.$q.notify({
                type: "negative",
                position: "top",
                message: res.Message
              });
            }
          })
          .catch(err => {
            this.$emit("cancelloading"); //取消按钮的加载
            if (err.Code == 2) {
              this.inceptionData = err;
              this.inception = true;
            }
          });
      },
      // 单个班课、约课
      singleAppointmentsFun() {
        //插班验证课程和生效时间
        if (this.OrderMsg.IsChaBan == 1) {
          this.$refs.CourseId.validate();
          this.$refs.EffectTime.validate();
          if (this.$refs.CourseId.hasError) {
            this.$q.notify({
              type: "negative",
              position: "top",
              message: `请选择课程!`
            });
            return;
          }
          if (this.$refs.EffectTime.hasError) {
            this.$q.notify({
              type: "negative",
              position: "top",
              message: `请选择生效时间!`
            });
            return;
          }
        }
        this.OrderMsg.StuIds = this.stuData.StuList.map(e => e.StuId).toString();
        this.OrderMsg.CustomerId = this.stuData.CustomerId;
        setClassOrder(this.OrderMsg)
          .then(res => {
            this.$emit("cancelloading"); //取消按钮的加载
            if (res.Code == 1) {
              this.$q.notify({
                icon: "iconfont icon-chenggong",
                color: "accent",
                timeout: 2000,
                message: "操作成功!",
                position: "top"
              });
              //调用父页面成功方法
              this.$emit("success");
              this.IsShowEditOrder = false;
            } else {
              this.$q.notify({
                type: "negative",
                position: "top",
                message: res.Message
              });
            }
          })
          .catch(err => {
            this.$emit("cancelloading"); //取消按钮的加载
            if (err.Code == 2) {
              this.inceptionData = err;
              this.inception = true;
            }
          });
      },
      goorder() {
        this.OpenNewUrl("/sale/myOrder", {
          OrderId: this.inceptionData.Data
        });
        this.inception = false;
      },
      //获取前置下拉数据
      getSelectClass() {
        var qMsg = {
          ClassId: 0
        };
        if (this.saveObj.ClassId) {
          qMsg.ClassId = this.saveObj.ClassId;
        }
        GetSelectClassOrderList(qMsg).then(res => {
          if (res.Code == 1) {
            this.beforeOrderList = res.Data;
          }
        });
      },
      getStudent(item) {
        let str = "";
        if (item && item.length > 1) {
          str = item[0].GuestName + "等" + item.length + "人";
        }
        if (item.length == 1) {
          str = item[0].GuestName;
        }
        return str;
      }
    }
  };

</script>
