<template>
  <ion-page>
    <ion-header>
      <ion-toolbar>
        <ion-buttons slot="start">
          <ion-icon :icon="arrowBackOutline" @click="onCancel" style="padding-left:10px;width:24px;height:24px;"></ion-icon>
        </ion-buttons>
        <ion-title>教育经历收集</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content>
      <div class="stepFlex">
        <div v-for="(record,key) in stepList" :key="key" class="stepFlex-item">
          <div
              :class="[(record.val < curStepData?.statusVal || curStepData?.statusVal==stepList.val) ? 'greenCircle' :record.val == curStepData?.statusVal ? 'now' : 'grayCircle']"></div>
          <div v-if="key !== stepList.length - 1"
               :class="[record.val < curStepData?.statusVal ? 'greenLine' : 'grayLine']"></div>
          <div class="stepFlex-item-label">
            <p class="stepFlex-item-label-title">{{ record.title }}</p>
<!--            <p class="stepFlex-item-label-desc">{{ record.desc }}</p>-->
          </div>
        </div>
      </div>
      <form ref="editForm" @submit.prevent="onSave">
        <ion-list class="canvasWrapper" >
          <ion-item-group>
            <ion-item>
              <div class="panel-title2">
                <div class="item-flag"></div>
                教育经历
              </div>
            </ion-item>
            <ion-label class="title-item">毕业院校/培训机构<span class="danger">*</span></ion-label>
            <ion-item :class="[educationValid.dataModel.schoolName.$error?'ion-invalid':'ion-valid']">
              <ion-input name="schoolName" id="schoolName" style="text-align: left;" class="custom"
                         placeholder="请输入毕业院校/培训机构" v-model="dataModel.schoolName" ></ion-input>
              <ion-note slot="error">毕业院校/培训机构不能为空</ion-note>
            </ion-item>
            <ion-label class="title-item">最高学历<span class="danger">*</span></ion-label>
            <ion-item :class="[educationValid.dataModel.cultureRank.$error?'ion-invalid':'ion-valid']">
              <ion-select name="cultureRank"  id="cultureRank" okText="确定" cancelText="取消" v-model="dataModel.cultureRank"
                          interface="action-sheet" placeholder="请选择最高学历" style="width:100%;text-align:left;">
                <ion-select-option v-for=" (it,key) in cultureRankList" :key="key" :value="it.value">
                  {{ it.name }}
                </ion-select-option>
              </ion-select>
              <ion-note slot="error">最高学历不能为空</ion-note>
            </ion-item>
            <ion-label class="title-item">专业<span class="danger">*</span></ion-label>
            <ion-item :class="[educationValid.dataModel.major.$error?'ion-invalid':'ion-valid']">
              <ion-input name="major" id="major" style="text-align: left;" class="custom"
                         placeholder="请输入专业" v-model="dataModel.major" ></ion-input>
              <ion-note slot="error">专业不能为空</ion-note>
            </ion-item>
            <ion-label class="title-item">起止日期<span class="danger">*</span></ion-label>
            <ion-item mode="md">
              <ion-datetime-button datetime="schoolTime"></ion-datetime-button>
              <ion-modal :keep-contents-mounted="true">
                <ion-datetime id="schoolTime" placeholder="日期"
                              v-model="dataModel.schoolTime" :prefer-wheel="true"
                              dataformatas="YYYY-MM-DD" presentation="date" cancel-text="取消" done-text="确定"
                              :show-default-buttons="true">
                </ion-datetime>
              </ion-modal>
              至
              <ion-datetime-button datetime="overTime"></ion-datetime-button>
              <ion-modal :keep-contents-mounted="true">
                <ion-datetime id="overTime" placeholder="日期"
                              v-model="dataModel.overTime" :prefer-wheel="true"
                              dataformatas="YYYY-MM-DD" presentation="date" cancel-text="取消" done-text="确定"
                              :show-default-buttons="true">
                </ion-datetime>
              </ion-modal>
              <!--<ion-note slot="error">起止日期不能为空</ion-note>-->
            </ion-item>
          </ion-item-group>
        </ion-list>
      </form>
    </ion-content>
    <ion-footer>
      <ion-toolbar>
        <div slot="end">
          <ion-button shape="round" expand="block"  @click="onSave" >保  存</ion-button>
        </div>
      </ion-toolbar>
    </ion-footer>
    <ion-loading
        :is-open="loading"
        message="加载中..."
        @didDismiss="setOpen(false)" >
    </ion-loading>
  </ion-page>
</template>
<script lang="ts">
import {arrowBackOutline} from 'ionicons/icons';
import {reactive,defineComponent, computed,ref,toRefs} from "vue";
import {useRoute,useRouter} from "vue-router";
import {required} from "@vuelidate/validators";
import {useVuelidate} from "@vuelidate/core";
import {getEducationByID,saveEducation} from "@/api/education";
import {getSysDictionaryList} from '@/api/system/dictionary';
import {alertController, onIonViewDidEnter} from "@ionic/vue";

