<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.StuId==0?'新增学员信息':'修改学员信息'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-md q-px-xs text-grey-6">基本资料</div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.StuName" ref="StuName"
            class="col-6 q-pr-lg q-pb-lg" label="学员名称" :rules="[val => !!val || '请填写学员姓名']" />
          <q-select filled stack-label option-value="value" option-label="label" v-model="objOption.StuSex" ref="StuSex"
            :options="SexOpts" label="性别" :dense="false" class="col-6 q-pb-lg q-pb-lg" emit-value map-options />
          <q-input type="date" filled stack-label maxlength="20" :dense="false" v-model="objOption.StuBirth" ref="StuBirth"
            class="col-6 q-pr-lg q-pb-lg" label="出生日期" :rules="[val => !!val || '请填写出生日期']" />
          <q-input type="tel" filled stack-label maxlength="100" :dense="false" v-model="objOption.StuTel" ref="StuTel"
            class="col-6 q-pr-lg q-pb-lg" label="联系电话" :rules="[val => !!val || '请填写学员联系电话']" />
          <div class="col-6 q-pb-lg">
            <q-uploader style="display: inline-block;max-height: 320px;max-width: 100%; background-repeat:no-repeat"
              :style="{'background-image':'url(' + objOption.StuIcon + ')'}" max-files="1" hide-upload-btn
              @rejected="onRejected" label="学员头像" :max-file-size="512*1024" accept=".jpg, image/*" auto-upload
              :factory="uploadFile" no-thumbnails>
            </q-uploader>
          </div>
          <q-select filled stack-label option-value="SId" option-label="SName" v-model="objOption.School_Id"
            ref="School_Id" :options="schoolList" label="所属校区" :dense="false" class="col-6 q-pb-lg" emit-value
            map-options :rules="[val => !!val || '请选择所属校区']" />
          <q-select filled stack-label option-value="ID" option-label="Name" v-model="objOption.ProviceId"
            ref="ProviceId" :options="ProviceList" label="省" :dense="false" class="col-6 q-pb-lg" emit-value map-options
            :rules="[val => !!val || '请选择省份']" @input="getAreaTree(2,objOption.ProviceId)" />
          <q-select filled stack-label option-value="ID" option-label="Name" v-model="objOption.CityId" ref="CityId"
            :options="CityList" label="市" :dense="false" class="col-6 q-pb-lg q-pb-lg" emit-value map-options
            :rules="[val => !!val || '请选择城市']" @input="getAreaTree(3,objOption.CityId)" />
          <q-select filled stack-label option-value="ID" option-label="Name" v-model="objOption.AreaId" ref="AreaId"
            :options="AreaList" label="区/县" :dense="false" class="col-6 q-pb-lg" emit-value map-options
            :rules="[val => !!val || '请选择区域']" />
        </div>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent" class="q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="setStudent" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    getSchoolDropdown,
    saveStudent
  } from '../../../api/school/index'
  import {
    UploadSelfFile,
    queryAreaList
  } from '../../../api/common/common'
  export default {
    components: {
    },
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          StuId: 0, //学生编号
          School_Id: 0, //学校编号
          StuName: "", //学生姓名
          StuTel: '', //学生电话
          StuIcon: '', //学生头像
          StuSex: 0, //性别
          StuBirth: "", //出生日期
          ProviceId: 0, //省份编号
          CityId: 0, //城市编号
          AreaId: 0, //地区编号
        },
        SexOpts: [{
            label: '男',
            value: 0
          },
          {
            label: '女',
            value: 1
          },
        ],
        optionTitle: "",
        schoolList: [],
        saveLoading: false,
        ProviceList: [], //省份列表
        CityList: [], //城市列表
        AreaList: [], //地区列表
      }
    },
    created() {
      this.getSchool();
    },
    mounted() {
      this.getAreaTree(1, 1);
      this.initObj()
    },
    methods: {
      getAreaTree(type, parentId) {
        queryAreaList({
          ParentID: parentId
        }).then(res => {
          if (res.Code == 1) {
            if (type == 1) {
              this.ProviceList = res.Data;
            } else if (type == 2) {
              this.CityList = res.Data;
            } else if (type == 3) {
              this.AreaList = res.Data;
            }
            this.$forceUpdate();
          }
        });
      },
      initObj() {
        if (this.saveObj) {
          this.optionTitle = "修改学员信息"
          this.objOption.StuId = this.saveObj.StuId;
          this.objOption.School_Id = this.saveObj.School_Id;
          this.objOption.StuName = this.saveObj.StuName;
          this.objOption.StuTel = this.saveObj.StuTel;
          this.objOption.StuIcon = this.saveObj.StuIcon;
          this.objOption.StuSex = this.saveObj.StuSex;
          this.objOption.StuBirth = this.saveObj.StuBirthStr;
          this.getAreaTree(1, 1);
          this.objOption.ProviceId = this.saveObj.ProviceId;
          if (this.saveObj.ProviceId && this.saveObj.ProviceId > 0) {
            this.getAreaTree(2, this.saveObj.ProviceId);
          }
          this.objOption.CityId = this.saveObj.CityId;
          if (this.saveObj.CityId && this.saveObj.CityId > 0) {
            this.getAreaTree(3, this.saveObj.CityId);
          }
          this.objOption.AreaId = this.saveObj.AreaId;
          this.$forceUpdate();
        } else {
          this.optionTitle = "新增学员"
        }
      },
      getSchool() {
        getSchoolDropdown({}).then(res => {
          this.schoolList = res.Data;
          this.$forceUpdate();
        })
      },
      onRejected(rejectedEntries) {
        this.$q.notify({
          type: 'negative',
          position: "top",
          message: `文件验证失败，请重新上传`
        })
      },
      uploadFile(files) {
        UploadSelfFile('studentIcon', files[0], res => {
          if (res.resultCode == 1) {
            this.objOption.StuIcon = res.FileUrl;
          }
        })
      },
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      setStudent() {
        this.saveLoading = true
        saveStudent(this.objOption).then(res => {
          this.saveLoading = false
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
          this.$emit("success")
          this.closeSaveForm()
        }).catch(() => {
          this.saveLoading = false
        })
      },
    },
  }

</script>

<style>
  .upload-assiatant-box .q-uploader__list {
    display: none;
  }

  .upload-assiatant-box .q-uploader {
    width: auto;
  }

</style>
