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

  .disableStatus {
    opacity: 0.3 !important;
    cursor: auto;
  }
</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.Id>0?"修改订单":"立即下单"}}</span>
        </div>
      </div>
      <div style="padding:20px 15px;">
        <q-select standout="bg-primary text-white" v-model="OrderMsg.EduStudentId" class="col-12  q-mb-lg"
          :options="stuList" clearable emit-value map-options label="学员" option-value="StuId" option-label="StuName"
          @input="stuChange" />
        <q-input filled stack-label maxlength="30" :dense="false" v-model="OrderMsg.LinkMan" class="col-12"
          ref="LinkMan" label="姓名" :disable="isdisable" :rules="[val => !!val || '请填写姓名']" />
        <q-input filled stack-label maxlength="30" :dense="false" type="number" :disable="isdisable"
          v-model="OrderMsg.LinkTel" class="col-12" ref="LinkTel" label="联系电话" :rules="[val => !!val || '请填写联系电话']" />
        <q-select standout="bg-primary text-white" v-model="OrderMsg.Sex" class="col-12  q-mb-lg" :options="sexList"
          :disable="isdisable" emit-value map-options label="性别" option-value="Id" option-label="name" />

        <q-select standout="bg-primary text-white" v-model="OrderMsg.TeacherId" class="col-12  q-mb-lg"
          :options="TeacherList" clearable emit-value map-options label="教师" option-value="TId"
          option-label="TeacherName" />

        <q-input filled stack-label :dense="false" v-model="OrderMsg.Remark" style="margin-top: 20px" type="textarea"
          class="col-12 q-mb-lg" ref="Remark" label="备注" />
        <div>
          单价：<span style="color:#f00" v-if="OrderMsg.UnitPrice">{{OrderMsg.UnitPrice}}元</span>
        </div>
        <div>
          总价：<span style="color:#f00" v-if="OrderMsg.UnitPrice">{{OrderMsg.UnitPrice*OrderMsg.PeopleNum}}元</span>
        </div>
        <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="保存" v-if="saveObj.PaymentWay!==1&&saveObj.Status!==1"
            @click="saveOrderInfo()" />
          <q-btn color="accent disableStatus" class="q-mr-md" v-else label="保存" />
        </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 {
    getTeacherDropDownList
  } from '../../api/school/index'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null,
      },
      sourceType: {
        type: Number,
        default: 1
      }
    },
    data() {
      return {
        IsShowEditOrder: true,
        OrderMsg: {
          Id: 0, //订单id
          LinkMan: "", //联系人
          LinkTel: "", //联系电话
          Remark: "", //备注
          ActivityId: 0, //活动id
          PeopleNum: 1, //人数
          Sex: 1, //性别
          EduStudentId: 0, //学员id
          UnitPrice: 0, //单价
          Money: 0, //总价
          EduUserId: 0, //销售id
          PayWay: 0, //付款方式
          TenantId: 0, //商户id
          MallBaseId: 0, //小程序id
          TeacherId: "", //教师编号
        },
        stuList: [], //学员列表
        loginId: -1, //登录人id
        sexList: [{
            Id: 1,
            name: "男"
          },
          {
            Id: 2,
            name: "女"
          }
        ],
        TeacherList: [], //教师列表
        isdisable: false,
      }
    },
    created() {
      this.getTeacherList();
    },
    mounted() {
      let data = JSON.parse(localStorage.getItem("loginUserInfo")).data
      this.loginId = data.Id
      this.OrderMsg.EduUserId = data.Id
      this.OrderMsg.TenantId = data.JHTenantId
      this.OrderMsg.MallBaseId = data.JHMallBaseId
      if (this.saveObj) {
        if (this.sourceType === 1) {
          this.OrderMsg.ActivityId = this.saveObj.Id
          this.OrderMsg.UnitPrice = this.saveObj.Price
        } else if (this.sourceType === 2) {
          this.OrderMsg.Id = this.saveObj.Id
          this.OrderMsg.ActivityId = this.saveObj.ActivityId
          this.OrderMsg.UnitPrice = this.saveObj.UnitPrice
          this.OrderMsg.EduStudentId = this.saveObj.EduStudentId
        }
        this.OrderMsg.LinkMan = this.saveObj.LinkMan
        this.OrderMsg.LinkTel = this.saveObj.LinkTel
        this.OrderMsg.Remark = this.saveObj.Remark;
        this.OrderMsg.TeacherId = this.saveObj.TeacherId;
      }
      this.getStu();
    },
    watch: {

    },
    methods: {
      //获取教师列表
      getTeacherList() {
        getTeacherDropDownList({}).then(res => {
          if (res.Code == 1) {
            this.TeacherList = res.Data;
          }
        })
      },
      //关闭弹窗
      closeEditOrder() {
        this.IsShowEditOrder = false;
        this.$emit('close')
      },
      saveOrderInfo() {
        this.$refs.LinkMan.validate();
        this.$refs.LinkTel.validate();

        if (!this.$refs.LinkMan.hasError &&
          !this.$refs.LinkTel.hasError
        ) {
          if (this.OrderMsg.EduStudentId === null || !this.OrderMsg.EduStudentId) {
            this.OrderMsg.EduStudentId = 0
          }
          this.OrderMsg.Money = this.OrderMsg.UnitPrice * this.OrderMsg.PeopleNum
          this.apipostDS("/api/Education/SetActivityEnrollInfo", this.OrderMsg, (res) => {
            if (res.data.resultCode === 1) {
              this.Success("操作成功");
              this.closeEditOrder();
              this.$emit('success');
            } else {
              this.Error(res.data.message);
            }
          })
        }
      },
      getStu() {
        this.apipostDS("/api/Education/GetEduStudent", {
          EduUserId: this.loginId
        }, (res) => {
          if (res.data.resultCode === 1) {
            let data = res.data.data
            this.stuList = data.map(item => {
              if (item.StuSex === 0) {
                item.StuSex = 1
              } else if (item.StuSex === 1) {
                item.StuSex = 2
              }
              return item
            })
          } else {
            this.Error(res.data.message);
          }
        })
      },
      stuChange(val) {
        let res = this.stuList.filter(item => {
          return item.StuId == val
        })
        if (res.length > 0) {
          this.OrderMsg.LinkMan = res[0].StuName
          this.OrderMsg.LinkTel = res[0].StuTel
          this.OrderMsg.Sex = res[0].StuSex
        }
        if (val > 0) {
          this.isdisable = true
        } else {
          this.isdisable = false
        }
      }
    }
  }
</script>