<!--订单操作-->
<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="drawerTop">
        <div style="display:flex;align-items:center;margin:20px 0 0 10px;">
          <span class="drawer_Span">{{OrderMsg.OrderId>0?"修改订单":"立即下单"}}</span>
        </div>
      </div>
      <div style="padding:20px 15px;">
        <q-input filled stack-label maxlength="100" :dense="false" v-model="OrderMsg.GuestNum" type="number"
          @input="countPrice" class="col-12" label="人数" :rules="[val => !!val || '请填写人数']"
          :disable="modityOrderType!=1" />
        <q-input filled stack-label :dense="false" v-model="OrderMsg.Unit_Price" :disable="true" @blur="countPrice"
          class="col-12" label="成交单价" :rules="[val => !!val || '请填成交单价']" />
        <q-select :disable="modityOrderType!=1" 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 standout="bg-primary text-white" option-value="Id" class="q-pb-lg" option-label="Name"
          v-model="OrderMsg.OrderNature" :options="OrderStatus" emit-value map-options label="订单性质"
          @input="OrderNatureChange" />
        <template v-if="OrderMsg.OrderNature==1">
          <q-input filled stack-label :dense="false" maxlength="10" @keyup.native="checkPrice(OrderMsg,'PreferPrice')"
            :disable="true" v-model="OrderMsg.PreferPrice" class="col-12 q-pb-lg" label="应收" />
        </template>
        <template v-else>
          <q-input filled stack-label :dense="false" maxlength="10" @keyup.native="checkPrice(OrderMsg,'PreferPrice')"
            :disable="!isHaveModify" v-model="OrderMsg.PreferPrice" class="col-12 q-pb-lg" label="应收" />
        </template>
        <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, //修改订单
  } 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-总经理修改
      },
    },
    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, //少价金额
          OrderNature: 1, //订单性质
          OldPreferPrice: 0, //原实际应收
          ClassHour: 1 //已上课时
        },
   
        UnitPrice: 0,
        SourceEnumList: [], //订单来源
        EmployeeList: [], //员工列表
        AllemployeeList: [], //所有员工列表
        OrderStatus: [{
            Id: 1,
            Name: '直通车'
          },
          {
            Id: 2,
            Name: '代收代付'
          },
          {
            Id: 3,
            Name: '返佣'
          }
        ],
        isHaveModify: false,
 
      }
    },
    created() {
      this.getOrderSEList();
      this.getEmployee();
      var localStorageData = window.localStorage["loginUserInfo"];
      let ActionMenuList = JSON.parse(localStorageData).data.ActionMenuList
      if (ActionMenuList && ActionMenuList.length > 0) {
        ActionMenuList.forEach(x => {
          if (x.FunctionCode == 'E_ModifyStudyOrderPrice') {
            this.isHaveModify = true
          }
        })
      }
    },
    mounted() {
      this.initData()
    },
    methods: {
      //订单性质切换
      OrderNatureChange() {
        if (this.OrderMsg.OrderNature == 1) {
          this.countPrice();
        }
      },
      initData() {
        if (this.saveObj && this.saveObj.OrderId > 0) {
          getClassOrderInfo({
            OrderId: this.saveObj.OrderId
          }).then(res => {
            if (res.Code == 1) {
              this.OrderMsg.ClassId = res.Data.OrderInfo.ClassId;
              this.OrderMsg.GuestNum = res.Data.OrderInfo.GuestNum;
              this.OrderMsg.Unit_Price = res.Data.OrderInfo.Unit_Price;
              this.OrderMsg.PreferPrice = res.Data.OrderInfo.PreferPrice;
              this.OrderMsg.OrderSource = res.Data.OrderInfo.OrderSource;
              this.OrderMsg.SaleRemark = res.Data.OrderInfo.SaleRemark;
              this.OrderMsg.Class_Price = res.Data.OrderInfo.Class_Price;
              this.OrderMsg.OrderId = res.Data.OrderInfo.OrderId;
              this.OrderMsg.OrderType = res.Data.OrderInfo.OrderType;
              this.OrderMsg.SourceId = res.Data.OrderInfo.SourceId;
              this.OrderMsg.HelpEnterId = res.Data.OrderInfo.HelpEnterId;
              this.OrderMsg.GeneralOccupation = res.Data.OrderInfo.GeneralOccupation;
              this.OrderMsg.EduOccupation = res.Data.OrderInfo.EduOccupation;
              this.OrderMsg.IsLessPrice = res.Data.OrderInfo.IsLessPrice;
              this.OrderMsg.LessPrice = res.Data.OrderInfo.LessPrice;
              this.OrderMsg.OrderNature = res.Data.OrderInfo.OrderNature;
              this.OrderMsg.OldPreferPrice = res.Data.OrderInfo.OldPreferPrice;
              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.OrderNature = 0;
          this.OrderMsg.OldPreferPrice = 0;
          this.OrderMsg.GuestNum = 1;
          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.UnitPrice = this.saveObj.Unit_Price;
            }
            if (this.saveObj.SourceId) {
              this.OrderMsg.SourceId = this.saveObj.SourceId;
            }
          }
          this.countPrice();
          this.IsShowEditOrder = true;
        }
      },
      //筛选员工
      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);
        }
        this.OrderMsg.PreferPrice = Number(guestNum * unit_price).toFixed(2);
        if (this.OrderMsg.OrderType == 2) {
          this.OrderMsg.OldPreferPrice = Number(guestNum * unit_price).toFixed(2);
        }
      },
      //修改订单
      saveOrderInfo() {
        if (this.OrderMsg.OrderType == 1) {
          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
            })
          }
        }).catch(() => {})
      },
    }
  }

</script>
