<template>
  <ion-page>
    <ion-header class="header-theme2">
      <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">

        <div class="bw-vue-form">
          <ion-list class="canvasWrapper" >
            <div class="form-title">工作经验</div>

            <div class="form-input">
              <ion-label>工作单位<span class="danger">*</span></ion-label>
              <ion-input name="workAddress" id="workAddress" style="text-align: left;" class="custom"
                         placeholder="请输入工作单位" v-model="dataModel.workAddress" ></ion-input>
<!--              <ion-note slot="error">工作单位不能为空</ion-note>-->
            </div>

            <div class="form-input">
              <ion-label>职务<span class="danger">*</span></ion-label>
              <ion-input name="duties" id="duties" style="text-align: left;" class="custom"
                         placeholder="请输入职务" v-model="dataModel.duties" ></ion-input>
<!--              <ion-note slot="error">职务不能为空</ion-note>-->
            </div>

            <div class="form-input">
            <ion-label>工作内容<span class="danger">*</span></ion-label>
              <ion-textarea name="workOffice" id="workOffice"  class="custom" rows="4"
                            placeholder="请输入工作内容" v-model="dataModel.workOffice" style="border-bottom: 1px solid #fff2e8;"></ion-textarea>
<!--              <ion-note slot="error">工作内容不能为空</ion-note>-->
            </div>

            <div class="form-input">
              <ion-label>起止日期<span class="danger">*</span></ion-label>
              <div>
                <ion-datetime-button datetime="startTime" style="float:left;"></ion-datetime-button>
                <span style="float:left;padding-top: 5px;">至</span>
                <ion-datetime-button datetime="endTime" style="float:left;"></ion-datetime-button>
                <ion-modal :keep-contents-mounted="true">
                  <ion-datetime id="startTime" placeholder="日期"
                                v-model="dataModel.startTime" :prefer-wheel="true"
                                dataformatas="YYYY-MM-DD" presentation="date" cancel-text="取消" done-text="确定"
                                :show-default-buttons="true">
                  </ion-datetime>
                </ion-modal>
                <ion-modal :keep-contents-mounted="true">
                  <ion-datetime id="endTime" placeholder="日期"
                                v-model="dataModel.endTime" :prefer-wheel="true"
                                dataformatas="YYYY-MM-DD" presentation="date" cancel-text="取消" done-text="确定"
                                :show-default-buttons="true">
                  </ion-datetime>
                </ion-modal>
              </div>
            </div>
          </ion-list>
        </div>
      </form>
    </ion-content>
    <ion-footer>
      <ion-toolbar>
          <ion-button style="width:100%;" @click="onSave" >保  存</ion-button>
      </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 {getExperienceByID,saveExperience} from "@/api/experience";
import {alertController, onIonViewDidEnter} from "@ionic/vue";

interface experienceModel {
  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},
      {title: '求职意向', desc: '个人求职意向', val: 4},
    ]);
    const cultureRankList = ref<SelectProps[]>([]);
    const experienceData = reactive<experienceModel>({
      dataModel:{
        jobuserID:null,
        workAddress:'',
        duties:'',
        workOffice:'',
        startTime:null,
        endTime:null,
    }});
    const experienceRules = computed(()=>{
      return {dataModel:{
          workAddress:{required},
          duties:{required},
          workOffice:{required},
          startTime:{required},
          endTime:{required},
        }}});
    const experienceValid = useVuelidate(experienceRules,experienceData);
    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 experienceValid.value.$validate();
      if(!isFormCorrect){
        console.log("当前工作经验数据",experienceData.dataModel);
        await presentAlert("请填写完整的信息！");
        return null;
      }
      saveExperience(experienceData.dataModel).then(result => {
        if (result) {
          router.push({path: './edit', query: {reload:1,jobUserID: experienceData.dataModel.jobuserID,status:3}});
        }
      });
    }

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


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

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

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

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

ion-input.custom{
  --placeholder-color: gray;
  --placeholder-opacity: 0.5;
}

.title-item{
  margin-left: 15px;
  color:#1c3d70 !important;
  font-size: 14px !important;
  font-weight: bold;
}

.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>