<?php

/*
 * ----------------------------------------------------
 * 程序名称: 牛叉广告管理系统(NiuXams)
 * 程序功能: 实现专业的广告投放、管理和统计等管理系统！
 * 程序开发: 牛叉软件(NiuXSoft.com)
 * 版权所有: [NiuXams] (C)2013-2099 NiuXSoft.com
 * 官方网站: www.niuxsoft.com  Email: niuxsoft@163.com
 * ----------------------------------------------------
 * 使用条款:
 * 1.为构建创新型经济社会,请尊重保护知识产权！
 * 2.该软件个人非商业用途免费使用.
 * 3.免费使用禁止修改版权信息和官方推广链接.
 * 4.禁止任何衍生版本.禁止任何非法修改源代码贩卖.
 * ----------------------------------------------------
 */

defined('NIUX_INCLUDED') or exit('Access Denied.');
require './mo_head.php';
?>
<style>
    h3 {
        width: 14em;
        padding: .3em;
    }
    .text {
        font-size: 1em;
        padding: .6em;
    }
    .tip {
        margin-left: 12px;
        padding: .3em;
        display: inline-block;
    }
    .list table th{
        border-width:0px 1px 0px 0px;
        font-size:1.2em;
        line-height:2em;
    }
    .list table td{
        border-width:0px 1px 1px 0px;
        font-size: 1em;
        line-height: 1em;
        overflow: hidden;
    }
    .ui-tooltip {
        max-width: 1200px;
        word-break: break-all;
    }
