<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>
    .ajaxeditspan{color: red;font-size: 6px;}
    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>
					
                    <section style="margin-top: 10px;">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                  <?php foreach($channelList as $id => $name){?>
                                    <a href="<?php echo base_url('#reconciliation/index2/'.$id);?>" role="button" class="btn btn-success <?php if($channeltype == $id){echo 'active';}?>"><?php echo $name;?></a>
                                  <?php } ?>
                                </div>
                            </div>
                        </div>
                    </section>
                    
                </aside>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <form class="form-inline" role="form" method="POST" action="#reconciliation/index2" name="stanpform">
                                    	<input type="hidden" value="1" name="payuserpost"/>
                                        <span class="gap"></span>
                                        <div class="form-group">
                                            <label>导入月份</label>
                                            <input type="text" class="form-control datetimepicker" value="<?php if($month) echo $month;?>" placeholder="导入月份" name="month" />
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
        <div class="row" style="padding-left: 15px;">
            <span id="filespanpro" class="btn btn-success fileinput-button">
                <span>导入GooGle账单</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"/>
            
        </div>
        <div class="row">
            <div class="col-sm-12">
            	
                <aside>
                	<header><i class="fa fa-fw fa-file"></i>月度流水对账</header>
                	
                    <section>
                        <div class="table-responsive">
                          <table class="table table-striped table-bordered table-hover">
                            <thead>
                              <tr>
                              	<th>本地订单数</th>
                                <th>本地金额(美元)</th>
                                <th>汇率<span class="ajaxeditspan">(双击编辑)</span></th>
                                <th>本地金额(港币)</th>
                                <th><?php echo $channelname;?>汇总金额(港币)</th>
                                <th>金额差值</th>
                                <th>差值百分比</th>
                                <th>漏单数量</th>
                                <th>年月</th>
                                <th>对账时间</th>
                                <th>操作</th>
                              </tr>
                            </thead>
                            <tbody>
                              <?php foreach ($list as $val) {?>
                              <tr>
                                <td><? echo $val['data']['local_ordernum']; ?></td>
                                <td><? echo round($val['data']['local_totalmoney'],2); ?></td>
                                <td class="ajaxedit" date="<?php echo $val['date'];?>" field="exchange_rate"><? echo $val['data']['exchange_rate']; ?></td>
                                <td><? echo round($val['data']['local_totalmoney']*$val['data']['exchange_rate'],2); ?></td>
                                <td><? echo round($val['data']['other_totalmoney'],2); ?></td>
                                <td><? echo round($val['data']['other_totalmoney'] - $val['data']['local_totalmoney']*$val['data']['exchange_rate'],2); ?></td>
                                <td><? echo number_format(($val['data']['other_totalmoney'] - $val['data']['local_totalmoney']*$val['data']['exchange_rate']) / $val['data']['other_totalmoney'] * 100, 2, '.', ''); ?>%</td>
                                <td><? echo $val['data']['local_ordernum'] - $val['data']['other_ordernum']; ?></td>
                                <td><? echo date("Y-m",strtotime($val['date'])); ?></td>
                                <td><? echo date("Y-m-d H:i",$val['data']['add_time']); ?></td>
                                <td>
                                   <input type="button" date="<?php echo $val['date'];?>" class="btn btn-primary btn-sm commit-btn" field="status" <?php if($val['data']['status']){echo 'disabled';}?>  value="<?php if($val['data']['status']){echo '已确认';}else{echo '是否确认';}?>"/>
                              </tr>
                              <?php } ?>
                            </tbody>
                          </table> 
                        </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>
    //选择日期
    $('.datetimepicker').datepicker({
        language:'zh-CN',
        format:'yyyy-mm',
        zIndexOffset:906  //控制z-index
    });

    //ajax编辑
    $('.ajaxedit').dblclick(function(){
        var date = $(this).attr('date');
        var field = $(this).attr('field');

        var td = $(this);
        var tbVal = td.text();
        var txt = $("<input type='text'>").val(tbVal);
        txt.blur(function(){
            var newText = $(this).val();
            $.post(
                "<?php echo base_url('reconciliation/ajaxEditIncome');?>",
                {date:date, field:field, val:newText}
            );
            // 移除文本框,显示新值
            $(this).remove();
            td.text(newText);

            //输入第三方后刷新
            if(field == 'other_income' || field == 'other_fee' || field == 'exchange_rate'){
                location.reload();
            }
        });
        td.text("");
        td.append(txt);

    });

    //是否确认或者收款
    $('.commit-btn').click(function () {
        var date = $(this).attr('date');
        var field = $(this).attr('field'),
            msg;
        if(field == 'confirm_btn') {
            msg = '确认';
        }else{
            msg = '收款';
        }
        if( confirm('是否进行'+ msg + '操作?')){
            console.log('aaaaa');
            $.post(
                "<?php echo base_url('reconciliation/ajaxEditIncome');?>",
                {date:date, field:field, val:1}
            );
            location.reload();
        }

    });

</script>
<script>
	$("#btnpro").click(function(){
    	var obj = $(this);
    	obj.val("上传中...");
    	
    	var month = $("input[name='month']").val();
    	if(month == ""){
    		msgTip = $('#msg-tip');
            msgTip.messager().warn('请输入月份再上传');
            return;
    	}
    	
        var formData = new FormData();
        formData.append("file",document.getElementById("upfilepro").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Reconciliation/import2/"+month, // 发送地址
            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('上传失败');
                    obj.val("上传失败");
                }
            },
            error:function(){
            	msgTip = $('#msg-tip');
                msgTip.messager().warn('上传失败');
                obj.val("上传失败");
                
            }
        });
    });

    $("#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
        }
    });

    
</script>