<style>
  .editOrderDrawerTop {
    width: 100%;
    height: 50px;
    display: flex;
    justify-content: space-between;
    background-color: #f0f5fb;
    padding: 5px 10px;
    align-items: center;
  }

</style>
<!--订单操作-->
<template>
  <q-dialog v-model="IsShowEditOrder" maximized full-height seamless position="right">
    <q-card style="margin-top:61px;width:500px" class="no-border-radius classinfo_Dialog">
      <div class="editOrderDrawerTop">
        <div style="display:flex;align-items:center;margin-left:10px;">
          <span class="drawer_Span">{{OrderMsg.OrderId>0?"修改订单":"立即下单"}}</span>
        </div>
      </div>
      <div style="padding:20px 15px;">
        <q-input filled stack-label maxlength="3" :dense="false" @keyup.native="checkInteger(OrderMsg,'GuestNum')"
          v-model="OrderMsg.GuestNum" @input="calcPrice()" class="col-12" label="人数" :rules="[val => !!val || '请填写人数']"
          :disable="modityOrderType==2" />
        <template v-if="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="q-pb-lg"
            :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-12 q-pb-lg" label="起始课时" />
          <q-input filled v-model="OrderMsg.EffectTime" ref="EffectTime" :rules="[val => !!val || '请填生效时间']"
            class="col-6 q-pb-lg" :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="q-pb-lg"
            :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-12 q-pb-lg"
          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-12" 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-12" label="成交单价" :rules="[val => !!val || '请填成交单价']" />
        </template>

        <q-input filled stack-label :dense="false" v-model="OrderMsg.PerDiscountMoney" :disable="true"
          class="col-12 q-pb-lg" label="优惠金额(每人)" :hint="'总优惠金额：'+(OrderMsg.PerDiscountMoney*OrderMsg.GuestNum)" />

        <template v-if="modityOrderType==3">
          <q-input filled stack-label :dense="false" v-model="OrderMsg.Unit_Price" @input="calcPrice()" class="col-12"
            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-12 q-pb-lg"
          label="应收" />
        <q-select :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="q-pb-lg"
          label="客人来源" />
        <q-select :disable="(modityOrderType==2)||(OrderMsg.JoinType==3)" v-model="OrderMsg.HelpEnterId"
          :options="EmployeeList" filled use-input label="协助老师" option-label="EmployeeName" option-value="Id"
          ref="EmployeeName" class="col-6 q-pb-lg" emit-value map-options @filter="filterFn">
          <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-12 q-pb-lg" label="一般同行" />
        <q-input :disable="(modityOrderType==2)" v-if="OrderMsg.OrderSource==7" filled stack-label :dense="false"
          maxlength="50" v-model="OrderMsg.EduOccupation" class="col-12 q-pb-lg" label="教育同行" />
        <q-toggle :disable="(modityOrderType==2)" v-model="OrderMsg.IsLessPrice" :false-value="0" :true-value="1"
          label="是否少价" class="q-mb-md" />
        <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-12" label="少价金额(每人)"
          :hint="'总少价金额：'+(OrderMsg.PerLessMoney*OrderMsg.GuestNum)" />
        <q-input filled stack-label :dense="false" v-model="OrderMsg.SaleRemark" style="margin-top: 20px"
          type="textarea" class="col-12" label="备注" />
        <div style="margin:30px 10px 70px 0;">
          <q-btn class="q-mr-md" label="取消" @click="closeEditOrder" />
          <q-btn color="accent" class="q-mr-md" label="保存" @click="saveOrderInfo()" />
        </div>
      </div>
    </q-card>
    <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>
