<template>
  <ion-page class="list-page company-list-page">
    <ion-header class="header-theme2">
      <ion-toolbar>
        <ion-buttons slot="start">
          <ion-icon :icon="arrowBackOutline" @click="onBack"></ion-icon>
        </ion-buttons>
        <ion-title>求职人员推荐</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content>
      <ion-item class="search-item">
        <ion-label>
          <h2>{{ searchParams.professionName }}</h2>
          <p>{{ searchParams.companyName }}</p>
        </ion-label>
      </ion-item>
      <ion-list class="list-content">
        <ion-item v-for="(record,key) in dataList" detail :key="key">
          <ion-label>
            <h2>{{ record.userName }}</h2>
            <p>应聘岗位：{{ record.professionName }}</p>
            <p>电话：{{ record.userMobile }}</p>
          </ion-label>
          <ion-avatar aria-hidden="true" class="container" slot="end">
            <ion-text style="text-align:right;" @click="onRecommend(record)">推荐</ion-text>
          </ion-avatar>
        </ion-item>
      </ion-list>
      <b-empty v-if="dataList.length<=0" :loading="loading"/>
      <ion-infinite-scroll threshold="100px" @ionInfinite="onScroll($event)">
        <ion-infinite-scroll-content
            :loadingText="pagination.total>pagination.pageIndex*pagination.pageSize?'正在加载...':'暂无更多'"
            loadingSpinner="bubbles">
        </ion-infinite-scroll-content>
      </ion-infinite-scroll>
    </ion-content>
    <ion-footer>
      <ion-toolbar>
        <ion-button expand="block" @click="onBatchRecommend()" fill="outline">全部推荐</ion-button>
      </ion-toolbar>
    </ion-footer>
  </ion-page>
</template>

<script lang="ts">
import {useRoute, useRouter} from "vue-router";
import {computed, defineComponent, reactive, ref, watch} from 'vue';
import {arrowBackOutline, addCircleOutline} from 'ionicons/icons';
import {alertController, IonIcon, onIonViewDidEnter} from '@ionic/vue';
import {getRecommendJobList, addRecommend} from "@/api/recommendmgt";

export default defineComponent({
  name: 'RecommendJobList',
  components: {IonIcon},
  setup() {
    const loading = ref(true);
    const router = useRouter();
    const route = useRoute();
    const total = ref(10);
    const pagination = computed(() => ({
      total: total,
      pageIndex: searchParams.pageIndex,
      pageSize: searchParams.pageSize
    }));
    const searchParams = reactive({
      pageIndex: 1,
      pageSize: 5,
      postID: '',
      professionID: '',
      companyName: '',
      professionName: '',
      type: 0
    })
    const dataList = ref<any>([]);
    const addRecommendList = ref<any>([]);
    const colors = ref(["secondary", "tertiary", "success", "warning"]);

    const loadData = async function () {
      loading.value = true;
      searchParams.postID = route.query.postID as any;
      searchParams.companyName = route.query.companyName as any;
      searchParams.professionID = route.query.professionID as any;
      searchParams.professionName = route.query.professionName as any;
      searchParams.type = route.query.type as any;
      getRecommendJobList(searchParams).then(data => {
        dataList.value = dataList.value.concat(data.list);
        total.value = data.total;
      });
      loading.value = false;
    }

    const onScroll = (e: any) => {
      setTimeout(() => {
        e.target.complete();
        if (pagination.value.total.value > pagination.value.pageIndex * pagination.value.pageSize) {
          searchParams.pageIndex += 1;
          loadData();
        }
      }, 500);
    }

    const onRecommend = (item: any) => {
      addRecommendList.value.push({
        recommendMgtID: item.recommendMgtID,
        jobHuntID: item.jobhuntID,
        postID: searchParams.postID,
        recommendType: 0
      });
      addRecommend(addRecommendList.value).then(() => {
        const index = dataList.value.findIndex((x: any) => x.recommendMgtID == item.recommendMgtID);
        dataList.value.splice(index, 1)
        addRecommendList.value = [];
      });
    };

    const onBatchRecommend = () => {
      if (dataList.value.length == 0) {
        presentAlert("没有需要推荐的求职人员！")
        return;
      }

      dataList.value.forEach((item: any) => {
        addRecommendList.value.push({
          recommendMgtID: item.recommendMgtID,
          jobHuntID: item.jobhuntID,
          postID: searchParams.postID,
          recommendType: 0
        });
      })
      addRecommend(addRecommendList.value).then(() => {
        loadData();
        dataList.value = [];
        addRecommendList.value = [];
      });
    };

    const presentAlert = async (message: string) => {
      const alert = await alertController.create({
        header: '错误！',
        message: message,
        buttons: [
          '确定'
        ],
      });

      await alert.present();
    }

    const onBack = () => {
      router.go(-1);
    }

    const reload = () => {
      dataList.value = [];
      searchParams.pageIndex = 1;
      loadData();
    }
    onIonViewDidEnter(() => {
      reload();
    });

    return {
      arrowBackOutline,
      addCircleOutline,
      router,
      route,
      colors,
      loading,
      pagination,
      searchParams,
      dataList,
      onScroll,
      loadData,
      reload,
      onBack,
      onRecommend,
      onBatchRecommend,
    }
  }
});
</script>

<style lang="less">
.custom {
  --placeholder-color: gray;
  --placeholder-font-style: italic;
  --placeholder-opacity: 1;
}

.company-list-page {
  .list-content {
    margin: 0px 15px !important;
    background-color: white !important;
    border-radius: 0 !important;

    ion-item {
      margin-top: 10px;
      font-size: 14px;
      border: 1px solid rgb(242, 242, 245);

      p {
        font-size: 12px;
      }
    }
  }
}

.container {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
}

.footer-ios ion-toolbar:first-of-type{
  --border-width:0 !important;
  --background:#ffffff !important;
}

</style>