</style>
<script>
    $(function () {
        function init_ajax_ggcl() {
            $(".button").button();
            $(".lei2").selectmenu({
                change: function (event, data) {
                    setCoo("lei2", $(this).val(), 120);
                    $("#listggcl").load("ggcl_list.php?menu=ajax&type=" + $("input[name='type']:checked").val(), function () {
                        init_ajax_ggcl();
                    });
                }
            });
            $(".zu2").selectmenu({
                change: function (event, data) {
                    setCoo("zu2", $(this).val(), 120);
                    $("#listggcl").load("ggcl_list.php?menu=ajax&type=" + $("input[name='type']:checked").val(), function () {
                        init_ajax_ggcl();
                    });
                }
            });
        }
        $("#fromggclk").click(function () {
            $("#listggcl").toggle();
        });
        $(document).on("click", ".cl_firstPage", function () {
            $("#listggcl").load("ggcl_list.php?menu=ajax&type=" + $("input[name='type']:checked").val(), function () {
                init_ajax_ggcl();
            });
        });
        $(document).on("click", ".cl_prePage", function () {
            $("#listggcl").load("ggcl_list.php?menu=ajax&type=" + $("input[name='type']:checked").val() + "&page=" + $(this).val(), function () {
                init_ajax_ggcl();
            });
        });
        $(document).on("click", ".cl_nexPage", function () {
            $("#listggcl").load("ggcl_list.php?menu=ajax&type=" + $("input[name='type']:checked").val() + "&page=" + $(this).val(), function () {
                init_ajax_ggcl();
            });
        });
        $(document).on("click", ".cl_pages", function () {
            $("#listggcl").load("ggcl_list.php?menu=ajax&type=" + $("input[name='type']:checked").val() + "&page=" + $(this).val(), function () {
                init_ajax_ggcl();
            });
        });
        $(document).on("click", ".selectggcl", function () {
            $(this).parent().parent().clone().appendTo("#xzggcl table");
            $("#xzggcl table tr:last").children("td").eq(0).replaceWith('<td class="ui-widget-content tc"><button type="button" class="button delselectggcl" title="' + $(this).val() + '" value="' + $(this).val() + '">移除</button></td>');
            $(".button").button();
        });
        $(document).on("click", ".delselectggcl", function () {
            $(this).parent().parent().remove();
        });
        function init_ajax_gg() {
            $(".button").button();
            $(".lei1").selectmenu({
                change: function (event, data) {
                    setCoo("lei1", $(this).val(), 120);
                    $("#listgg").load("gg_list.php?menu=ajax&wllx=" + $("input[name='type']:checked").val(), function () {
                        init_ajax_gg();
                    });
                }
            });
            $(".zu1").selectmenu({
                change: function (event, data) {
                    setCoo("zu1", $(this).val(), 120);
                    $("#listgg").load("gg_list.php?menu=ajax&wllx=" + $("input[name='type']:checked").val(), function () {
                        init_ajax_gg();
                    });
                }
            });
        }
        $("#type0").click(function () {
            $("#listgg").load("gg_list.php?menu=ajax&wllx=0", function () {
                init_ajax_gg();
            });
            $("#listggcl").load("ggcl_list.php?menu=ajax&type=0", function () {
                init_ajax_ggcl();
            });
        });
        $("#type1").click(function () {
            $("#listgg").load("gg_list.php?menu=ajax&wllx=1", function () {
                init_ajax_gg();
            });
            $("#listggcl").load("ggcl_list.php?menu=ajax&type=1", function () {
                init_ajax_ggcl();
            });
        });
        if ($("#type0").attr("checked")) {
            $("#type0").click();
        }
        if ($("#type1").attr("checked")) {
            $("#type1").click();
        }
        $("#fromggk").click(function () {
            $("#listgg").toggle();
        });
        $(document).on("click", ".firstPage", function () {
            $("#listgg").load("gg_list.php?menu=ajax&wllx=" + $("input[name='type']:checked").val(), function () {
                init_ajax_gg();
            });
        });
        $(document).on("click", ".prePage", function () {
            $("#listgg").load("gg_list.php?menu=ajax&wllx=" + $("input[name='type']:checked").val() + "&page=" + $(this).val(), function () {
                init_ajax_gg();
            });
        });
        $(document).on("click", ".nexPage", function () {
            $("#listgg").load("gg_list.php?menu=ajax&wllx=" + $("input[name='type']:checked").val() + "&page=" + $(this).val(), function () {
                init_ajax_gg();
            });
        });
        $(document).on("click", ".pages", function () {
            $("#listgg").load("gg_list.php?menu=ajax&wllx=" + $("input[name='type']:checked").val() + "&page=" + $(this).val(), function () {
                init_ajax_gg();
            });
        });
        $(document).on("click", ".selectgg", function () {
            $("#xzgg table tr:eq(1)").remove();
            $(this).parent().parent().clone().appendTo("#xzgg table");
            $("#xzgg table tr:last").children("td").eq(0).replaceWith('<td class="ui-widget-content tc"><button type="button" class="button delselectgg" title="' + $(this).val() + '" value="' + $(this).val() + '">移除</button></td>');
            $(".button").button();
        });
        $(document).on("click", ".delselectgg", function () {
            $(this).parent().parent().remove();
        });
        $(document).tooltip({
            content: function () {
                var element = $(this);
                if (element.is(".gg_preview")) {
                    return element.attr("title");
                } else if (element.is(".gg_source")) {
                    return htmlspecialchars(element.attr("title")).replace(/\n/igm, "<br>").replace(/\s/igm, "&nbsp;");
                } else {
                    return element.attr("title").replace(/</igm, "&lt;").replace(/>/igm, "&gt;").replace(/\n/igm, "<br>");
                }
            }
        });
        $("#reset").click(function () {
            window.location.reload(true);
        });
        $("#submit").click(function () {
            if (checkInput("#ggwTitle", /^.{1,80}$/) && checkInput('#ggwWidth', /^\d{0,5}$/) && checkInput('#ggwHeight', /^\d{0,5}$/)) {
                var id = <?php echo $id ?>, recycle = <?php echo $recycle ?>, copy = '<?php echo $copy ?>', xzggcls = "", bjgg = "", ggcl = "";
                if (id && !recycle && !copy && !confirm('修改即刻生效！您确定要修改吗？'))
                    return false;
                $(".delselectggcl").each(function () {
                    ggcl = $(this).val();
                    if (ggcl && xzggcls.indexOf(ggcl) == -1)
                        xzggcls += ggcl + ". ";
                });
                $(".delselectgg").each(function () {
                    bjgg += $(this).val();
                });
                loading();
                setCoo('lei3', $("#ggwLei").val(), 120);
                setCoo('zu3', $("#ggwZu").val(), 120);
                $.ajax({
                    type: "POST",
                    url: location.href,
                    data: {
                        menu: "submit",
                        id: id,
                        ggwTitle: $("#ggwTitle").val(),
                        ggwType: $("input[name='type']:checked").val(),
                        ggwWidth: $("#ggwWidth").val(),
                        ggwHeight: $("#ggwHeight").val(),
                        ggwLei: $("#ggwLei").val(),
                        ggwZu: $("#ggwZu").val(),
                        xzggcls: xzggcls,
                        bjgg: bjgg
                    },
                    success: function (msg) {
                        loaded();
                        if (msg == "1") {
                            dialog('恭喜你，添加广告位成功！是否继续添加广告位？', 1, location.href);
                        } else if (msg == "2") {
                            dialog('恭喜你，修改广告位成功！是否继续修改广告位？', 1, location.href);
                        } else {
                            dialog(msg);
                        }
                    }
                });
            }
        });
    });