interface educationModel {
  dataModel:any
}
interface SelectProps {
  name: string,
  value: string
}
interface StepParams{
  name: string,
  statusVal: number
}
export default defineComponent({
  name: 'educationEdit',
  setup() {
    const router = useRouter();
    const route = useRoute();
    const loading = ref(false);
    const editForm = ref();
    const curStepData = ref<StepParams>({
      name:"",
      statusVal: 2
    });
    const stepList = ref([
      {title: '个人基础信息', desc: '个人基础信息', val: 1},
      {title: '完善教育经历', desc: '完善个人教育经历',val: 2},
      {title: '个人求职意向', desc: '个人求职意向', val: 3}]);
    const cultureRankList = ref<SelectProps[]>([]);
    const educationData = reactive<educationModel>({
      dataModel:{
        jobuserID:null,
        schoolName:'',
        cultureRank:0,
        major:'',
        schoolTime:null,
        overTime:null,
    }});
    const educationRules = computed(()=>{
      return {dataModel:{
          schoolName:{required},
          cultureRank:{required},
          major:{required},
          schoolTime:{required},
          overTime:{required},
        }}});
    const educationValid = useVuelidate(educationRules,educationData);
    const setOpen = (isOpen: boolean) => {
      loading.value = isOpen;
    };
    const presentAlert = async (message: string) => {
      const alert = await alertController.create({
        header: '错误！',
        message: message,
        buttons: [
          '确定'
        ],
      });

      await alert.present();
    }

    const onSave = async function (){
      const isFormCorrect = await educationValid.value.$validate();
      if(!isFormCorrect){
        console.log("当前教育经历数据",educationData.dataModel);
        await presentAlert("请填写完整的信息！");
        return null;
      }
      saveEducation(educationData.dataModel).then(result => {
        if (result) {
          router.push({path: './edit', query: {reload:1,jobUserID: educationData.dataModel.jobuserID,status:2}});
        }
      });
    }

    const onCancel = () => {
      router.push({path: './edit', query: {reload:1,jobUserID: educationData.dataModel.jobuserID,status:2}});
    }

    const getCultureRankList = async function(){
      const data :any = await getSysDictionaryList("CultureLevel");
      cultureRankList.value = data;
    }

    const loadData = async (educationID: any,jobUserID:any,status:any) => {
      loading.value = true;
      await getCultureRankList();
      const reqData = await getEducationByID(educationID);
      curStepData.value.statusVal = status;
      educationData.dataModel = reqData;
      educationData.dataModel.jobuserID = jobUserID;
      console.log("初始化教育经历",educationData.dataModel);
      loading.value = false;
    };

    const reload = (educationID: any,jobUserID:any,status:any) => {
      educationData.dataModel = {};
      loadData(educationID,jobUserID,status);
    }

    onIonViewDidEnter(() => {
      if (route.query.reload)
        reload(route.query.educationID,route.query.jobUserID,route.query.status);
    });

    return {
      ...toRefs(educationData),
      arrowBackOutline,
      editForm,
      curStepData,
      stepList,
      cultureRankList,
      setOpen,
      onSave,
      onCancel,
      loadData,
      route,
      router,
      loading,
      educationValid
    }
  }
});
</script>
<style lang="less">

ion-input.custom{
  --placeholder-color: gray;
  --placeholder-font-style:oblique;
  --placeholder-opacity: 1;
}

.title-item{
  margin-left: 15px;
  color:#3a7be0;
  font-size: 16px;
}

.stepFlex {
  margin: 0;
  display: flex;
  width: 100%;

  .stepFlex-item {
    position: relative;
    flex: 1;
    text-align: center;
    margin-top: -10px;

    .stepFlex-item-label {
      padding-top: 60px;
      font-size: 14px;

      .stepFlex-item-label-title{
        margin-top:30px;
      }

      .stepFlex-item-label-desc{
        margin-top:5px;color: #b9b9bd;
      }
    }
  }

  .greenCircle {
    top: calc(50% - 15px);
    left: calc(50% - 4px);
    position: absolute;
    z-index: 2;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: #31A2FE;
  }

  .now {
    top: calc(50% - 18px);
    left: calc(50% - 8px);
    position: absolute;
    z-index: 3;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background-color: #31A2FE;
    border: 4px solid #c5e8f9;
  }

  .grayCircle {
    top: calc(50% - 15px);
    left: calc(50% - 4px);
    position: absolute;
    z-index: 2;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background-color: #ccc;
  }

  .greenLine {
    width: 100%;
    top: calc(50% - 11px);
    left: calc(50% - 2px);
    height: 2px;
    background-color: #31A2FE;
    position: absolute;
  }

  .grayLine {
    height: 0;
    border: 1px dashed #ccc;
    width: 100%;
    top: calc(50% - 11px);
    left: calc(50% - 2px);
    position: absolute;
  }
}

</style>