<link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/css/bootstrap-datepicker.standalone.min.css">
<script src="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>
<script src="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
<style type="text/css">
    table {
        table-layout:fixed !important;
    }

    th{
        background: rgb(146, 208, 80);
    }

    td,th{
        max-width: 400px;
        word-wrap: break-word;
    }

    .table-hover > tbody > tr:hover {
        background-color: #c8bd6d;
    }

    input {
        width:60px;
    }

    #roleTable tr:nth-child(5n){
        background:rgb(198, 239, 206);
    } /**隔五行*/

    .fixed-table-toolbar {
        display: none;
    }

    .fileinput-button {
        position: relative;
        top: -5px;
    }

    .fileinput-button input{
        position:absolute;
        right: 0px;
        top: 0px;
        opacity: 0;
        -ms-filter: 'alpha(opacity=0)';
    }
</style>
<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">
                                    <form class="form-inline" role="form" method="POST" action="#operate/advertData/2" name="stanpform">
                                        <span class="gap"></span>
                                        <div class="form-group">
                                            <label>开始时间</label>
                                            <input type="text" class="form-control datetimepicker" value="<?php if($stime) echo $stime;?>" placeholder="开始时间" name="stime" />
                                        </div>
                                        <span class="gap"></span>
                                        <div class="form-group">
                                            <label>结束时间</label>
                                            <input type="text" class="form-control datetimepicker" value="<?php if($etime) echo $etime;?>" placeholder="结束时间" name="etime" />
                                        </div>
                                        <button type="submit" class="btn btn-primary">查询</button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
            
        </div>
        
        <?php
        function formatChipRound($value, $round=2)
        {
            $value = ceil($value);

            if(1 <= abs($valueB = round($value / 1000000000, $round))) {
                return  $valueB . 'B';
            }else if(1 <= abs($valueM=round($value / 1000000, $round))) {
                return $valueM . 'M';
            }else if(1 <= abs($valueK = round($value / 1000), $round)) {
                return $valueK . 'K';
            }else {
                return $value;
            }
        }
        ?>
                                            
		<div class="row" style="padding-left: 15px;">
            <span id="filespanpro" class="btn btn-success fileinput-button">
                <span>导入数据</span>
                <input type="file" id="upfilepro">
            </span>
            <input type="button" style="display: none;margin-bottom: 10px;width: 80px;" class="btn btn-success" value="上传" id="btnpro"/>
            
            <button type="button" style="width: 80px;" class="btn btn-danger fileinput-button" disabled id="commitBtn">提交</button>
        </div>
        <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="btn-group" style="margin-bottom:20px;">
                                    
                                </div>
                                
                                <div class="table-responsive">
                                    <table id="roleTable" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                        <thead>
	                                        <tr>
	                                            <th style="text-align: center;vertical-align: middle;">日期</th>
	                                            <th style="text-align: center;vertical-align: middle;">地区</th>
	                                            <th style="text-align: center;vertical-align: middle;">平台</th>
	                                            <th style="text-align: center;vertical-align: middle;">广告展示</th>
	                                            <th style="text-align: center;vertical-align: middle;">广告收入</th>
	                                            <th style="text-align: center;vertical-align: middle;">操作</th>
	                                        </tr>
                                        	
                                        </thead>
                                        <tbody id="reportBody">
                                        <?php
                                         if(is_array($data)){
                                            foreach($data['sum'] as $k => $val){
                                                ?>
                                                <tr>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $val['date'];?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $val['address'];?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $sidList[$val['sid']]; ?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $val['show']; ?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $val['income']; ?></td>
                                                	<td>
                                                		<button type="button" class="btn btn-success operateOpen" date="<?php echo $val['date'];?>">展开+</button>
                                                		<button type="button" class="btn btn-danger operateShrink" style="display:none;" date="<?php echo $val['date'];?>">收缩-</button>
                                                	</td>
                                                	<!--<td><button type="button" class="btn btn-danger" onclick="deleteTask('<?php echo $val['date'];?>','<?php echo $val['address'];?>','<?php echo $val['sid'];?>')">删除</button></td>-->
                                                </tr>
                                                <?php foreach($data['list'][$val['date']] as $key => $row){ ?>
                                                <tr class="trList<?php echo $val['date'];?>" style="display:none;">
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $row['date'];?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $row['address'];?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $sidList[$row['sid']]; ?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $row['show']; ?></td>
                                                	<td style="text-align: center;white-space: nowrap"><?php echo $row['income']; ?></td>
                                                	<td><button type="button" class="btn btn-danger" onclick="deleteTask('<?php echo $row['date'];?>','<?php echo $row['address'];?>','<?php echo $row['sid'];?>')">删除</button></td>
                                                </tr>
                                                <?php } ?>
                                                
                                                <?php
                                            }
                                         }
                                        ?>
                                        </tbody>
                                    </table>
                                </div>
                                
                                <button type="button"  class="btn btn-primary"  id="addBtn"><i class="fa fa-plus-square"></i>添加</button>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
        
        
    </div>