</template>
<script>
  import {
    getClassOrderInfo, //获取订单操作日志列表
    getOrderSourceEnumList, //获取订单来源 枚举
    setClassOrder, //修改订单
    queryChaClassInfo,
    GetSelectClassOrderList //获取前置下拉
  } from '../../api/sale/sale'
  import {
    queryEmployee
  } from '../../api/users/user'; //获取员工
  import {
    mapState
  } from "vuex";

  export default {
    name: "editOrder-form",
    props: {
      saveObj: {
        type: Object,
        default: null,
      },
      //订单类型
      orderType: {
        type: Number,
        default: 1,
      },
      //修改订单类型
      modityOrderType: {
        type: Number,
        default: 0, //1-销售修改，2-OP修改，3-总经理修改
      },
      isChaBan: {
        type: Number,
        default: 0, //1-是插班报入
      }
    },
    data() {
      return {
        IsShowEditOrder: true,
        OrderMsg: {
          ClassId: 0, //班级编号
          GuestNum: 0, //人数
          Unit_Price: 0,
          PreferPrice: 0,
          OrderSource: 0,
          SaleRemark: "",
          Class_Price: 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, //每人优惠金额
        },
        Unit_PriceRemark: "", //单价规则
        IsShowUpPrice: false, //是否显示高于定价
        UnitPrice: 0,
        SourceEnumList: [], //订单来源
        EmployeeList: [], //员工列表
        AllemployeeList: [], //所有员工列表
        CourseList: [], //课程列表
        courseObj: {}, //选择的课程
        beforeOrderList: [], //前置订单数据
      }
    },
    created() {
      this.OrderMsg.IsChaBan = this.isChaBan;
      //HK2021-08-06新增
      if (this.saveObj && this.saveObj.CourseId) {
        this.OrderMsg.CourseId = this.saveObj.CourseId;
      }
      this.getOrderSEList();
      this.getEmployee();
      this.initConfig();
      this.getSelectClass();
    },
    mounted() {
      this.initData()
    },
    watch: {

    },
    methods: {
      //获取班级课程信息
      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() {
        var guestNum = 0;
        if (this.OrderMsg.GuestNum && this.OrderMsg.GuestNum > 0) {
          guestNum = Number(this.OrderMsg.GuestNum);
        }
        let temp = this.CourseList.find(x => x.CourseId == this.OrderMsg.CourseId);
        if (temp) {
          this.courseObj = temp;
          var tempDiscountMoney = 0; //优惠金额
          var tempSaleRemark = ""; //备注
          //插班课时单价
          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;
          this.OrderMsg.DiscountMoney = perDiscountMoney * guestNum;
          //计算每人优惠和总优惠
          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) {
            newPreferPrice = chaBanPrice * guestNum;
          }
          this.OrderMsg.PreferPrice = Number(newPreferPrice).toFixed(2) - this.OrderMsg.LessPrice;
        } else {
          this.OrderMsg.PreferPrice = Number(guestNum * unit_price).toFixed(2) - this.OrderMsg.LessPrice;
        }
        //留学就业订单
        if (this.OrderMsg.OrderType == 2) {
          this.OrderMsg.OldPreferPrice = Number(guestNum * unit_price).toFixed(2);
        }
      },
      initData() {
        if (this.saveObj && this.saveObj.OrderId > 0) {
          getClassOrderInfo({
            OrderId: this.saveObj.OrderId
          }).then(res => {
            if (res.Code == 1) {
              var tempData = res.Data.OrderInfo;
              this.OrderMsg.ClassId = tempData.ClassId;
              this.OrderMsg.GuestNum = tempData.GuestNum;
              this.OrderMsg.Unit_Price = tempData.Unit_Price;
              this.OrderMsg.PreferPrice = tempData.PreferPrice;
              this.OrderMsg.OrderSource = tempData.OrderSource;
              this.OrderMsg.SaleRemark = tempData.SaleRemark;
              this.OrderMsg.Class_Price = tempData.Class_Price;
              this.OrderMsg.OrderId = tempData.OrderId;
              this.OrderMsg.OrderType = tempData.OrderType;
              this.OrderMsg.SourceId = tempData.SourceId;
              this.OrderMsg.HelpEnterId = tempData.HelpEnterId;
              this.OrderMsg.GeneralOccupation = tempData.GeneralOccupation;
              this.OrderMsg.EduOccupation = tempData.EduOccupation;
              this.OrderMsg.IsLessPrice = tempData.IsLessPrice;
              this.OrderMsg.LessPrice = tempData.LessPrice;
              this.OrderMsg.PerLessMoney = tempData.PerLessMoney;
              this.OrderMsg.OrderNature = tempData.OrderNature;
              this.OrderMsg.OldPreferPrice = tempData.OldPreferPrice;
              this.OrderMsg.CourseId = tempData.CourseId;
              this.OrderMsg.JoinType = tempData.JoinType;
              this.OrderMsg.StartClassHours = tempData.StartClassHours;
              this.OrderMsg.IsChaBan = tempData.IsChaBan;
              this.OrderMsg.EffectTime = tempData.EffectTime;
              this.OrderMsg.UpOrderId = tempData.UpOrderId;
              this.OrderMsg.DiscountMoney = tempData.DiscountMoney;
              this.OrderMsg.PerDiscountMoney = tempData.PerDiscountMoney;
              this.UnitPrice = this.OrderMsg.Unit_Price;
              this.IsShowEditOrder = true;
            }
          })
        } else {
          this.OrderMsg.SaleRemark = ''; //清空备注
          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.GuestNum = 1;
          this.OrderMsg.StartClassHours = 0;
          this.OrderMsg.IsChaBan = this.isChaBan;
          this.OrderMsg.EffectTime = "";
          this.OrderMsg.UpOrderId = 0;
          this.OrderMsg.JoinType = 1;
          this.OrderMsg.DiscountMoney = 0;
          this.OrderMsg.PerDiscountMoney = 0;
          this.OrderMsg.OrderType = this.orderType;
          if (this.OrderMsg.OrderType == 2) {
            this.OrderMsg.OrderNature = 1;
          }
          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.CourseId) {
              this.OrderMsg.CourseId = this.saveObj.CourseId;
            } else {
              this.OrderMsg.CourseId = '';
            }
          }
          this.IsShowEditOrder = true;
          this.calcPrice();
        }
      },
      //筛选员工
      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);
          }
        })
      },
      //获取业务员
      getEmployee() {
        var qMsg = {
          Dept_Id: 0
        }
        queryEmployee(qMsg).then(res => {
          if (res.Code == 1) {
            this.EmployeeList = res.Data;
            this.AllemployeeList = res.Data;
            let obj = {
              Id: 0,
              EmployeeName: '不限'
            }
            this.EmployeeList.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;
          }
        })
      },
      //修改订单
      saveOrderInfo() {
        //插班验证课程和生效时间
        if (this.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;
          }
        }

        //插班报名计算应收
        if (this.OrderMsg.IsChaBan == 1) {
          if (this.modityOrderType == 1 || this.modityOrderType == 2) {
            if (!this.courseObj || (this.courseObj && !this.courseObj.ClassHours)) {
              if (this.OrderMsg && this.OrderMsg.CourseId) {
                this.courseObj = this.CourseList.find(x => x.CourseId == this.OrderMsg.CourseId);
              }
            }
            //插班课时单价
            var classHourPrice = this.courseObj.SellPrice / this.courseObj.ClassHours;
            var chaBanPrice = Number(classHourPrice * (this.courseObj.ClassHours - this.OrderMsg.StartClassHours))
              .toFixed(2);
            var newPreferPrice = chaBanPrice * this.OrderMsg.GuestNum - this.OrderMsg.LessPrice;
            this.OrderMsg.PreferPrice = Number(newPreferPrice).toFixed(2);
          }
        }
        //正常报名计算应收
        if (this.OrderMsg.IsChaBan == 0) {
          if (this.modityOrderType == 1 || this.modityOrderType == 2) {
            this.OrderMsg.PreferPrice = (this.OrderMsg.GuestNum * this.OrderMsg.Unit_Price) - this.OrderMsg.LessPrice;
          }
        }
        setClassOrder(this.OrderMsg).then(res => {
          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
            })
          }
        })
      },
      //获取前置下拉数据
      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>
