<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>
        <ion-list class="canvasWrapper" >
          <ion-item-group>
            <ion-item>
              <div class="panel-title2">
                <div class="item-flag"></div>
                求职意向
              </div>
            </ion-item>
            <ion-item :class="[huntDataValid.dataModel.workName.$error?'ion-invalid':'ion-valid']">
              <ion-label>工种名称<span class="danger">*</span></ion-label>
              <ion-input name="workName" id="workName"  style="text-align: right;" placeholder="请输入工种名称" v-model="dataModel.workName" ></ion-input>
              <ion-note slot="error">工种名称不能为空</ion-note>
            </ion-item>
            <ion-item :class="[huntDataValid.dataModel.jobHuntType.$error?'ion-invalid':'ion-valid']">
              <ion-label>求职类型<span class="danger">*</span></ion-label>
              <ion-select name="jobHuntType"  id="jobHuntType" okText="确定" cancelText="取消" v-model="dataModel.jobHuntType"
                          interface="action-sheet" placeholder="请选择求职类型" >
                <ion-select-option v-for=" (it,key) in jobHuntTypeList" :key="key" :value="it.value">
                  {{ it.name }}
                </ion-select-option>
              </ion-select>
              <ion-note slot="error">求职类型不能为空</ion-note>
            </ion-item>
            <ion-item :class="[huntDataValid.dataModel.jobUserType.$error?'ion-invalid':'ion-valid']">
              <ion-label>人才类型<span class="danger">*</span></ion-label>
              <ion-select name="jobUserType"  id="jobUserType" okText="确定" cancelText="取消" v-model="dataModel.jobUserType"
                          interface="action-sheet" placeholder="请选择人才类型" >
                <ion-select-option v-for=" (it,key) in jobUserTypeList" :key="key" :value="it.value">
                  {{ it.name }}
                </ion-select-option>
              </ion-select>
              <ion-note slot="error">人才类型不能为空</ion-note>
            </ion-item>
            <ion-item :class="[huntDataValid.dataModel.maxSalary.$error||huntDataValid.dataModel.minSalary.$error?'ion-invalid':'ion-valid']">
              <ion-label>月薪要求<span class="danger">*</span></ion-label>
              <ion-input inputmode="numeric" name="minSalary" id="minSalary" style="text-align: center;" placeholder="输入最低薪资" v-model="dataModel.minSalary" ></ion-input>
              至
              <ion-input inputmode="numeric" name="maxSalary" id="maxSalary" style="text-align: center;" placeholder="输入最高薪资" v-model="dataModel.maxSalary" ></ion-input>
              <ion-note slot="error">月薪要求不能为空</ion-note>
            </ion-item>
            <ion-item :class="[huntDataValid.dataModel.workYear.$error?'ion-invalid':'ion-valid']">
              <ion-label>工作年限<span class="danger">*</span></ion-label>
              <ion-input inputmode="numeric" name="workYear" id="workYear" style="text-align: right;" placeholder="请输入工作年限" v-model="dataModel.workYear" ></ion-input>
<!--           <ion-range name="workYear" id="workYear" aria-label="Range with pin"  pin="true" pin-formatter="pinFormatter" v-model="dataModel.workYear"
                         min=0 max=60 style="display: inline-block; width: 320px;"></ion-range>-->
              <ion-note slot="error">工作年限不能为空</ion-note>
            </ion-item>
            <ion-item :class="[huntDataValid.dataModel.areaWork.$error?'ion-invalid':'ion-valid']">
              <ion-label>期望工作地区<span class="danger">*</span></ion-label>
              <ion-input name="areaWork" id="areaWork" style="text-align: right;" placeholder="请输入期望工作地区" v-model="dataModel.areaWork" ></ion-input>
              <ion-note slot="error">期望工作地区不能为空</ion-note>
            </ion-item>
          </ion-item-group>
        </ion-list>
    </ion-content>
    <ion-footer>
      <ion-toolbar>
        <div slot="end">
          <ion-button class="next-btn" @click="onSave">保  存</ion-button>
        </div>
      </ion-toolbar>
    </ion-footer>
  </ion-page>
</template>
<script lang="ts">
import {arrowBackOutline,addCircleOutline} 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 {getJobHuntByID,saveJobHunt} from "@/api/jobHuntInfo";
import {getSysDictionaryList} from '@/api/system/dictionary';
import {alertController, onIonViewDidEnter} from "@ionic/vue";

interface huntModel {
  dataModel: any;
}
interface SelectProps {
  name: string,
  value: string
}
interface StepParams{
  name: string,
  statusVal: number
}
export class ExampleComponent {
  pinFormatter(value: number) {
    return `${value}`;
  }
}

export default defineComponent({
  name: 'jobHuntEdit',
  setup() {
    const router = useRouter();
    const route = useRoute();
    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 jobHuntTypeList = ref<SelectProps[]>([]);
    const jobUserTypeList =ref<SelectProps[]>([]);

    const jobHuntData = reactive<huntModel>({dataModel:{
        jobUserID:null,
        workName:null,
        jobHuntType:null,
        jobUserType:null,
        minSalary:null,
        maxSalary:null,
        workYear:null,
        areaWork:null,
      }});
    const huntDataRule = computed(()=>{
      return {dataModel:{
          workName:{required},
          jobHuntType:{required},
          jobUserType:{required},
          minSalary:{required},
          maxSalary:{required},
          workYear:{required},
          areaWork:{required},
        }}});
    const huntDataValid = useVuelidate(huntDataRule,jobHuntData);
    const presentAlert = async (message: string) => {
      const alert = await alertController.create({
        header: '错误！',
        message: message,
        buttons: [
          '确定'
        ],
      });

      await alert.present();
    }
    const getJobHuntTypeList = async function(){
      const data :any = await getSysDictionaryList("JobHuntType");
      jobHuntTypeList.value = data;
    }
    const getJobUserTypeList = async function(){
      const data :any = await getSysDictionaryList("JobUserType");
      jobUserTypeList.value = data;
    }

    const onSave = async function (){
      const isFormCorrect = await huntDataValid.value.$validate();
      if(!isFormCorrect){
        console.log("当前求职意向数据",jobHuntData.dataModel);
        await presentAlert("请填写完整的信息！");
        return null;
      }
      saveJobHunt(jobHuntData.dataModel).then(result => {
        if (result) {
          router.push({path: './edit', query: {reload:1,jobUserID: jobHuntData.dataModel.jobUserID,status:3}});
        }
      });
    }

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


    const loadData = async (jobHuntID: any,jobUserID:any,status:any) => {
      await getJobHuntTypeList();
      await getJobUserTypeList();
      const reqData = await getJobHuntByID(jobHuntID);
      curStepData.value.statusVal = status;
      jobHuntData.dataModel = reqData;
      jobHuntData.dataModel.jobUserID = jobUserID;
      console.log("初始化求职意向 ",jobHuntData.dataModel);
    };

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

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

    return {
      ...toRefs(jobHuntData),
      arrowBackOutline,
      addCircleOutline,
      editForm,
      curStepData,
      stepList,
      jobUserTypeList,
      jobHuntTypeList,
      onSave,
      onCancel,
      loadData,
      route,
      router,
      huntDataValid
    }
  }
});
</script>
<style lang="less">
.next-btn {
  width: 80px;
  border-radius: 5px;
  background-color:#3a7be0;
  color: #363432;
  font-size: 14px;
}

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