$(document).ready(function () {
    console.log(206000 * 1.9999);
    try {
        window.Origami.fastclick(document.body);
    } catch (e) {
    }
    var clickEvent = $.isFunction($.fn.tap) ? 'tap' : 'click';
    window['max_zindex'] = 100;
    $.fn.show_dialog = function () {
        window['max_zindex'] += 1;
        this.css('z-index', window['max_zindex']);
        this.show();
    };
    var selYear = window.document.getElementById("selYear");
    var selMonth = window.document.getElementById("selMonth");
    var selDay = window.document.getElementById("selDay");
// 新建一个DateSelector类的实例，将三个select对象传进去
    new DateSelector(selYear, selMonth, selDay, 0, 0, 0);
    $('.btn_play').on(clickEvent, function () {
        $('.page_home').hide();
        $('.swiper-one').show();
        var swiper = new Swiper('.swiper-one', {
            pagination: '.swiper-pagination',
            paginationClickable: true,
            direction: 'vertical'
        });
    });
    $('.n-7').on(clickEvent, function () {
        $('.t-s').show();
        $('.page_3 .hand').hide();
    });
    $('.page_5 .d-a').on(clickEvent, function () {
        $('.word_two').show();
        $('.page_5 .hand').show();
        $('.page_5 .hand_5').hide();
    });
    $('.btn_money').on(clickEvent, function () {
        $('.swiper-one').hide();
        $('.page_4_2').show();
    });
    $('.btn_c-x').on(clickEvent, function () {
        $('.page_4_2').hide();
        $('.swiper-one').show();
    });
    $('.btn_q_t').on(clickEvent, function () {
        $('.swiper-one').hide();
        $('.swiper-two').show();
        var swiper_two = new Swiper('.swiper-two', {
            pagination: '.swiper-pagination',
            paginationClickable: true,
            noSwiping: true,
            direction: 'vertical'
        });
    });
    $('.btn').on(clickEvent, function () {
        var id = $(this).data('id');

        $('.count').hide();
        $('.btn').removeClass('btn_sate');
        $(this).addClass('btn_sate');
        if (id == 2) {
            $('.page_6 .map').show();
            $('.page_6 .map img').attr('src', 'Public/static/images/3-1.png');

        } else if (id == 3) {
            $('.page_6 .map').show();
            $('.page_6 .map img').attr('src', 'Public/static/images/3-2.png');

        } else if (id == 4) {
            $('.page_6 .map').show();
            $('.page_6 .map img').attr('src', 'Public/static/images/3-3.png');

        } else if (id == 5) {

            $('.data').show();
        } else {
            window.location.href = '/';
        }
    });
    window.age = 2;
    $('.btn_x').on(clickEvent, function () {
        $('.btn_x').find('img').hide();
        $(this).find('img').show();
        var age_number = $(this).data('id');
        window.age = age_number;
    });


    $('.btn_s').on(clickEvent, function () {
        $('.btn_d').removeClass('h-s');
        $('.btn_d').removeClass('black');
        var year = $('#selYear').val();
        var yue = $('#selMonth').val();
        var myDate = new Date();
        var ri = $('#selDay').val();
        if (!(year && yue && ri)) {

            my_notify('请填写日期');
            return;
        }
        var count_date = year + '-' + yue + '-' + ri;
        var count_year = ages(count_date);
        $('.count').hide();
        $('.h-s').show();
        var month = myDate.getMonth() + 1;
        var day = myDate.getDate();


        if (month >= yue && day >= ri) {
            $('.n-l').text(count_year);


        } else {

            if (count_year == 0) {

                $('.n-l').text(count_year);
            } else {
                $('.n-l').text(count_year - 1);

            }
        }
        if (window.age == 1) {
            $('.sex').text('男性');

        } else {
            $('.sex').text('女性');

        }
        if (count_year >= 56 && count_year <= 60) {
            $('.btn-5').addClass('h-s');
        } else if (count_year >= 61 && count_year <= 65) {
            $('.btn-5').addClass('h-s');
            $('.btn-4').addClass('h-s');
        } else if (count_year >= 66 && count_year <= 70) {
            $('.btn-5').addClass('h-s');
            $('.btn-4').addClass('h-s');
            $('.btn-3').addClass('h-s');
        } else if (count_year >= 71) {
            $('.btn_d').addClass('h-s');
        }
        $('.y-s').text('0');
        $('.money').val('');
        $('.count_money').text('');
    });
    $('.btn_t_close').on(clickEvent, function () {
        $('.dialog').hide();
    });
    $('.btn_d').on(clickEvent, function () {
        if ($(this).hasClass("h-s")) {
            my_notify('暂无');
            return;
        }
        var x_money = $('.money').val();
        if (x_money < 100000) {
            $('.dialog_t').show();

            return;
        }
        var z_s = x_money % 1000;

        if (z_s != 0) {
            $('.dialog_t').show();
            return;
        }
        $('.btn_d').removeClass('black');
        $(this).addClass('black');
        var age = $('.n-l').text();
        var year = $(this).data('id');
        $('.info_2').text(year);

        $.post('index/algorithm', {year: year, age: age, type: window.age}, function (resp) {
            if (resp.errcode == 0) {
                if (resp.sate == 0) {

                    my_notify('暂无')
                } else {
                    var b_l = resp.info;
                    var f_l = b_l * parseInt(x_money / 1000);
                    $('#count_money').val(f_l);
                    $('.info_3').text(formatCurrency(toDecimal2(toDecimal2(f_l))));
                    if (year == 100) {

                        $('.info_4').text(formatCurrency((toDecimal2(f_l))));
                        $('.info_2').text('1次交清');
                    } else {

                        $('.info_4').text(formatCurrency(toDecimal2(f_l * year)));

                    }

                }

            }
        });
    });


    $('.money').bind('keyup', function () {
        var countMoney = parseInt($('.money').val());
        $('#countMoney').val(countMoney / 1000);
        $('#end').val(countMoney);
        $('.count_money').text(formatCurrency(countMoney));
        $('.info_1').text(formatCurrency(countMoney));
    });
    $('.btn-end').on(clickEvent, function () {

        $('.list-c').html('');
        var year_money = $('#count_money').val();

        var n_l = $('.n-l').text();
        var count_two = $('.info_2').text();
        var count_three = parseInt(106) - parseInt(count_two);
        if (count_two == '1次交清') {
            count_two = parseInt(106) - parseInt(n_l);
            count_three = parseInt(106) - parseInt(count_two);

        }

        var type = $('.info_2').text();
        if (type == '1次交清') {
            for (var l = 0; l < 1; ++l) {
                var list_l = $('<div class="c-l"></div>');
                list_l.addClass('count_' + l);
                // list_l.text(formatCurrency(toDecimal2(year_money * (l + 1))));
                list_l.text(parseInt(year_money * (l + 1)));
                $('.list-3').append(list_l);

            }

            type = '100';
            count_two = 1;
        } else {

            for (var a = 0; a < count_two; ++a) {

                var list_three = $('<div class="c-l"></div>');

                if (type != '100') {

                    list_three.addClass('count_' + a);
                    // list_three.text(formatCurrency(toDecimal2(year_money * (a + 1))));
                    list_three.text(parseInt(year_money * (a + 1)));

                    $('.list-3').append(list_three);
                }

            }
        }
        for (var n = 0; n < 106 - n_l - count_two; ++n) {
            var list_n = $('<div class="c-l"></div>');
            list_n.addClass('count_' + parseInt(n + parseInt(count_two)));
            // list_n.text(formatCurrency(toDecimal2(year_money * count_two)));
            list_n.text(parseInt(year_money * count_two));
            $('.list-3').append(list_n);

        }
        window.count_two = count_two;
        for (var i = 0; i < 106 - n_l; ++i) {
            var list = $('<div class="c-l"></div>');
            var count = i + 1;
            list.text(count);
            $('.list-1').append(list);

        }


        for (var h = 0; h < 106 - n_l; ++h) {

            var list_two = $('<div class="c-l"></div>');
            list_two.addClass('age_' + h);
            list_two.text(parseInt(h) + parseInt(n_l));
            $('.list-2').append(list_two);

        }


        var prem_total = $('#count_money').val();//累计已交保费
        var value; // 现价
        var insurance;  //身故保险金
        var r;//系数
        var policy = $('#countMoney').val();//保额
        $.post('index/ceshi', {type: type, sex: window.age, j_year: count_two, year: n_l}, function (resp) {
            if (resp.errcode == 0) {
                if (resp.sate == 0) {

                    var u_money = parseInt($('.money').val() / 1000);
                    for (var i = 0; i < 105 - n_l; ++i) {
                        var list_four = $('<div class="c-l"></div>');
                        var list_five = $('<div class="c-l"></div>');
                        var age = $('.age_' + i).text();//被保人（按照每行年龄身故）年龄
                        var prem = $('.count_' + i).text();//当年应缴保费
                        console.log(prem);
                        if (age <= 41) {
                            r = 1.6;
                        } else if (age <= 61) {
                            r = 1.4;

                        } else if (age >= 61) {
                            r = 1.2;
                        }
                        if (resp.key == 0) {
                            list_four.text(formatCurrency(toDecimal2(resp.list[i].one * u_money)));
                            value = resp.list[i].one

                        } else if (resp.key == 1) {

                            list_four.text(formatCurrency(toDecimal2(resp.list[i].two * u_money)));
                            value = resp.list[i].two
                        } else if (resp.key == 2) {

                            list_four.text(formatCurrency(toDecimal2(resp.list[i].three * u_money)));
                            value = resp.list[i].three
                        }
                        else if (resp.key == 3) {

                            list_four.text(formatCurrency(toDecimal2(resp.list[i].four * u_money)));
                            value = resp.list[i].four
                        }
                        else if (resp.key == 4) {


                            list_four.text(formatCurrency(toDecimal2(resp.list[i].five * u_money)));
                            value = resp.list[i].five
                        } else if (resp.key == 5) {

                            list_four.text(formatCurrency(toDecimal2(resp.list[i].six * u_money)));
                            value = resp.list[i].six
                        } else if (resp.key == 6) {

                            list_four.text(formatCurrency(toDecimal2(resp.list[i].seven * u_money)));
                            value = resp.list[i].seven
                        } else if (resp.key == 7) {

                            list_four.text(formatCurrency(toDecimal2(resp.list[i].eight * u_money)));
                            value = resp.list[i].eight
                        } else if (resp.key == 8) {


                            list_four.text(formatCurrency(toDecimal2(resp.list[i].nine * u_money)));
                            value = resp.list[i].nine
                        } else if (resp.key == 9) {


                            list_four.text(formatCurrency(toDecimal2(resp.list[i].ten * u_money)));
                            value = resp.list[i].ten
                        } else if (resp.key == 10) {
                            list_four.text(formatCurrency(toDecimal2(resp.list[i].eleven * u_money)));
                            value = resp.list[i].eleven
                        }
                        if (i > 0) {
                            policy = policy * 1.03;
                        }

                        if (i >= window.count_two) {
                            var p = $('#end').val();
                            // console.log(p);
                            var d = p * 0.03 / 0.03;
                            $('#end').val(d);
                            // console.log(d);
                            if (age < 18) {
                                insurance = Math.max(prem, value * u_money);
                            } else if (age >= 18) {
                                insurance = Math.max(parseInt(window.count), value * u_money, d);
                                insurance = insurance;
                            }


                        } else {
                            if (age < 18) {
                                insurance = Math.max(prem, value * u_money);

                            } else if (age >= 18) {
                                insurance = Math.max(prem_total * r, value, parseInt(policy));
                                insurance = insurance * parseInt(i + 1);
                            }


                        }
                        var t = $('#end').val();
                        var a = t * 1.03;
                        $('#end').val(a);

                        window.count = insurance;

                        list_five.text(formatCurrency(toDecimal2(insurance)));
                        // list_five.text(insurance);
                        // list_five.text(insurance);
                        $('.list-4').append(list_four);
                        $('.list-5').append(list_five);
                    }

                    // var ceshi=$('.n-l').text();
                    // if(ceshi==0){
                    var list_six = $('<div class="c-l"></div>');
                    var list_six_two = $('<div class="c-l"></div>');
                    list_six.text(0);
                    list_six_two.text(0);
                    $('.list-4').append(list_six);
                    $('.list-5').append(list_six_two);
                    // }
                }

            }
        });


        $('.dialog_end').show();
    });

    $('.btn_close').on(clickEvent, function () {
        $('.dialog_end .list').get(0).scrollHeight == 0;
        $('.dialog').hide();
        $('#end').val('0');
    })
});
function toDecimal2(x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(x * 1000) / 1000;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

function fomatFloat(src, pos) {
    return Math.round(src * Math.pow(10, pos)) / Math.pow(10, pos);
}
function formatCurrency(num) {
    if (num) {
        //将num中的$,去掉，将num变成一个纯粹的数据格式字符串
        num = num.toString().replace(/\$|\,/g, '');
        //如果num不是数字，则将num置0，并返回
        if ('' == num || isNaN(num)) {
            return 'Not a Number ! ';
        }
        //如果num是负数，则获取她的符号
        var sign = num.indexOf("-") > 0 ? '-' : '';
        //如果存在小数点，则获取数字的小数部分
        var cents = num.indexOf(".") > 0 ? num.substr(num.indexOf(".")) : '';
        cents = cents.length > 1 ? cents : '';//注意：这里如果是使用change方法不断的调用，小数是输入不了的
        //获取数字的整数数部分
        num = num.indexOf(".") > 0 ? num.substring(0, (num.indexOf("."))) : num;
        //如果没有小数点，整数部分不能以0开头
        if ('' == cents) {
            if (num.length > 1 && '0' == num.substr(0, 1)) {
                return 'Not a Number ! ';
            }
        }
        //如果有小数点，且整数的部分的长度大于1，则整数部分不能以0开头
        else {
            if (num.length > 1 && '0' == num.substr(0, 1)) {
                return 'Not a Number ! ';
            }
        }
        //针对整数部分进行格式化处理，这是此方法的核心，也是稍难理解的一个地方，逆向的来思考或者采用简单的事例来实现就容易多了
        /*
         也可以这样想象，现在有一串数字字符串在你面前，如果让你给他家千分位的逗号的话，你是怎么来思考和操作的?
         字符串长度为0/1/2/3时都不用添加
         字符串长度大于3的时候，从右往左数，有三位字符就加一个逗号，然后继续往前数，直到不到往前数少于三位字符为止
         */
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
            num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
        }
        //将数据（符号、整数部分、小数部分）整体组合返回
        return (sign + num + cents);
    }

}
function ages(str) {
    var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (r == null)return false;
    var d = new Date(r[1], r[3] - 1, r[4]);
    if (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4]) {
        var Y = new Date().getFullYear();
        return ((Y - r[1]));
    }
    return ("输入的日期格式错误！");
}
function DateSelector(selYear, selMonth, selDay) {
    this.selYear = selYear;
    this.selMonth = selMonth;
    this.selDay = selDay;
    this.selYear.Group = this;
    this.selMonth.Group = this;
// 给年份、月份下拉菜单添加处理onchange事件的函数
    if (window.document.all != null) // IE
    {
        this.selYear.attachEvent("onchange", DateSelector.Onchange);
        this.selMonth.attachEvent("onchange", DateSelector.Onchange);
    }
    else // Firefox
    {
        this.selYear.addEventListener("change", DateSelector.Onchange, false);
        this.selMonth.addEventListener("change", DateSelector.Onchange, false);
    }
    if (arguments.length == 4) // 如果传入参数个数为4，最后一个参数必须为Date对象
        this.InitSelector(arguments[3].getFullYear(), arguments[3].getMonth() + 1, arguments[3].getDate());
    else if (arguments.length == 6) // 如果传入参数个数为6，最后三个参数必须为初始的年月日数值
        this.InitSelector(arguments[3], arguments[4], arguments[5]);
    else // 默认使用当前日期
    {
        var dt = new Date();
        this.InitSelector(dt.getFullYear(), dt.getMonth() + 1, dt.getDate());
    }
}
// 增加一个最大年份的属性
DateSelector.prototype.MinYear = 1900;
// 增加一个最大年份的属性
DateSelector.prototype.MaxYear = (new Date()).getFullYear();
// 初始化年份
DateSelector.prototype.InitYearSelect = function () {
// 循环添加OPION元素到年份select对象中
    for (var i = this.MaxYear; i >= this.MinYear; i--) {
// 新建一个OPTION对象
        var op = window.document.createElement("OPTION");
// 设置OPTION对象的值
        op.value = i;
// 设置OPTION对象的内容
        op.innerHTML = i;
// 添加到年份select对象
        this.selYear.appendChild(op);
    }
}
// 初始化月份
DateSelector.prototype.InitMonthSelect = function () {
// 循环添加OPION元素到月份select对象中
    for (var i = 1; i < 13; i++) {
// 新建一个OPTION对象
        var op = window.document.createElement("OPTION");
// 设置OPTION对象的值
        op.value = i;
// 设置OPTION对象的内容
        op.innerHTML = i;
// 添加到月份select对象
        this.selMonth.appendChild(op);
    }
}
// 根据年份与月份获取当月的天数
DateSelector.DaysInMonth = function (year, month) {
    var date = new Date(year, month, 0);
    return date.getDate();
}
// 初始化天数
DateSelector.prototype.InitDaySelect = function () {
// 使用parseInt函数获取当前的年份和月份
    var year = parseInt(this.selYear.value);
    var month = parseInt(this.selMonth.value);
// 获取当月的天数
    var daysInMonth = DateSelector.DaysInMonth(year, month);
// 清空原有的选项
    this.selDay.options.length = 0;
// 循环添加OPION元素到天数select对象中
    for (var i = 1; i <= daysInMonth; i++) {
// 新建一个OPTION对象
        var op = window.document.createElement("OPTION");
// 设置OPTION对象的值
        op.value = i;
// 设置OPTION对象的内容
        op.innerHTML = i;
// 添加到天数select对象
        this.selDay.appendChild(op);
    }
}
// 处理年份和月份onchange事件的方法，它获取事件来源对象（即selYear或selMonth）
// 并调用它的Group对象（即DateSelector实例，请见构造函数）提供的InitDaySelect方法重新初始化天数
// 参数e为event对象
DateSelector.Onchange = function (e) {
    var selector = window.document.all != null ? e.srcElement : e.target;
    selector.Group.InitDaySelect();
}
// 根据参数初始化下拉菜单选项
DateSelector.prototype.InitSelector = function (year, month, day) {
// 由于外部是可以调用这个方法，因此我们在这里也要将selYear和selMonth的选项清空掉
// 另外因为InitDaySelect方法已经有清空天数下拉菜单，因此这里就不用重复工作了
    this.selYear.options.length = 0;
    this.selMonth.options.length = 0;
// 初始化年、月
    this.InitYearSelect();
    this.InitMonthSelect();
// 设置年、月初始值
    this.selYear.selectedIndex = this.MaxYear - year;
    this.selMonth.selectedIndex = month - 1;
// 初始化天数
    this.InitDaySelect();
// 设置天数初始值
    this.selDay.selectedIndex = day - 1;
}


window.wx && wx.ready(function () {
    document.getElementById('music').play();
    var base = location.origin;
    var wxFriendData = {
        'link': base,
        'imgUrl': base + '/Public/static/images/share.png',
        // 内容
        'desc': '中银三星人寿倾情巨制，祝您尊享传世荣耀，智盈财富人生。',
        // 标题
        'title': '尊享家盈二号终身寿险',
        trigger: function () {
        },
        success: function () {
        }
    };
    var wxTimelineData = {
        'link': base,
        'imgUrl': base + '/Public/static/images/share.png',
        'title': '尊享家盈二号终身寿险',
        trigger: function () {


        },
        success: function () {

        }
    };
    wx.onMenuShareAppMessage(wxFriendData);
    wx.onMenuShareTimeline(wxTimelineData);
});