/**
 * 小储云商城
 * 模板视图js调用类 cool
 * @type {{input_data: (function(*, *, *, *): string), data_class: (function(*): string), num: (function(): string), data_goods: (function(*): string)}}
 */
var template = {
    data_goods: function (data) { //商品列表模板
        if (data.quota != 0) {
            var quota = '' + data.quantity + '个';
        } else {
            var quota = '<font color=#EF5350>库存不足</font>'
        }
        if (data.image == '') {
            data.image = 'assets/cloud/image/error_img.png';
        }

        if (data.price == 0) {
            var buy = '<span class="buy" style="background-color: yellowgreen;color:#fff;position: absolute;    right: 0;">领取</span>'
        } else {
            var buy = '<span class="buy" style="color:#fff;position: absolute;    right: 0;">购买</span>';
        }

        var content = '<a class="fui-goods-item" title="' + quota + '" href="?mod=shop&gid=' + data.gid + '&cid=' + data.cid + '">\n' +
            '      <div class="image" style="background-image: url(' + data.image + '); background-size: 100% 100%">\n' +
            '      </div>\n' +
            '      <div class="detail">\n' +
            '          <div class="name" style="color: #000000;">\n' +
            '             ' + data.name + '<br>' + quota + '          </div>\n' +
            '          <div class="price" style="margin-top: 0.9rem;">\n' +
            '              <span class="text" style="color: #ff5555;"> <p class="minprice">￥' + (data.price - 0) + '</p> </span>\n' +
            '\n' +
            '              ' + buy + '          </div>\n' +
            '      </div>\n' +
            '  </a>';
        return content;
    },
    input_data: function (name, placeholder, id, addstr) { //商品输入框模板
        if (addstr != '') {
            var content = '<div class="layui-form-item">\n' +
                '      <label class="layui-form-label"  style="width: 100%;text-align: left;padding:0" id="var_c' + id + '" >' + name + '</label>\n' +
                '      <div class="layui-input-"  style="padding-right: 80px !important;" >\n' +
                '          <input type="text" onblur="checkInput(\'var_c' + id + '\',\'input_val_' + id + '\')" placeholder="' + placeholder + '"  id="input_val_' + id + '"    class="layui-input" />\n' +
                '      </div>' + addstr + '\n' +
                '  </div>';
        } else {
            var content = '<div class="layui-form-item">\n' +
                '      <label class="layui-form-label"  style="width: 100%;text-align: left;padding:0" id="var_c' + id + '" >' + name + '</label>\n' +
                '      <div class="layui-input-">\n' +
                '          <input type="text"  onblur="checkInput(\'var_c' + id + '\',\'input_val_' + id + '\')" placeholder="' + placeholder + '"  id="input_val_' + id + '"  class="layui-input">\n' +
                '      </div>\n' +
                '  </div>';
        }
        return content;
    },
    select_data: function (selectname, addstr, i) { //多选框构建
        var content = '<div class="layui-form-item">\n' +
            '      <label class="layui-form-label"  style="width: 100%;text-align: left;padding:0" id="var_c' + i + '">' + selectname + '</label>\n' +
            '      <div class="layui-input-">\n' +
            '          <select lay-verify="required"  id="input_val_' + i + '">\n' +
            '              <option value=""></option>' + addstr + '\n' +
            '          </select>\n' +
            '      </div>\n' +
            '  </div>';
        return content;
    },
    num: function () { //商品下单数量模板
        var content = '<div class="layui-form-item">\n' +
            '      <label class="layui-form-label" style="width: 100%;text-align: left;padding:0">下单份数</label>\n' +
            '      <div class="layui-input-">\n' +
            '          <input type="number" min="1" value="1"  placeholder="请填写商品下单份数！"  id="num"  class="layui-input">\n' +
            '      </div>\n' +
            '  </div>';
        return content;
    }, success: function (data) {
        if (data.data['CarriageTem'] != undefined && data.data['freight'] != "") { //启用运费模板
            if (data.data['freight'] != "-1") {
                CarriageTem.start(data.data['CarriageTem'], data.data['freight'], data.data['price']);
            }
        }

        var method = data.data.method; //商品状态

        $("#gootsp").text($("#gootsp").attr('value'));
        $(".pay_price").html(data.data['price'] + '元<br>');
        $(".quota").text(data.data['count_quota']);
        $(".points").text(data.data['points']);
        $("#level").html('当前级别：<font color="#48d1cc">' + data.data['level'] + '</font>');

        if (method.indexOf(1) != -1 || method.indexOf(2) != -1) { //是否关闭金额显示！
            $(".pay_prices").show(10);
        }

        if (method.indexOf(3) != -1) { //是否关闭积分显示！
            $(".pointss").show();
        }

        var picture = $("#gootsp").attr('data-picture');
        var coutent_img = '';
        if (picture == '' || picture == undefined) {
            coutent_img = '<div class="swiper-slide"><img onclick="template.image(\'' + $("#gootsp").attr('data-image') + '\')" src="' + $("#gootsp").attr('data-image') + '"  /></div>';
        } else {
            $("#swiper").show(100);
            array_img = picture.split('|');
            $.each(array_img, function (key, val) {
                coutent_img += '<div class="swiper-slide"><img onclick="template.image(\'' + val + '\')" src="' + val + '"  /></div>';
            });
        }
        $("#picture").html(coutent_img);

        var swiper = new Swiper('.swiper-container', {
            slidesPerView: 1,
            spaceBetween: 30,
            loop: true,
            pagination: {
                el: '.swiper-pagination',
                dynamicBullets: true,
            }
        });

        var level_arr = $("#gootsp").attr('data-level_arr');
        if (level_arr == '-1') {
            layer.tips('恭喜！您当前为最高等级密价！', '#level', {tips: [1, '#651FFF']});
            $("#level").attr('onclick', 'layer.msg(\'恭喜！您当前为最高等级密价！\')');
        } else {
            layer.tips('此商品有更多优惠哦！<br>点击查看获取！', '#level', {tips: [1, '#6200EA']});

            $("#level").attr('onclick', 'layer.alert(\'' + unescape(level_arr) + '\',{title:\'密价等级表\',btn:[\'朕已阅\',\'升级密价\'],shade: [0.8, \'#000\'],shadeClose:true,btn2:function(layero,index){window.open(\'/user/grade.php\')}})');
        }

        $(".thumb img").attr('src', data.data.image);
    },
    image: function (img) {
        swal({
            title: false,
            text: false,
            imageUrl: img,
            imageWidth: 300,
            imageHeight: 300,
            animation: false,
            customClass: 'jello animated',
            showConfirmButton: false,
        });
    },
    query_tem: function (data) { //订单模板
        $("#order_all,#order_a,#order_b,#order_c,#order_d,#order_e").html('');
        var content_error = '<div class="layui-col-xs12 layui-col-sm6 layui-col-md4 layui-col-lg3"><div class="layui-card qt-card">\n' +
            '              <div class="layui-card-header">\n' +
            '               <p style="width: 70%" class="layui-elip">没有查询到相关订单</p>\n' +
            '                       </div>\n' +
            '                        <div class="layui-card-body">\n' +
            '                       <div class="layui-row layui-col-space10">\n' +
            '                            <div class="layui-col-xs4" style="text-align: center">\n' +
            '                                <img src="' + logo + '">\n' +
            '                            </div><div class="layui-col-xs8" style="text-align: center">\n' +
            '                                登陆后台可永久保存订单哦<hr><a href="user">进入后台</a>\n' +
            '                            </div>\n' +
            '         </div>\n' +
            '    </div>\n' +
            '</div></div><hr>';

        if (data.code == 1) {
            var content = ''; //全部订单
            var content_a = ''; //已完成
            var content_b = ''; //待处理
            var content_c = ''; //异常订单
            var content_d = ''; //进行中
            var content_e = ''; //已退款
            $.each(data.data, function (key, val) {

                switch (val['state']) {
                    case '1': //成功
                        var state = '<font color="#43A047">已完成</font>';
                        break;
                    case '2': //待处理
                        var state = '<font color="#651FFF">待处理</font>';
                        break;
                    case '3': //异常
                        var state = '<font color="#FF1744">异常中</font>';
                        break;
                    case '4': //正在处理
                        var state = '<font color="#F57C00">正在处理</font>';
                        break;
                    case '5': //已退款
                        var state = '<font color="#424242">已退款</font>';
                        break;
                    default:
                        var state = '<font color="#607D8B">未知状态</font>';
                        break;
                }

                if (val.payment == '积分付款') {
                    val.price = val.price + '积分';
                } else if (val.payment == '免费领取') {
                    val.price = '<font color="#2e8b57">免费商品</font>';
                } else {
                    val.price = val.price + '元';
                }

                if (val.token !== "0") {
                    msg_token = (val.token).join('<br>');
                    btns = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="layer.alert(\'卡密信息为：<br>' + msg_token + '\',{title:\'您购买的卡密信息\'})">\n' +
                        '                             提取卡密\n' +
                        '                      </button>';
                } else {
                    btns = '';
                }

                if (val.logistics != "-1") { //有快递？当然是查询
                    btne = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="layer.alert(\'快递单号信息：<br>' + val.logistics + '\<hr>查询快递方法：复制快递单号，手机打开菜鸟包裹查询即可！\',{title:\'您的快递单号\'})">\n' +
                        '                             快递单号\n' +
                        '                      </button>';
                } else {
                    btne = '';
                }

                content += content_bf = '<div class="layui-card qt-card">\n' +
                    '              <div class="layui-card-header">\n' +
                    '               <p style="width: 70%" class="layui-elip">' + val.name + '</p>\n' +
                    '                    <span class="layui-layout-right layui-elip"\n' +
                    '                           style="width:30%;text-align: right;margin-right: 0.5em">\n' +
                    '                          ' + state + '\n' +
                    '                    </span>\n' +
                    '                       </div>\n' +
                    '                        <div class="layui-card-body">\n' +
                    '                       <div class="layui-row layui-col-space10">\n' +
                    '                            <div class="layui-col-xs4">\n' +
                    '                                <img src="' + val.image + '"/>\n' +
                    '                            </div>\n' +
                    '                       <div class="layui-col-xs8"\n' +
                    '                            style="font-size: 0.8em;color:black;font-family: \'微软雅黑\'">\n' +
                    '                            下单时间：' + val.addtitm + '<br>\n' +
                    '                            商品总价：' + val.price + '<br>\n' +
                    '                            购买方式：' + val.payment + '\n' +
                    '                      </div>\n' +
                    '                  <div style="width: 100%;text-align: right">\n' +
                    '                       <button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_data(' + val.id + ')">\n' +
                    '                             查看详情\n' +
                    '                      </button>' + btns + btne + '\n' +
                    '               </div>\n' +
                    '         </div>\n' +
                    '    </div>\n' +
                    '</div>';

                switch (val['state']) {
                    case '1': //成功
                        content_a += content_bf;
                        break;
                    case '2': //待处理
                        content_b += content_bf;
                        break;
                    case '3': //异常
                        content_c += content_bf;
                        break;
                    case '4': //正在处理
                        content_d += content_bf;
                        break;
                    case '5': //已退款
                        content_e += content_bf;
                        break;
                }


                content_bf = '';
            });

            if (content == '') {
                content = content_error;
            }
            if (content_a == '') {
                content_a = content_error;
            }
            if (content_b == '') {
                content_b = content_error;
            }
            if (content_c == '') {
                content_c = content_error;
            }
            if (content_d == '') {
                content_d = content_error;
            }
            if (content_e == '') {
                content_e = content_error;
            }

            $("#order_all").html(content);
            $("#order_a").html(content_a);
            $("#order_b").html(content_b);
            $("#order_c").html(content_c);
            $("#order_d").html(content_d);
            $("#order_e").html(content_e);
            content = '';
        } else {
            $("#order_all,#order_a,#order_b,#order_c,#order_d,#order_e").html(content_error);
        }
    },
    SharePoster: function (gid) {
        layer.msg('正在生成商品分享海报...', {icon: 16, time: 9999999})
        $.ajax({
            type: "post",
            url: "/ajax.php?act=SharePoster",
            data: {gid: gid},
            dataType: "json",
            success: function (data) {
                if (data.code == 1) {
                    layer.alert('<img src="' + data.src + '" width=300 heigth=450 />', {
                        area: ['340px', '490px'],
                        title: false,
                        btn: false, shade: [0.8, '#000'],
                        shadeClose: true,
                    })
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            },
            error: function () {
                layer.alert('生成失败！');
            }
        });
    }

};

if ($_GET['cid'] != undefined && $_GET['gid'] == undefined || $_GET['name'] != undefined) {
    merchandise.gid_data($_GET['cid'], $_GET['name']);
} else if ($_GET['cid'] != undefined && $_GET['gid'] != undefined) {
    /**
     * 调用商品下单界面模块
     */
    merchandise.shop_data($_GET['gid']);
} else {
    merchandise.gid_data(-1);
}