import {Component, Vue} from 'vue-property-decorator';
import Http from "../../extend/Http";
import Utils from "../../extend/Utils";
import router from "../../router";
import {Getter, Action} from 'vuex-class';
import {IndexData} from '@/types/views/index.interface';
import Carousel from '@/components/Carousel/Carousel.vue';
// import {  } from "@/components" // 组件
import {Message} from "element-ui";

@Component({
    components: {
    },
})
export default class Index extends Vue {
    // Getter
    // @Getter author

    // Action
    // @Action GET_DATA_ASYN: any;

    // data
    data: any = {
        markShow:false,
        appid:'wx98622dc60a8a6abb',
        show: 1,
        bannerlist:'',
        id_card: null
    };
    private citycode = localStorage.getItem("citycode");
    private cityname = localStorage.getItem("cityname");

   async  created() {
        if(localStorage.getItem('access_token')){
           let res = await Http.getInstance().userinfo();
            let resbanner = await Http.getInstance().getBannerList();
            this.data.id_card = res.data.id_card;
            this.data.bannerlist = resbanner.data;
            this.citycode = res.data.social_city  as string;
            this.cityname = res.data.city_name as string;
            localStorage.setItem("citycode", this.citycode);
            localStorage.setItem("cityname", this.cityname);
            localStorage.setItem('mobile',res.data.mobile);
            localStorage.setItem("social_type", res.data.social_type);
            localStorage.setItem("social_name", res.data.social_name);
            if(res.data.is_first == 0){
                this.data.markShow = true
                let ress = await Http.getInstance().setFirstLogin();
            }else {
                let isWeiXin = () => { return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1 }
                if (isWeiXin()) {
                    console.log(res.data.openid)
                    if(res.data.openid == null || res.data.openid.length == 0){
                        if(!localStorage.getItem('openid')){
                            let url = Utils.getInstance().getParams(location.href);
                            if(url.code){
                                let ress = await Http.getInstance().getWeixinpay({
                                    code: url.code,
                                });
                                if(ress.data.openid){
                                    let rer = await Http.getInstance().setUserOpenid({
                                        openid: ress.data.openid
                                    });
                                    localStorage.setItem("openid", ress.data.openid);
                                }
                            }else{
                                this.getcode();
                            }
                        }
                    }else{
                        localStorage.setItem("openid", res.data.openid);
                    }

                }
                this.data.markShow = false
            }
        }
    }
    async getcode(){
        let redirectURI='https://hr.hemadj.com/index';
        window.location.replace(
            'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.data.appid + '&redirect_uri=' + redirectURI + '&response_type=code&scope=snsapi_base&state=WXPaySTATE#wechat_redirect'
        )
    }
    async close(event: any){
        // let ress = await Http.getInstance().setFirstLogin();
        // console.log(ress);
        this.data.markShow = false;
        let isWeiXin = () => { return navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1 }
        if (isWeiXin()) {
            if(!localStorage.getItem('openid')){
                let url = Utils.getInstance().getParams(location.href);
                if(url.code){
                    let ress = await Http.getInstance().getWeixinpay({
                        code: url.code,
                    });
                    if(ress.data.openid){
                        let rer = await Http.getInstance().setUserOpenid({
                            openid: ress.data.openid
                        });
                        localStorage.setItem("openid", ress.data.openid);
                    }
                }else{
                    this.getcode();
                }
            }
        }

    }
    yaoqing(){
       this.$router.push('/user/share')
    }
    jiaofei(){
       this.$router.push('/other')
    }
    tanrules(){
         console.log(this.data.show)
        this.$router.push({
            name: "share", params: {
               state: this.data.show
            }
        });

    }

    activated() {
        //
    }



    // 初始化函数
    init() {
        //
    }

    // 点击五险一金
    hFiveSocial() {
        // 获取本地信息
        let name = localStorage.getItem('realname')
        let socialType = localStorage.getItem('token_type')
        if (name === '' || socialType === '' || this.data.id_card === '') {
            this.$router.push('/user/info')
        } else {
            this.$router.push('/social?is_fund=1')
        }
    }

    // 点击社保
    hSocialInsurance () {
        let name = localStorage.getItem('realname')
        let socialType = localStorage.getItem('token_type')
        // 获取本地社保类型
        let is_town = localStorage.getItem('is_town')
        console.log(is_town);
        if (is_town === 'true') {
            Message({
                message: '城镇户口只能交五险一金，不能交社保', type: "warning"
            });
            return
        }
        if (name === '' || socialType === '' || this.data.id_card === '') {
            this.$router.push('/user/info')
        } else {
            this.$router.push('/social?is_fund=0')
        } 
    }

}
