<template>
  <div>
    <NavHeader color="#0687D2" title="人才码" @back="$router.go(-1)" />
    <van-steps :active="active">
      <van-step>缴纳社保</van-step>
      <van-step>人才类型</van-step>
    </van-steps>
    <div class="step" v-if="active === 0">
      <div class="box" @click="next('0')">在镇缴纳社保</div>
      <div class="boxtwo" @click="next('1')">未在镇缴纳社保</div>
    </div>
    <div class="step" v-if="active === 1">
      <div
        class="cell"
        v-for="item in list"
        :key="item.name"
        @click="filterData(item)"
      >
        <img class="type" :src="item.icon" alt="" />
        <p class="des">{{ item.name }}</p>
        <img class="icon" src="@/assets/common/enter_icon.png" alt="" />
      </div>
      <van-button type="primary" @click="prev">上一步</van-button>
    </div>

    <van-dialog
      v-model="show"
      title="填写信息"
      show-cancel-button
      @confirm="onConfirm"
      @cancel="onCancel"
    >
      <van-field v-model="name" label="姓名" required />
      <van-field v-model="passport" label="护照" required />
    </van-dialog>
  </div>
</template>
<script>
import NavHeader from "@/components/NavHeader";
import Vue from "vue";
import { Step, Steps, Button, Dialog, Field } from "vant";
import { social, doServerMedical } from "@/api/insurance";
import { getPersonnel } from "@/api/talents";
// import { encryptByDES } from "@/utils/utils";
import { encryptByDES, decryptByDES } from "@/utils/utils";
Vue.use(Step).use(Steps).use(Button).use(Dialog).use(Field);
export default {
  name: "reportFilter",
  components: { NavHeader },
  data() {
    return {
      show: false,
      active: 0,
      status: "",
      typeList: [
        {
          status: "0",
          name: "企业全职人员",
          icon: require("@/assets/talents/type1.png"),
          index: 1
        },
        {
          status: "0",
          name: "事业单位专技人员（不包含管理岗）",
          icon: require("@/assets/talents/type1.png"),
          index: 2
        },
        {
          status: "1",
          name: "海外设立研发中心等机构的全职引进人才",
          icon: require("@/assets/talents/type3.png"),
          index: 3
        },
        {
          status: "1",
          check: true,
          name: "入选的市级以上重点人才工程或行业人才计划",
          icon: require("@/assets/talents/type2.png"),
          index: 4
        }
      ],
      list: [],
      name: "",
      passport: "",
      genera: "",
      res: [],
    };
  },
  methods: {
    // 点击在镇缴纳或未在镇缴纳  0为在  1为不在
    next(status) {
      this.status = status;
      this.list = this.typeList.filter(item => {
        return item.status === status;
      });
      this.active = 1;
    },
    prev() {
      this.active = 0;
      this.list = [];
    },
    filterData(item) {
      this.genera = item.name;
      let area = this.$store.state.talents.area;
        if (item.status == "0") {
          // 需要检查社保缴纳情况
          if (area === 0) {
            // alert(JSON.stringify(item))
            if (item.name != "企业全职人员") {
              // 取消校验
              this.$router.push({ name: "ReportForm",query: {genera:this.genera, ticket: this.$route.query.ticket ? this.$route.query.ticket : ''} }); 
            } else {
              // 暂时关闭校验，直接退出
              // this.$dialog.confirm({
              //   message:
              //     "由于省人社厅近期系统升级维护，暂时关闭社保查询通道，相关业务降于7月22日之后陆续开放",
              //   showCancelButton: false
              // })
              // .then(() => {
              //   this.$router.go(-1);
              // });
              let res = sessionStorage.getItem("ZJCD-UserInfo");
              // alert(res);
              let idCard = JSON.parse(res).idCardNo;
              console.log("------------", idCard);
              let a = idCard.substring(0, 4);
              console.log("a--------", a);
              if (Number(a) + "" !== NaN + "") {
                let idCardNo1 = idCard;
                let idCardNo2 = idCardNo1;
                console.log("如果为明文走这个---", idCardNo2);
                this.socialCheck(idCardNo2, item.index);
                console.log(item.index,'item.index-----')

              } else {
                console.log("如果为加密走这个---", idCardNo2);
                let idCardNo = decryptByDES(idCard, "MnuYbhGv");
                let idCardNo1 = idCardNo;
                let idCardNo2 = encryptByDES(idCardNo1, "MnuYbhGv");
                this.socialCheck(idCardNo2, item.index);
              }  
            }
          } else {
            // 外籍或港澳台
            this.show = true;
          }
        } else {
          if (item.check) {
            // 需要检测是否在名单中
            if (area === 0) {
              // 国内人员
              let res = sessionStorage.getItem("ZJCD-UserInfo");
              let name = JSON.parse(res).realName
                ? JSON.parse(res).realName
                : JSON.parse(res).name;
              // let idCard = decryptByDES(JSON.parse(res).idCardNo, "MnuYbhGv");
              this.nameCheck(name);
            } else {
              // 外籍或港澳台
              this.show = true;
            }
          } else {
            this.$store.commit("talents/SET_GENERA", this.genera);
            this.$router.push({ name: "ReportForm",query: {genera:this.genera, ticket: this.$route.query.ticket ? this.$route.query.ticket : ''} });
          }
        } 
    },
    nameCheck(name) {
      // 更改前代码
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: "加载中..."
      });
      getPersonnel({ appId: 1234, name: name }).then(res => {
        this.$toast.clear();
        if (res && res.length > 0) {
          this.$store.commit("talents/SET_NAME", this.name);
          this.$store.commit("talents/SET_PASSPORT", this.passport);
          this.$store.commit("talents/SET_GENERA", this.genera);
          this.$router.push({ name: "ReportForm",query: {genera:this.genera, ticket: this.$route.query.ticket ? this.$route.query.ticket : ''} });
        } else {
          this.$dialog
            .confirm({
              message:
                "您未在入选的市级以上重点人才工程或行业人才计划名单中，暂不具备申报条件",
              showCancelButton: false
            })
            .then(() => {
              this.$router.go(-1);
            });
        }
      });
    },
    socialCheck(aae135, index) {
      let params = {
        busCode: "WS-B0004",
        aae135: aae135,
        cpage: 1,
        rows: 1000
      };
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: "加载中..."
      });
      social(params).then((res) => {
        this.$toast.clear();
        // 无社保企业全职人员、退出
        if (index == 1 && res.data.data.busiId != "0000") {
          this.dialogWarning(1);
        } else if (
          res.data.data.dataSet &&
          res.data.data.dataSet.rowSet &&
          res.data.data.dataSet.rowSet.row &&
          res.data.data.dataSet.rowSet.row.length > 0
        ) {
          let targetList = res.data.data.dataSet.rowSet.row;
          let company = this.checkSecure(targetList, "企业职工基本养老保险","企业基本养老保险");
          if (company) {
            sessionStorage.setItem("ZJCD-Company", company);
            this.$router.push({ name: "ReportForm",query: {genera:this.genera, ticket: this.$route.query.ticket ? this.$route.query.ticket : ''} });
            return;
          } else if (index == 1) {
            sessionStorage.removeItem("ZJCD-Company");
            // 社保异常、企业全职人员
            this.dialogWarning(2);
            return;
          } else {
            // 当没有社保或缴费异常的时候、对于专技人员的判断
            this.checkMedical(params);
          }
        } else if (index == 2) {
          // 社保异常，进入专职人员
          this.checkMedical(params);
        } else {
          // 社保异常、企业全职人员
          this.dialogWarning(2);
        }
        this.$store.commit("talents/SET_NAME", this.name);
        this.$store.commit("talents/SET_PASSPORT", this.passport);
        this.$store.commit("talents/SET_GENERA", this.genera);
      });
    },
    // 社保异常提示
    dialogWarning(type) {
      this.$dialog
        .confirm({
          message:
            type == 1
              ? "未查询到您的参保信息"
              : "您未在镇江正常缴纳社保，暂不具备申报条件",
          showCancelButton: false,
        })
        .then(() => {
          this.$router.go(-1);
        });
      return;
    },
    // 校验医保
    checkMedical(params) {
      doServerMedical(params).then((res) => {
        if (res.data.data.busiId !== "0000") {
          this.dialogWarning(1);
        } else if (
          res.data.data.dataSet &&
          res.data.data.dataSet.rowSet &&
          res.data.data.dataSet.rowSet.row &&
          res.data.data.dataSet.rowSet.row.length > 0
        ) {
          let targetList = res.data.data.dataSet.rowSet.row;
          let company = this.checkSecure(targetList, "职工基本医疗保险");
          // 校验医保是否缴费正常
          if (company) {
            sessionStorage.setItem("ZJCD-Company", company);
            this.$router.push({ name: "ReportForm",query: {genera:this.genera, ticket: this.$route.query.ticket ? this.$route.query.ticket : ''} });
            return;
          } else {
            sessionStorage.removeItem("ZJCD-Company");
            this.dialogWarning(2);
          }
        } else {
          this.dialogWarning(2);
        }
      });
    },
    // 校验保险种类、返回单位
    checkSecure(data, name, name1) {
      let company = "";
      data.map((res) => {
        let a = res.bab002.substring(0,4)
        if ((res.aae140 == name || res.aae140 == name1) && res.aac031code == "1" && this.genera == '企业全职人员' && a == '3211') {
          company = res.aae044;
        } else if ( res.aae140 == name && res.aac031code == "1" && this.genera != '企业全职人员' ) {
          company = res.aae044;
        }
      });
      return company;
    },
    onConfirm() {
      const { name, passport } = this;
      if (name && passport) {
        if (this.status === "0") {
          // 这是用来查社保的
          let passport = encryptByDES(this.passport, "MnuYbhGv"); // 给护照加密
          this.socialCheck(passport);
        } else {
          console.log("onConfirm----", 2222);
          // 这个名字用来查名单
          this.nameCheck(this.name);
        }
      } else {
        this.$toast("请将信息填写完整");
      }
    },
    onCancel() {
      this.name = "";
      this.passport = "";
      this.show = false;
    }
  }
};
</script>

<style lang="stylus" scoped>
.step
  margin-top: 20px
  padding: 20px
  background: #fff
  .box
    width: 100%
    height: 100px
    background: linear-gradient(270deg, #4CD9A5 0%, #50EF9F 100%)
    box-shadow: 0px 2px 4px 0px #BBBBBB
    border-radius: 4px
    line-height: 100px
    text-align: center
    font-size: 50px
    color: #FFFFFF
    font-weight: 500
  .boxtwo
    width: 100%
    height: 100px
    margin-top: 20px
    background: linear-gradient(270deg, #FF8C4B 0%, #FFB64E 100%)
    box-shadow: 0px 2px 4px 0px #BBBBBB
    border-radius: 4px
    line-height: 100px
    text-align: center
    font-size: 50px
    color: #FFFFFF
    font-weight: 500
  .cell
    height: 160px
    padding: 0 20px 0 40px
    margin-bottom: 30px
    display: flex
    align-items: center
    background: #FFFFFF
    .type
      width: 90px
      height: 90px
    .des
      width: 420px
      margin-left: 40px
      line-height: 32px
      font-weight: 500
      font-size: 28px
      color: #333333
    .icon
      width: 30px
      height: 30px
      margin-left: 50px
</style>
