<template>
  <div class="home">
    <img src="../../../assets/image/homeBg.png" alt="" class="homeBg" />
    <div class="myBody">
      <div class="itemBody">
        <div
          v-for="(item, index) in itemList"
          :key="index"
          class="itemBox"
          @click="toDetail(item)"
        >
          <div
            class="jiaobiao"
            v-if="item.name == '通知公告' && messageNum > 0"
          >
            {{ messageNum }}
          </div>
          <div
            class="jiaobiao"
            v-if="item.name == '园区活动' && parkActivity > 0"
          >
            {{ parkActivity }}
          </div>
          <div class="jiaobiao" v-if="item.name == '企业填报' && countNum > 0">
            {{ countNum }}
          </div>

          <img :src="item.img" class="homeIcon" />
          <p>{{ item.name }}</p>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import auth from "@/service/auth";
import {
  isFirstLogin,
  getUnreadCount,
  initPasswordCheck,
  getCount,
  getRegistrationCount,
} from "@/service/api_company.js";
import { initCompanyOpenId } from "@/service/api_fillController";
import { Toast, Dialog } from "vant";

export default {
  data() {
    return {
      countNum: 0,
      messageNum: 0,
      parkActivity: 0,
      itemList: [
        {
          name: "企业信息",
          img: require("../../../assets/image/qiye.png"),
          path: "/enterpriseInformation",
        },
        // {
        //     name: '人员名单',
        //     img: require('../../../assets/image/renyuan.png'),
        //     path: '/personList'
        // },
        // {
        //     name: '访客审核',
        //     img: require('../../../assets/image/fangke.png'),
        //     path: '/visitorExamine'
        // },
        // {
        //     name: '疑似确认',
        //     img: require('../../../assets/image/quezhen.png'),
        //     path: '/suspected'
        // },
        {
          name: "企业填报",
          img: require("../../../assets/image/qytb.png"),
          path: "/enterpriseReporting",
        },
        {
          name: "我的待办",
          img: require("../../../assets/image/qytb.png"),
          path: "/todo",
        },
        // {
        //     name: '我的入驻',
        //     img: require('../../../assets/image/rzsq.png'),
        //     path: '/settle_in'
        // },
        // {
        //     name: '退租申请',
        //     img: require('../../../assets/image/tzsq.png'),
        //     path: '/settle_out'
        // },
        // {
        //     name: '我的账单',
        //     img: require('../../../assets/image/wdzd.png'),
        //     path: '/my-bill'
        // },
        // {
        //   name: "会议室预约",
        //   img: require("../../../assets/image/hys.png"),
        //   path: "/meetingFerenceRoom",
        // },
        {
          name: "场馆预约",
          img: require("../../../assets/image/hys.png"),
          path: "/venue",
        },
        {
          name: "安全资料",
          img: require("../../../assets/image/aqzl.png"),
          path: "/saveInfoList",
        },
        {
          name: "政企服务",
          img: require("../../../assets/image/bsbx.png"),
          path: "/reportRepair",
        },
        {
          name: "园区活动",
          img: require("../../../assets/image/yqhd.png"),
          path: "/parkActivities",
        },
        {
          name: "通知公告",
          img: require("../../../assets/image/yqgg.png"),
          path: "/announcement",
        },
        {
          name: "案卷处理",
          img: require("../../../assets/image/yqgg.png"),
          path: "/modifyHandle",
        },
        {
          name: "安全隐患反馈",
          img: require("../../../assets/image/yqgg.png"),
          path: "/saveCallback",
        },
      ],
    };
  },
  created() {
    // isFirstLogin({ comId: auth.currUser().id }).then((res) => {
    //   console.log("get", res);
    //   const local = localStorage.getItem("company" + auth.currUser().id);
    //   if (local) {
    //     Toast("已获取暂存企业信息，请尽快前往企业信息完善内容并提交");
    //   } else {
    //     if (res.errno == 0) {
    //       Toast("首次登录请填写企业信息");
    //       this.$router.push("/enterpriseInformation");
    //     }
    //   }
    // });
  },
  mounted() {
    this.initUrlParam();
    this.getUnreadCount();
    // alert(window.location.href);
    // this.getCode()
    this.getRegistrationCount();
    this.initPasswordCheck();
    this.getCount();
  },
  methods: {
    initPasswordCheck() {
      let that = this;
      initPasswordCheck({ companyId: auth.currUser().id }).then((e) => {
        console.log("initPasswordCheck", e);
        if (e.errno == 500) {
          Dialog.alert({
            type: "info",
            confirmButtonColor: "black",
            title: "提示",
            message: "当前密码为初始密码，请修改后再继续使用。",
            confirmButtonText: "去修改",
          }).then(() => {
            // on close
            that.$router.push("/register");
          });
        }
      });
    },
    getUnreadCount() {
      getUnreadCount({ companyId: auth.currUser().id }).then((e) => {
        console.log(e);
        this.messageNum = e;
      });
    },
    getCount() {
      getCount({ enterId: auth.currUser().id, saveTypes: "1,2" }).then((e) => {
        console.log(e);
        this.countNum = e;
      });
    },
    getRegistrationCount() {
      getRegistrationCount({ companyId: auth.currUser().id }).then((e) => {
        console.log(e);
        this.parkActivity = e;
      });
    },
    initUrlParam() {
      const url = window.location.href;
      const parm = url.split("?");
      const hrefData = {};
      if (parm.length > 1) {
        parm[1].split("&").forEach((item) => {
          const itemRow = item.split("=");
          hrefData[itemRow[0]] = itemRow[1];
        });
      }
      if (hrefData.loginCode) {
        this.getOpenId(hrefData.loginCode);
      }
    },

    toDetail(item) {
      this.$router.push(item.path);
    },
    // getCode() {
    //     // const appid = 'wx6271d6fc90b56ff6' // 个人开发者appid
    //     const appid = 'wxb68cd3e98a502451' // 个人开发者appid
    //     const redirect = encodeURIComponent(window.location.href) // 重定向回来的地址
    //     const wx_code = this.getUrlParam('code') // 截取url中的code
    //     const url = encodeURIComponent('https://pgy.idea-sf.com/mnpH5')
    //     // const urlStr = window.location.href
    //     // urlStr = urlStr.split("#")[0]
    //     // const src = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6271d6fc90b56ff6&redirect_uri=https://pgy.idea-sf.com&response_type=code&scope=snsapi_userinfo&state=type%3Dquan%2Curl%3Dhttp%3A%2F%2Fmm.dianping.com%2Fweixin%2Faccount%2Fhome'
    //     const src = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${url}&response_type=code&scope=snsapi_base&state=123#wechat_redirect`
    //     // 判断有没有code
    //     if (!wx_code) {
    //         // 获取code的地址。获取成功重定向后地址栏中将会带有code，判断没有code的话，就跳转到微信官方链接上获取，获取成功后会再重定向回来，注意url是需要使用encodeURIComponent处理一下编码的
    //         window.location.href = src
    //     } else {
    //         // 获取到了code
    //         this.getOpenId(wx_code) // 把code传给后台获取用户信息
    //     }
    // },
    // getUrlParam方法就是使用正则截取地址栏里的code
    getUrlParam: function (name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    getOpenId(code) {
      // 这里调用后端给的接口吧code传过去就好了
      initCompanyOpenId({ id: auth.currUser().id, openId: code }).then(
        (res) => {}
      );
    },
  },
};
</script>
<style lang="scss">
.jiaobiao {
  width: 30px;
  height: 30px;
  position: absolute;
  top: 16px;
  right: 10px;
  background: red;
  line-height: 30px;
  text-align: center;
  color: white;
  border-radius: 50%;
  font-size: 30px;
}
.home {
  background: white;
  height: 95vh;

  .homeBg {
    width: 100vw;
  }

  .myBody {
    min-height: 65vh;
    position: absolute;
    top: 55vw;
    border-radius: 40px 40px 0 0;
    background: white;
    padding-bottom: 20vw;

    .itemBody {
      margin-top: 4vw;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr 1fr;

      .itemBox {
        text-align: center;
        color: rgba(51, 51, 51, 1);
        font-size: 3.5vw;
        font-weight: 700;
        position: relative;
        .homeIcon {
          width: 25vw;
        }
      }
    }
  }
}
</style>