</div>


<script type="text/javascript" src="<?php echo base_url();?>public/js/highcharts/highcharts.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>public/js/highcharts/modules/exporting.js"></script>
<script src="<?php echo base_url();?>public/js/common.js"></script>
<script type="text/javascript">
    //选择日期
    $('.datetimepicker').datepicker({
        language:'zh-CN',
        format:'yyyy-mm-dd',
        zIndexOffset:906  //控制z-index
    });
    
    $(document).on("click",".operateOpen",function(){
    	var date = $(this).attr("date");
    	$(".trList"+date).show();
    	$(this).parent().find(".operateShrink").show();
    	$(this).hide();
    });
    
    $(document).on("click",".operateShrink",function(){
    	var date = $(this).attr("date");
    	$(".trList"+date).hide();
    	$(this).parent().find(".operateOpen").show();
    	$(this).hide();
    });
    
    $("#filespanpro").on("change", "input[type='file']", function() {
        var filePath = $(this).val();
        //如果仅上传图片 if(filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
        if (filePath) {
            $('#filespanpro').hide();
            $('#btnpro').show();
        } else {
            return false
        }
    });

    $("#btnpro").click(function(){
        var formData = new FormData();
        formData.append("file",document.getElementById("upfilepro").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Operate/advertDataImport", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function(data) { // 成功回调
                data = JSON.parse(data);
                if(data.ret == 0){
                    window.location.reload();
                }else {
                    msgTip = $('#msg-tip');
                    msgTip.messager().warn('上传失败');
                }
            }
        });
    });
    
    $("#submit").click(function(){
        var formData = new FormData(document.getElementById("goodForm"));
        // var file = document.getElementById("imgfile").files[0];
        // if(file == "" || file == undefined){
        //     $('#actionCode').modal('hide');
        //     msgTip = $('#msg-tip');
        //     msgTip.messager().warn('请选择图片');
        //     return false;
        // }
        // formData.append("file",file);
        $.ajax({
            type: "POST",
            url: "operate/addAdvertData",
            data: formData,
            async: true,
            processData: false, //
            contentType: false, //
            success: function(data) {
                data = JSON.parse(data);
                if(data.ret == 0){
                    $('#actionCode').modal('hide');
                    msgTip = $('#msg-tip');
                    msgTip.messager().success('操作成功');
                    setTimeout(function () {
                        window.location.reload();
                    },2000)
                }else {
                    msgTip = $('#msg-tip');
                    msgTip.messager().warn('操作失败');
                }
            }
        });
    });
    
    function deleteTask(id,address,sid) {
        if(confirm('确定要删除吗')==true){
            $.post('/operate/delAdvertData/2',{
                id:id,
                address:address,
                sid:sid
            },function (data) {
            	if(data.ret == 0){
            		window.location.reload();
            	}else{
            		msgTip = $('#msg-tip');
                    msgTip.messager().warn('删除失败');
            	}
            },"json");
        }
    }
    
    $('#roleTable').on('dblclick','td',function(){
        var _this = $(this),//保存td
            tdText = _this.text();
        var  cellIndex =  _this[0].cellIndex;
        if(cellIndex == 0) {return false;} //点击操作列不可编辑
        if(cellIndex != 1 && cellIndex != 2 && cellIndex != 3 && cellIndex != 3){
        	//下拉框
        	return false;
        }
        if(!_this.is('.input')){
            _this.addClass('input').html('<input type="text" value="'+ tdText +'" />').find('input').focus().blur(function(){
                var _input = $(this); //保存input对象
                var thisvalue = $(this).val();
                
                _input.parent().removeClass('input').html(thisvalue || '');
                
                if($('#commitBtn').prop('disabled')){
                    $('#commitBtn').attr('disabled', false);
                }
            });
        }

    });
    
    //新增一行
    $('#addBtn').click(function () {
        var editUserList = '<?php echo json_encode($editUserList) ?>';
        var username = '<?php echo get_cookie('username');?>';
        var msgTip = $('#msg-tip');

        editUserList = JSON.parse(editUserList);
        if($.inArray(username, editUserList) < 0){
            //msgTip.messager().warn('没有编辑权限');
            //return;
        }

        if($('#commitBtn').prop('disabled')){
            $('#commitBtn').attr('disabled', false);
        }
        var html = '<tr>';
        for(var i=0; i<5; i++){
            html += '<td class="input add-row"><input value=""/></td>';
        }

        html += ' </tr>';
        $('#roleTable').append(html);
    });

    //删除一行
    $('#roleTable').on('click','.delete-row',function () {
        if(confirm('是否确认删除该行')){
            if($('#commitBtn').prop('disabled')){
                $('#commitBtn').attr('disabled', false);
            }
            $(this).parent().parent().remove();
        }

    });


    $('#roleTable').on('blur', '.add-row', function () {
        var _that = $(this);
        var thisval = _that.find('input');
        var div = $(thisval[0]).parent();
        if($(thisval[0]).val() == ""){
            div.html(0);
        }else{
            div.html($(thisval[0]).val());
        }

        div.removeClass('input');
        div.parent().removeClass('input');
    });
    
    //提交表格数据
    $('#commitBtn').click(function () {
        var tableContent = $('#roleTable').html(),
            msgTip = $('#msg-tip');
        //如果还有添加未编辑的，则不能提交
        if($('#roleTable td').hasClass('input')){
            msgTip.messager().warn('列表中还有未编辑的');
            return;
        }

        var tr = $('#roleTable tr');
        var arr = [];
        for (var i = 1; i < tr.length; i++) {
            var tempData = {};
            td = $(tr[i]).find('td');
            var status = 1;
            for (var j = 0; j < td.length; j++) {
                switch (j) {
                    case 0:
                        tempData['date'] = $(td[j]).html();
                    break;
                    case 1:
                        tempData['address'] = $(td[j]).html();
                        if(tempData['address'] == '所有'){
                        	status = 0;
                        }
                    break;
                    case 2:
                        tempData['sid'] = $(td[j]).html();
                        if(tempData['sid'] == '所有'){
                        	status = 0;
                        }
                        break;
                    case 3:
                        tempData['show'] = $(td[j]).html();
                        break;
                    case 4:
                        tempData['income'] = $(td[j]).html();
                        break;
                        break;
                }
            }
            if(status){
            	arr.push(tempData)
            }
        }

        $.ajaxSetup({
            async: true
        });


        $.ajax({
            type: 'POST',
            dataType: 'json',
            data:{
                data: JSON.stringify(arr)
            },
            url:'operate/editAdvertData',
            success: function(data){
                if(data.ret == 0){
                    window.location.reload()
                    $('#commitBtn').attr('disabled', true);
                }else{
                    msgTip.messager().error(data.msg);
                }
            }
        });
    });
</script>