﻿
jQuery.fn.Price = function(config) {

    //参数
    config = jQuery.extend({
        id: null,    //ID
        catNo: null,
        className: 'CountryPrice',
        row: null,
        callback: function() { return false; } //回调函数
    }, config || {});

    return this.each(function() {

        var me = $(this);

        init = function() {
            userFrom(make);
        };

        make = function() {
            $.getJSON("../Handler/SystemHandler.ashx?r=" + Math.random(),
            { func: 'GetCountry' },
            function(res) {
                //1. from cookie
                var countrySelected = $.cookie('ptg_user_country_selected');
                //2. from login info
                if (countrySelected == null && $.cookie('ptg_user_info_login') != null)
                    countrySelected = $.cookie('ptg_user_info_login').Country;
                //3. from page response
                if (countrySelected == null && $.cookie('ptg_user_web_from') != null)
                    countrySelected = $.cookie('ptg_user_web_from').CountryName;

                var str = new Array();
                str.push('<table border="0" class="' + config.className + '"><tbody>');
                str.push('<tr height="20"><th width="130">Country/Region</th><th width="70">Size</th><th width="60">Price</th><th width="95">Freight/Packing</th></tr>');
                str.push('<tr><td style="height: 20px;">');
                str.push('<select name="select" id="country" style="width: 126px;height: 20px;">');
                str.push('<option agent="" value="">Select Country</option>');

                $.each(res.table[0].rows, function(i) {
                    var c = res.table[0].rows[i].CountryName;
                    str.push('<option agent="' + res.table[0].rows[i].AgentOrNot + '" value="' + c + '" ');
                    if (countrySelected && (countrySelected.toLowerCase() == c.toLowerCase()))
                        str.push(' selected ');
                    str.push('>' + c + '</option>');
                });
                str.push('</select>');
                str.push('</td><td style="height: 20px;"><span id="spSize">' + config.row.Concentration + 'ug</span></td>');
                str.push('<td style="height: 20px;"><span id="spPrice">Loading</span></td>');
                str.push('<td style="height: 20px;"><span id="spFreightFee">Loading</span></td>');
                str.push('</tr></tbody></table>');
                str.push('<div id="dvProAddCart" name="' + config.catNo + '" style="padding-right: 5px;" class="addMiniCart" title="Add to cart">');
                str.push('</div>');
                str.push('<div id="dvProNoCountry"/>');

                me.empty().append(str.join(''));

                //Button Event
                $('#dvProAddCart').click(function() { addtoMiniCart(this); });

                //Select Event
                $('#country').change(function() { setPriceVal(this); });

                setPriceVal();

            });
        };

        setPriceVal = function(o) {
            var cName = $('#country').val();
            //null
            if (cName == "") {
                $('#spPrice').text('--');
                $('#spFreightFee').text('--');
                $('#dvProAddCart').hide();
                $('#dvProNoCountry').empty().html('Select a country first to see the price').show();

            }
            //other
            else if (cName.toLowerCase().indexOf("other")>=0) {
                $('#spPrice').text('--');
                $('#spFreightFee').text('--');
                $('#dvProAddCart').hide();
                $('#dvProNoCountry').empty().html('<a href="Contacts.aspx#US">Please contact our US headquater</a>').show(); ;

            }

            //Agent
            else if ($('#country option:selected').attr('agent').toLowerCase() == 'true') {
                $('#spPrice').text('--');
                $('#spFreightFee').text('--');
                $('#dvProAddCart').hide();
                var url = '<a href="Contacts.aspx';
                if (cName.toLowerCase() != 'china') url = url + '?step=1';
                $('#dvProNoCountry').empty().html(url + '#' + cName + '">Please contact our ' + cName + ((cName.toLowerCase() == 'china') ? ' branch' : ' distributor') + '</a>').show();
            }

            else {
                //read price
                $.getJSON("../Handler/ProductHandler.ashx?r=" + Math.random(),
                { func: 'GetProductPrice', param: 'catNo=' + config.catNo + '|cName=' + cName },
                function(res) {
                    if (res.table[1].rows.length > 0) {
                        $('#spPrice').html(res.table[0].rows[0].Currency + parseFloat(res.table[2].rows[0].Price).format('#.00'));
                        $('#spSize').html(res.table[1].rows[0].Size);
                    }
                    $('#spFreightFee').html(res.table[0].rows[0].Currency + parseFloat(res.table[0].rows[0].ShippingFee).format('#.00'));

                    $('#dvProNoCountry').hide();
                    $('#dvProAddCart').show();

                    $.cookie('ptg_user_currency_selected', res.table[0].rows[0].Currency, SYSTEM_CONFIG.CookieOpt);

                });
            }

            $.cookie('ptg_user_country_selected', $('#country').val(), SYSTEM_CONFIG.CookieOpt);

        };


        //insert into mini cart
        addtoMiniCart = function(o) {
            $.cookie('ptg_user_country_selected', $('#country').val(), SYSTEM_CONFIG.CookieOpt);
            $.MiniCart({ catNo: config.catNo, country: $('#country').val(), dowhat: 'add', from: 'pro' });
        }

        start = function() {
            init();
            //make();
        };

        start();

    });
}