<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">转订单</span>
        </div>
      </div>
      <div style="padding:20px 15px;">
        <q-input filled stack-label maxlength="100" :disable="true" :dense="false"
          @keyup.native="checkInteger(OrderMsg,'GuestNum')" v-model="OrderMsg.GuestNum"
          @input="changePrice(),countPrice()" class="col-12" label="人数" :rules="[val => !!val || '请填写人数']" />

        <q-select filled v-model="OrderMsg.ClassId" class="q-pb-lg" ref="ClassId" :options="classList"
          option-label="ClassName" option-value="ClassId" emit-value map-options :rules="[val => !!val || '请选择班级']"
          label="选择班级" @input="getClassList(OrderMsg.ClassId)" />

        <q-select filled option-value="CourseId" option-label="CourseName" ref="CourseId" v-model="OrderMsg.CourseId"
          :options="CourseList" emit-value map-options class="q-pb-lg" :rules="[val => !!val || '请选择课程']" label="选择课程"
          @input="changePrice" />
        <q-input filled stack-label :dense="false" v-model="OrderMsg.Unit_Price"
          :disable="IsShowUpPrice==true?false:true" @blur="countPrice" class="col-12" label="成交单价"
          :rules="[val => !!val || '请填成交单价']" />
        <q-toggle v-model="IsShowUpPrice" label="高于定价收生" class="q-mb-md" />
        <q-input filled stack-label :dense="false" maxlength="10" @keyup.native="checkPrice(OrderMsg,'PreferPrice')"
          v-model="OrderMsg.PreferPrice" class="col-12 q-pb-lg" label="应收" />
        <q-select 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 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 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 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 v-model="OrderMsg.IsLessPrice" :false-value="0" :true-value="1" label="是否少价" class="q-mb-md" />
        <q-input v-if="OrderMsg.IsLessPrice==1" @keyup.native="checkPrice(OrderMsg,'LessPrice')" maxlength="10" filled
          stack-label :dense="false" v-model="OrderMsg.LessPrice" class="col-12" label="少价金额" />
        <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 {
    getOrderSourceEnumList, //获取订单来源 枚举
    setClassOrder, //修改订单
    queryChaClassInfo,
    GetSelectClassOrderList //获取前置下拉
  } from '../../api/sale/sale'
  import {
    queryEmployee
  } from '../../api/users/user'; //获取员工
  import {
    getClassDropDownList
  } from '../../api/school/index'; //获取校区列表

  import {
    mapState
  } from "vuex";

  export default {
    name: "editOrder-form",
    props: {
      saveObj: {
        type: Object,
        default: null,
      }
    },
    data() {
      return {
        IsShowEditOrder: true,
        classList: [],
        OrderMsg: {
          VisitorReserveId: 0,
          ClassId: 0, //班级编号
          GuestNum: 1, //人数
          Unit_Price: 0,
          PreferPrice: 0,
          OrderSource: 0,
          SaleRemark: "",
          Class_Price: 0,
          OrderId: 0,
          OrderType: 1, //订单类型(1-班级课程订单，2-留学就业订单)
          SourceId: 0, //来源编号
          HelpEnterId: 0, //协助老师编号
          GeneralOccupation: "", //一般同行
          EduOccupation: "", //教育同行
          IsLessPrice: 0, //是否少价 0 不少 1 少
          LessPrice: 0, //少价金额
          OrderNature: 1, //订单性质
          OldPreferPrice: 0, //原实际应收
          IsChaBan: 0, //是否插班报入(1-是)
          CourseId: '', //课程编号
          StartClassHours: 0, //已上课时
          EffectTime: '', //生效时间
          UpOrderId: 0, //前置订单编号
        },
        IsShowUpPrice: false, //是否显示高于定价
        UnitPrice: 0,
        SourceEnumList: [], //订单来源
        EmployeeList: [], //员工列表
        AllemployeeList: [], //所有员工列表
        CourseList: [], //课程列表
        courseObj: {}, //选择的课程
      }
    },
    created() {
      this.getOrderSEList();
      this.getEmployee();

    },
    mounted() {
      this.initData()
      this.getClass();
    },
    watch: {

    },
    methods: {
      initData() {
        if (this.saveObj.AccountId && this.saveObj.AccountId > 0) {
          this.OrderMsg.HelpEnterId = this.saveObj.AccountId;
        }
      },
      //获取课程信息
      getClassList(ClassId) {
        this.OrderMsg.CourseId = '';
        this.CourseList = [];
        var qMsg = {
          ClassId: ClassId
        };
        queryChaClassInfo(qMsg).then(res => {
          if (res.Code == 1) {
            this.CourseList = res.Data.otherCourse;
          }
        });
      },
      //获取班级列表
      getClass() {
        getClassDropDownList({
          IsAddDefault: 0,
          IsQuerySurplus: 1
        }).then(res => {
          if (res.Code == 1) {
            let tempArray = res.Data;
            if (!tempArray) {
              tempArray = [];
            }
            tempArray.unshift({
              ClassId: 0,
              ClassName: "不限"
            })
            this.classList = tempArray;
          }
        })
      },
      //选择课程切换价格
      changePrice() {
        let temp = this.CourseList.find(x => x.CourseId == this.OrderMsg.CourseId);
        if (temp) {
          this.courseObj = temp;
          if (this.OrderMsg.GuestNum == 1) {
            if (temp.CoursePriceList && temp.CoursePriceList.length > 0) {
              if (temp.CoursePriceList[0].PriceType == 0) {
                let DisCount = (100 - temp.CoursePriceList[0].PriceMoney) / 100;
                this.OrderMsg.Unit_Price = temp.SellPrice * DisCount;
                this.UnitPrice = temp.SellPrice * DisCount;
              } else {
                this.OrderMsg.Unit_Price = temp.SellPrice - item.CoursePriceList[0].PriceMoney;
                this.UnitPrice = temp.SellPrice - item.CoursePriceList[0].PriceMoney;
              }
            } else {
              this.OrderMsg.Unit_Price = temp.SellPrice;
              this.UnitPrice = temp.SellPrice;
            }
          }
          if (this.OrderMsg.GuestNum > 1) {
            if (temp.CoursePriceList && temp.CoursePriceList.length > 0) {
              if (temp.CoursePriceList[1].PriceType == 0) {
                let DisCount = (100 - temp.CoursePriceList[1].PriceMoney) / 100;
                this.OrderMsg.Unit_Price = temp.SellPrice * DisCount;
                this.UnitPrice = temp.SellPrice * DisCount;
              } else {
                this.OrderMsg.Unit_Price = temp.SellPrice - item.CoursePriceList[1].PriceMoney;
                this.UnitPrice = temp.SellPrice - item.CoursePriceList[1].PriceMoney;
              }
            } else {
              this.OrderMsg.Unit_Price = temp.SellPrice;
              this.UnitPrice = temp.SellPrice;
            }
          }
        } else {
          this.OrderMsg.Unit_Price = 0;
          this.UnitPrice = 0;
          this.courseObj = {};
        }
        this.countPrice();
      },
      //筛选员工
      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);
            this.AllemployeeList.unshift(obj);
          }
        }).catch(() => {})
      },
      //关闭弹窗
      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;
          }
        })
      },
      //计算价格
      countPrice() {
        var guestNum = 0;
        var unit_price = 0;
        if (this.OrderMsg.GuestNum && this.OrderMsg.GuestNum > 0) {
          guestNum = Number(this.OrderMsg.GuestNum);
        }
        if (this.OrderMsg.Unit_Price && this.OrderMsg.Unit_Price > 0) {
          unit_price = Number(this.OrderMsg.Unit_Price);
        }
        //计算应收价格
        if (this.OrderMsg.IsChaBan == 1) {
          var newPreferPrice = 0;
          if (this.courseObj && this.courseObj.CourseId && this.courseObj.CourseId > 0) {
            newPreferPrice = (this.UnitPrice / this.courseObj.ClassHours) * (this.courseObj.ClassHours - this
              .OrderMsg.StartClassHours) * guestNum
          }
          this.OrderMsg.PreferPrice = Number(newPreferPrice).toFixed(2);
        } else {
          this.OrderMsg.PreferPrice = Number(guestNum * unit_price).toFixed(2);
        }
        if (this.OrderMsg.OrderType == 2) {
          this.OrderMsg.OldPreferPrice = Number(guestNum * unit_price).toFixed(2);
        }
      },
      //修改订单
      saveOrderInfo() {
        this.OrderMsg.PreferPrice = (this.OrderMsg.GuestNum * this.OrderMsg.Unit_Price) - this.OrderMsg.LessPrice;
        this.OrderMsg.VisitorReserveId = this.saveObj.Id;
        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
            })
          }
        }).catch(() => {})
      }
    }
  }

</script>
