<!--订单操作-->
<template>
  <div>
    <div class="q-mb-sm text-accent">
      <span>{{ saveObj.ClassName }}</span>
    </div>
    <div style="row 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" />
        <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 || '请填成交单价']" />
        <q-input filled stack-label :dense="false" v-model="OrderMsg.DiscountMoney" :disable="true"
          class="col-6 q-py-sm" label="优惠金额" />
        <q-input filled stack-label :disable="true" :dense="false" maxlength="10"
          @keyup.native="checkPrice(OrderMsg, 'PreferPrice')" v-model="OrderMsg.PreferPrice" class="col-6 q-py-sm"
          label="应收" />
        <q-select style="display:none;" 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-model="OrderMsg.HelpEnterId" :disable="HelpEnterDisable" :options="EmployeeList" filled use-input label="协助老师"
          option-label="EmployeeName" option-value="Id" ref="EmployeeName" class="col-6 q-py-sm" 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-select v-if="false" v-model="OrderMsg.CourseConsultantId" :disable="CourseConsultantDisable" :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
          v-if="OrderMsg.EnterId > 0"
          filled
          stack-label
          v-model="EnterName"
          class="col-6 q-py-sm"
          label="市场人员"
        />

        <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 {
    getClassOrderInfo, //获取订单操作日志列表
    getOrderSourceEnumList, //获取订单来源 枚举
    setClassOrder, //修改订单
    queryChaClassInfo,
  } from "../../../../api/sale/sale";
  import {
    queryEmployee
  } from "../../../../api/users/user"; //获取员工
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      },
      select: {
        type: Array,
        default: () => []
      },
    },
    data() {
      return {
        IsShowEditOrder: true,

        UnitPrice: 0,
        SourceEnumList: [], //订单来源
        EmployeeList: [], //教师列表
        AllemployeeList: [], //所有教师列表
        EmployeeList2: [], //课程顾问列表
        AllemployeeList2: [], //所有课程顾问列表
        inception: false,
        inceptionData: null,
        OrderMsg: {
          OrderType: 2,
          GuestNum: 1, //人数
          Unit_Price: '', //成交单价
          DiscountMoney: 0, //优惠金额
          PreferPrice: '', //应收
          OrderSource: '', //客人来源
          HelpEnterId: '', //协助老师
          CourseConsultantId: '', //课程顾问
          SourceId: 1, //传入Id
          SaleRemark: '', //备注
          EnterId: 0, //市场人员
          StuIds:'',
          OrderCourseList:[],
        },
        EnterName:'',
        HelpEnterDisable:false,
        CourseConsultantDisable:false
      };
    },
    created() {
      if (this.saveObj) {
        this.OrderMsg.SourceId = this.saveObj.Id;
        this.OrderMsg.Unit_Price = this.saveObj.SellPrice;
        this.OrderMsg.PreferPrice = this.OrderMsg.Unit_Price;
        this.OrderMsg.OrderCourseList = JSON.parse(JSON.stringify(this.saveObj.OrderCourseList));
      }
      if(this.select&&this.select.length>0){
        this.OrderMsg.EnterId = this.select[0].CreateBy;
        this.EnterName = this.select[0].CreateByName;
        this.OrderMsg.StuIds = this.select[0].StuId;
        if(this.select[0].AssistList&&this.select[0].AssistList.length>0){
          this.select[0].AssistList.map(e => {
            if (e.AssistType == 4) {
              this.OrderMsg.HelpEnterId = e.AssistId;
              this.HelpEnterDisable = true;
            }
            if (e.AssistType == 2) {
              this.OrderMsg.CourseConsultantId = e.AssistId;
              this.CourseConsultantDisable = true;
            }
          });
        }
      }
      this.getOrderSEList();
      this.getEmployee(0);
      this.getEmployee(2);
      this.initConfig();
    },
    mounted() {
      this.initData();
    },
    methods: {
      //获取班级课程信息
      initConfig() {

      },
      //计算课程单价和应收金额
      calcPrice() {

      },
      initData() {

      },
      //筛选员工
      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
            );
          }
        });
      },
      //筛选课程顾问
      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;
          }
        });
      },
      //修改订单
      saveOrderInfo() {
        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;
      },
    }
  };

</script>
