<link rel="stylesheet" href="<?php echo base_url();?>public/css/vendor/bootstrap/bootstrap-table.css">
<link rel="stylesheet" href="<?php echo base_url();?>public/css/vendor/bootstrap/table-fix-column.css">

<script src="<?php echo base_url();?>public/js/vendor/bootstrap/src/bootstrap-table.js"></script>
<script src="<?php echo base_url();?>public/js/vendor/bootstrap/src/table-fix-column.js"></script>

<div class="content_wrapper">
    <div id="msg-tip"></div>
    <div class="container-fluid ">
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>分享配置</header>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <a style="margin:5px;" class="btn btn-primary " href="#config/shareConfig" role="button">分享配置</a>
                                    <a style="margin:5px;" class="btn btn-primary " href="#config/shareConfig/img" role="button">分享图配置</a>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>配图国家</header>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-2">
                                    <select class="form-control" id="lang" onchange="changeLang(this.value)">
                                        <option value="DEFAULT" >默认</option>
                                        <?php foreach ($langList as $langKey=>$row){ ?>
                                            <option value="<?php echo $langKey;?>" <?php if($country == $langKey) echo 'selected';?>><?php echo $row;?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-sm-2"><a target="_blank" href="<?php echo $href; ?>" type="button" style="margin-bottom: 10px;width: 80px;" class="btn btn-success" >上传图片</a></div>
                            </div>
                        </div>
                    </section>
                </aside>
                <aside>
                    <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="goodForm" action="#" style="margin-top: 20px">
                        <section>
                            <div class="container-fluid">
                                <button type="button" style="margin-bottom: 10px;width: 80px;" class="btn btn-danger" id="commitBtn">提交配置</button>
                                <input type="file" style="display: none" id="importFiles">
                                <button type="button" style="margin-bottom: 10px;" class="btn btn-primary importBtn"  data-href="/import/trackers/<?=$country?>">导入跟踪链</button>
                                <button type="button" style="margin-bottom: 10px;" class="btn btn-success exportBtn"  data-href="/export/trackers/<?=$country?>">导出跟踪链</button>
                                <div class="row">
                                    <div class="col-sm-12">
                                        <div class="table-responsive">
                                            <table id="roleTable" class="table table-bordered">
                                                <thead>
                                                <tr>
                                                    <th style="width:10%">分享主键</th>
                                                    <th>默认图片标识</th>
                                                    <th>默认图片</th>
                                                    <th>默认跟踪标识</th>
                                                    <th>link-A图片标识</th>
                                                    <th>link-A图片</th>
                                                    <th>link-A跟踪标识</th>
                                                    <th>link-B图片标识</th>
                                                    <th>link-B图片</th>
                                                    <th>link-B跟踪标识</th>
                                                    <th>状态（A/B）</th>
                                                    <th style="width:6%">使用生效图</th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <?php
                                                if(is_array($langData)  && !empty($langData)){
                                                    foreach ($langData as $key => $vl) { ?>
                                                        <tr>
                                                            <td><input type="text" class="form-control" readonly value="<?php echo $key;?>"></td>
                                                            <td><input type="text" class="form-control" readonly value="<?php echo $country."-".$key;?>"></td>
                                                            <td><a target="_blank" href="<?php echo $server."/img/feed/" . $country."/" . $country."-".$key.".jpg?".time(); ?>"><img src="<?php echo $server."/img/feed/" . $country."/" . $country."-".$key.".jpg?".time(); ?>" height="60px" ></td></a>
                                                            <td><input  type="text" class="form-control track-default" name="<?php echo $key;?>" value="<?php echo $track[$key]['d'];?>"></td>
                                                            <td><input type="text" class="form-control " readonly value="<?php echo $country."-".$key."-A";?>"></td>
                                                            <td><a target="_blank" href="<?php echo $server."/img/feed/" . $country."/" . $country."-".$key."-A.jpg?".time(); ?>"><img class="apath" data-exists="<?=file_exists(__ROOT__."/img/feed/" . $country."/" . $country."-".$key."-A.jpg")?1:0?>" src="<?php echo $server."/img/feed/" . $country."/" . $country."-".$key."-A.jpg?".time(); ?>" height="60px" ></td></a>
                                                            <td><input  type="text" class="form-control track-a" name="<?php echo $key;?>" value="<?php echo $track[$key]['a'];?>"></td>
                                                            <td><input type="text" class="form-control" readonly value="<?php echo $country."-".$key."-B";?>"></td>
                                                            <td><a target="_blank" href="<?php echo $server."/img/feed/" . $country."/" . $country ."-".$key."-B.jpg?".time(); ?>"><img class="bpath" data-exists="<?=file_exists(__ROOT__."/img/feed/" . $country."/" . $country ."-".$key."-B.jpg")?1:0?>" src="<?php echo $server."/img/feed/" . $country."/" . $country ."-".$key."-B.jpg?".time(); ?>" height="60px" ></td></a>
                                                            <td><input  type="text" class="form-control track-b" name="<?php echo $key;?>" value="<?php echo $track[$key]['b'];?>"></td>
                                                            <td>
                                                                <input class='mySwitch abTest' type="checkbox" name="<?php echo $key;?>" value="<?php echo $vl;?>" <?php if($vl == 1){ echo 'checked';}?>>
                                                            </td>
                                                            <td>
                                                                <select class="form-control activeImg" name="<?=$key?>">
                                                                    <option <?=($vl == 1)?'style="display:none;"':''?> <?=file_exists(__ROOT__."/img/feed/" . $country."/" . $country."-".$key.".jpg")?'':'disabled="disabled"'?> value="0" <?=empty($actImg[$key])?'selected="selected"':''?>>不使用A、B图</option>
                                                                    <option <?=($vl == 1)?'style="display:none;"':''?> <?=file_exists(__ROOT__."/img/feed/" . $country."/" . $country."-".$key."-A.jpg")?'':'disabled="disabled"'?> value="1" <?=(!empty($actImg[$key])&&(int)$actImg[$key]==1)?'selected="selected"':''?>>Link-A图</option>
                                                                    <option <?=($vl == 1)?'style="display:none;"':''?> <?=file_exists(__ROOT__."/img/feed/" . $country."/" . $country ."-".$key."-B.jpg")?'':'disabled="disabled"'?> value="2" <?=(!empty($actImg[$key])&&(int)$actImg[$key]==2)?'selected="selected"':''?>>Link-B图</option>
                                                                    <option value="" <?=($vl == 1)?'selected':'style="display:none;"'?>>A/BTest中</option>
                                                                </select>
                                                            </td>
                                                        </tr>
                                                    <?php } } else{?>
                                                    <tr>
                                                        <td colspan="14">暂无数据</td>
                                                    </tr>
                                                <?php } ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </section>
                    </form>
                </aside>
            </div>
        </div>
    </div>
</div>


<script>
    //提交表格数据
    $('#commitBtn').click(function () {
        var data = {};
        var d = {};
        var a = {};
        var b = {};
        var actImg = {};
        $('.abTest').each(function(){
            var name  = $(this).attr("name")
            var value = $(this).val()
            data[name]= value
        });
        $('.track-default').each(function(){
            var name  = $(this).attr("name")
            var value = $(this).val()
            d[name]= value
        });
        $('.track-a').each(function(){
            var name  = $(this).attr("name")
            var value = $(this).val()
            a[name]= value
        });
        $('.track-b').each(function(){
            var name  = $(this).attr("name")
            var value = $(this).val()
            b[name]= value
        });
        $('.activeImg').each(function(){
            var name  = $(this).attr("name")
            var value = $(this).val()
            actImg[name]= value
        });
        $.post(rootPath + 'config/actShareConfig/img',{
            country : '<?php echo $country;?>',
            data : data,
            d : d,
            a : a,
            b : b,
            actImg : actImg,
        },function (ret) {
            ret = JSON.parse(ret);
            alert(ret.msg);
            // window.location.reload();
        });
    });

    $(".mySwitch").bootstrapSwitch({
        onSwitchChange:function(event, state){
            var aExists = $(this).parents('tr').find('.apath').attr('data-exists');
            var bExists = $(this).parents('tr').find('.bpath').attr('data-exists');
            if($(this).val() == 0 && (aExists == "0" || bExists == "0")){
                if(!$(this).bootstrapSwitch("state")){
                    return;
                }
                alert("请配齐A/Btest所需图片！");
                $(this).bootstrapSwitch("state",false);
                $(this).bootstrapSwitch("disabled",true);
                return;
            }
            if(state){
                var state = 1;
            }else{
                var state = 0;
            }

            $(this).parents('tr').find('.activeImg option:selected').removeAttr("selected");
            if(state){
                $(this).parents('tr').find('.activeImg option:last').show();
                $(this).parents('tr').find('.activeImg option:last').siblings().hide();
                $(this).parents('tr').find('.activeImg option:last').attr("selected", true);
            } else {
                $(this).parents('tr').find('.activeImg option:last').hide();
                $(this).parents('tr').find('.activeImg option:last').siblings().show();
                $(this).parents('tr').find('.activeImg option:first').attr("selected", true);
            }

            $(this).val(state);
        }
    });

    function changeLang(value) {
        window.location.href = "/#config/shareConfig/img/"+value;
    }


    $('.importBtn').click(function(){
        $('#importFiles').data('href', $(this).data('href'))
        if('<?=$country?>' == 'DEFAULT' ){
            if(confirm('导入默认配置会覆盖其他全部语言配置！确定导入吗？')){
                $('#importFiles').click()
            }
        }
        else {
            $('#importFiles').click()
        }
    })

    $('.exportBtn').click(function(){
        window.open($(this).data('href'))
    })
    $('#importFiles').change(function(){
        var url = $(this).data('href');
        console.log(url)
        var formData = new FormData();
        formData.append("file",document.getElementById("importFiles").files[0]);
        $(this).val('');
        showLoading()
        $.ajax({
            type: "POST", // 数据提交类型
            url: url, // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function(data) { // 成功回调
                hideLoading()
                data = JSON.parse(data);
                alert(data.msg);
                if(data.ret > 0){
                    window.location.reload();
                }
            }
            ,error: function(xhr,err) { // 成功回调
                hideLoading()
                console.log(xhr,err)
                alert('上传失败');
            }
        });
    })
</script>