</script>
<div class="fullScreen">
    <h2 class="ptb hd ui-widget-content"><?php echo $title . ($id ? ' ID：' . $id : '') ?></h2>
    <form>
        <div class="plr ptb">
            <h3 class="fl">广告位名称</h3>
            <input class="text large fl ui-widget-content ui-corner-all" id="ggwTitle" value="<?php echo htmlspecialchars($ggwTitle) ?>" placeholder="不超过80个汉字">
            <span class="tip clear">* 不超过80个汉字</span>
        </div>
        <div class="plr ptb">
            <h3 class="fl">广告位类型</h3>
            <p class="buttonset fl">
                <input type="radio" id="type0" value="0" name="type"<?php echo ($ggwType ? '' : ' checked="checked"') ?>><label for="type0">固定占位</label>
                <input type="radio" id="type1" value="1" name="type"<?php echo ($ggwType ? ' checked="checked"' : '') ?>><label for="type1">非固定占位</label>
            </p>
            <span class="tip clear">固定占位：占有网页内容的部分固定位置来展现广告。非固定占位：不是固定占位的广告，如，漂浮、弹窗等。</span>
        </div>
        <div class="plr ptb">
            <h3 class="fl">广告位展示尺寸</h3>
            <input class="text short ui-widget-content ui-corner-all" id="ggwWidth" value="<?php echo $ggwWidth ?>" placeholder="小于6位数"><span>宽度(px)</span> &times;
            <input class="text short ui-widget-content ui-corner-all" id="ggwHeight" value="<?php echo $ggwHeight ?>" placeholder="小于6位数"><span>高度(px)</span>
            <span class="tip clear">&nbsp;</span>
        </div>
        <div class="plr ptb">
            <h3 class="fl">广告位分类</h3>
            <p class="fl">
                <select class="selectmenu long" id="ggwLei"><?php echo $ggwLeiSelectList ?></select>
            </p>
            <span class="tip clear">&nbsp;</span>
        </div>
        <div class="plr ptb">
            <h3 class="fl">广告位分组</h3>
            <p class="fl">
                <select class="selectmenu long" id="ggwZu"><?php echo $ggwZuSelectList ?></select>
            </p>
            <span class="tip clear">&nbsp;</span>
        </div>
        <div class="plr ptb">
            <h3 class="fl">投放广告策略</h3>
            <div class="fl">策略条件投放广告。固定占位策略权重优先顺序10级>9级>...>2级>1级，权重相同按其在广告位排位顺序决定优先。非固定占位策略无视权重！</div>
            <div class="list fl" id="xzggcl">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ui-widget-content" style="border-width:1px 0px 0px 1px;">
                    <tr>
                        <th class="ui-widget-header" scope="col" style="width: 4.8em;">操作</th>
                        <th class="ui-widget-header" scope="col" style="width: 4em;">编号</th>
                        <th class="ui-widget-header" scope="col" style="width: 28em;">广告策略名称（停留查看信息/点击修改）</th>
                        <th class="ui-widget-header" scope="col" style="width: 3em;">权重</th>
                        <th class="ui-widget-header" scope="col" style="width: 3em;">展现</th>
                        <th class="ui-widget-header" scope="col" style="width: 8em;">分类</th>
                        <th class="ui-widget-header" scope="col" style="width: 8em;">分组</th>
                    </tr>
                    <?php
                    if ($ggcl) {
                        $ggcl_array = explode('. ', $ggcl);
                        $size = count($ggcl_array) - 1;
                        $list = '';
                        for ($i = 0; $i < $size; $i++) {
                            if ($ggcl_array[$i]) {
                                list($ggcl_gid, $ggcl_id) = explode(' / ', $ggcl_array[$i]);
                                $query = "SELECT g.id,g.title,g.exp,g.gid,g.type,g.weight,g.gg,g.rotate_cl,g.time_content,g.limit_gg_num,g.visitor_num,g.os_content,g.screen_content,g.browser_content,g.lang_content,g.referer_content,g.url_content,g.local_content,g.ua_content,g.admin_id,g.time,l.name,z.name "
                                        . "FROM ${cfg_dbprefix}ggcl AS g "
                                        . "LEFT JOIN ${cfg_dbprefix}lei AS l ON g.lei_id=l.id "
                                        . "LEFT JOIN ${cfg_dbprefix}zu AS z ON g.zu_id=z.id "
                                        . "WHERE g.id=${ggcl_id};";
                                $row = $nxsql->oneRowVal($query);
                                $tips = '策略说明：' . htmlspecialchars($row[2]) . "\n";
                                $xzggs = preg_replace('|(, )(.+?)(\. )|is', "\n", $row[6]);
                                $tips .= '包含广告：' . "\n" . $xzggs;
                                $tips .= substr_count($xzggs, "\n") > 1 ? '轮换策略：' . getLHcln($row[7]) . "\n" : '';
                                $tips .= $row[8] ? "时间策略：√\n" : '';
                                $tips .= ($row[9] || $row[10]) ? "限数策略：√\n" : '';
                                $tips .= $row[11] ? "操作系统策略：√\n" : '';
                                $tips .= $row[12] ? "分辨率策略：√\n" : '';
                                $tips .= $row[13] ? "浏览器策略：√\n" : '';
                                $tips .= $row[14] ? "语言策略：√\n" : '';
                                $tips .= $row[15] ? "来源策略：√\n" : '';
                                $tips .= $row[16] ? "网址策略：√\n" : '';
                                $tips .= $row[17] ? "地域策略：√\n" : '';
                                $tips .= $row[18] ? "高级策略：√\n" : '';
                                $list .= '<tr>';
                                $list .= '<td class="ui-widget-content tc"><button type="button" class="button delselectggcl" title="' . $row[3] . ' / ' . $row[0] . '" value="' . $row[3] . ' / ' . $row[0] . '">移除</button></td>';
                                $list .= '<td class="ui-widget-content tc">' . $row[0] . '</td>';
                                $list .= '<td class="ui-widget-content plr"><a href="ggcl_edit.php?id=' . $row[0] . '" target="_blank" class="ggcl_exp" title="' . $tips . '">' . htmlspecialchars(cutStr($row[1], 56)) . '</a></td>';
                                $list .= '<td class="ui-widget-content tc">' . ($row[4] ? '' : "$row[5]") . '</td>';
                                $list .= '<td class="ui-widget-content tc">' . ($row[4] ? '非固定' : '固定') . '</td>';
                                $list .= '<td class="ui-widget-content plr"><span title="' . htmlspecialchars($row [21]) . '">' . htmlspecialchars(cutStr($row [21], 14)) . '</span></td>';
                                $list .= '<td class="ui-widget-content plr"><span title="' . htmlspecialchars($row [22]) . '">' . htmlspecialchars(cutStr($row [22], 14)) . '</span></td>';
                                $list .= '</tr>';
                            }
                        }
                        echo $list;
                    }
                    ?> 
                </table>
            </div>
            <span class="tip clear"></span>
        </div>
        <div class="plr ptb">
            <h3 class="fl">&nbsp;</h3>
            <div class="fl">
                <button type="button" class="button" id="fromggclk">从广告策略库中选择</button>
                <div class="list hide" id="listggcl"></div>
            </div>
            <div class="clear"></div>
        </div>
        <div class="plr ptb">
            <h3 class="fl">背景广告</h3>
            <div class="fl">当没有广告策略或广告策略没有达到条件而没有输出广告的时候最终展现的广告！</div>
            <div class="list fl" id="xzgg">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="ui-widget-content" style="border-width:1px 0px 0px 1px;">
                    <tr>
                        <th class="ui-widget-header" scope="col" style="width: 4.8em;">操作</th>
                        <th class="ui-widget-header" scope="col" style="width: 4em;">编号</th>
                        <th class="ui-widget-header" scope="col" style="width: 18em;">广告名称（停留查看代码/点击预览）</th>
                        <th class="ui-widget-header" scope="col" style="width: 3em;">修改</th>
                        <th class="ui-widget-header" scope="col" style="width: 3em;">展现</th>
                        <th class="ui-widget-header" scope="col" style="width: 4em;">宽(px)</th>
                        <th class="ui-widget-header" scope="col" style="width: 4em;">高(px)</th>
                        <th class="ui-widget-header" scope="col" style="width: 8em;">分类</th>
                        <th class="ui-widget-header" scope="col" style="width: 8em;">分组</th>
                    </tr>
                    <?php
                    if ($bjgg) {
                        $list = '';
                        list($gg_gid, $gg_id) = explode(' / ', $bjgg);
                        $query = "SELECT g.id,g.title,g.content,g.gid,g.tj,g.yb,g.type,g.width,g.height,g.admin_id,g.time,l.name,z.name "
                                . "FROM ${cfg_dbprefix}gg AS g "
                                . "LEFT JOIN ${cfg_dbprefix}lei AS l ON g.lei_id=l.id "
                                . "LEFT JOIN ${cfg_dbprefix}zu AS z ON g.zu_id=z.id "
                                . "WHERE g.id=${gg_id};";
                        $row = $nxsql->oneRowVal($query);
                        $list .= '<tr>';
                        $list .= '<td class="ui-widget-content tc"><button type="button" class="button delselectgg" title="' . $bjgg . '" value="' . $bjgg . '">移除</button></td>';
                        $list .= '<td class="ui-widget-content tc">' . $row[0] . '</td>';
                        $list .= '<td class="ui-widget-content plr"><a href="gg_preview.php?id=' . $row[0] . '" target="_blank" class="gg_source" title="' . htmlspecialchars(cutStr(unescape($row[2]), 3000)) . '">' . htmlspecialchars(cutStr(unescape($row[1]), 36)) . '</a></td>';
                        $list .= '<td class="ui-widget-content tc"><a href="gg_edit.php?id=' . $row[0] . '" target="_blank" title="' . htmlspecialchars(cutStr(unescape($row[1]), 255)) . '">修改</a></td>';
                        $list .= '<td class="ui-widget-content tc"><span title="' . htmlspecialchars(cutStr(unescape($row[2]), 10000)) . '" class="gg_preview">' . ($row[6] ? '非固定' : '固定') . '</span></td>';
                        $list .= '<td class="ui-widget-content tc">' . ($row [7] ? $row [7] : '-') . '</td>';
                        $list .= '<td class="ui-widget-content tc">' . ($row [8] ? $row [8] : '-') . '</td>';
                        $list .= '<td class="ui-widget-content plr"><span title="' . htmlspecialchars($row [11]) . '">' . htmlspecialchars(cutStr($row [11], 14)) . '</span></td>';
                        $list .= '<td class="ui-widget-content plr"><span title="' . htmlspecialchars($row [12]) . '">' . htmlspecialchars(cutStr($row [12], 14)) . '</span></td>';
                        $list .= '</tr>';
                        echo $list;
                    }
                    ?> 
                </table>
            </div>
            <span class="tip clear"></span>
        </div>
        <div class="plr ptb">
            <h3 class="fl">&nbsp;</h3>
            <div class="fl">
                <button type="button" class="button" id="fromggk">从广告库中选择</button>
                <div class="list hide" id="listgg"></div>
            </div>
            <div class="clear"></div>
        </div>
        <div class="ptb hd ui-widget-content"></div>
        <div class="plr ptb">
            <h3 class="fl"></h3>
            <button type="button" class="button" id="submit"> 完 成 </button>
            <button type="button" class="button" id="reset"> 重 置 </button>
            <span class="tip clear"></span>
        </div>
    </form>
</div>
<?php
require './mo_foot.php';
