<extend name="Public/base"/>

<block name="body">
    <link rel="stylesheet" href="__CSS__/select2.min.css" type="text/css" />
    <link rel="stylesheet" href="__CSS__/pro_promote.css" type="text/css" />
    <script src="__STATIC__/jquery.form.js"></script>
    <script src="__STATIC__/layer/layer.js"></script>
    <script type="text/javascript" src="__JS__/bootstrap.min.js"></script>
    <script type="text/javascript" src="__JS__/select2.min.js"></script>
    <script type="text/javascript" src="__STATIC__/layer3/layer.js"></script>

    <style>
        .select2-container--default .select2-selection--single {
            color: #000;
            resize: none;
            border-width: 1px;
            border-style: solid;
            border-color: #a7b5bc #ced9df #ced9df #a7b5bc;
            box-shadow: 0px 3px 3px #F7F8F9 inset;height:35px;
            height:28px;border-radius:3px;font-size:12px;
        }
        .select2-container--default .select2-selection--single .select2-selection__rendered {
            line-height:35px;
            line-height:28px;
        }
        .select2-container--default .select2-selection--single .select2-selection__arrow {
            height:26px;
        }
        .select2-container--default .select2-search--dropdown .select2-search__field {
            height:26px;line-height:26px;font-size:12px;
        }
        .select2-results__option[aria-selected] {font-size:12px;}
        .layui-layer-dialog .layui-layer-content{color:red}
        .butnbox {padding:10px 0 10px;}
        .butnbox .butnlist {overflow:hidden;clear:both;}
        .butnbox .butnlist .butn,.butnbox .butnlist .butn:hover {text-decoration:none;border:none;}
        .butnbox .butnlist .butn {display:inline-block;width:120px;height:28px;line-height:28px;text-align:center;color:#FFF;background:#3C95C8;border-radius:3px;}
        .butnbox .butnlist .butn.last {background:#009900;}
        .butnbox .butnlist .butn~.butn {margin-left:20px;}
        .data_list table tbody tr a.disabled,.data_list table tbody tr a.disabled:hover {color:#999;cursor:default;}
        .layui-layer-title {
            text-align: center;
            height: 80px;
            line-height: 80px;
            font-weight: 600;
            font-size: 18px;
        }
    </style>

    <div class="cf main-place top_nav_list navtab_list">
        <h3 class="page_title">聚合渠道结算单管理</h3>
       <p class="description_text">流程说明:结算自动生成对账单--->发起人申请开票--->财务审批开票--->寄发票--->发起人上传收款凭证--->财务确认到账</p>
    </div>
    <div class="cf top_nav_list" style="height: 38px;">
        <!-- 高级搜索 -->
        <div class="jssearch fl cf search_list" style="margin-bottom: 0">
            <div class="input-list input-list-promote search_label_rehab">
                <select id="channel_id" name="channel_id" class="select_gallery" >
                    <option company-type="" value="">请选择或输入合作公司</option>
                    <volist name="channel" id="vo">
                        <option  value="{$vo.channel_id}" <?php echo $vo.channel_id == I('channel_id') ? 'selected':''; ?>  >{$vo.channel_name}</option>
                    </volist>
                </select>
            </div>

            <div class="input-list input-list-promote search_label_rehab">
                <select id="withdraw_type" name="withdraw_type" class="select_gallery" style="width:150px;">
                    <option value="">结算周期</option>
                    <option value="0">周结</option>
                    <option value="1">月结</option>
                </select>
            </div>
            <div class="input-list input-list-promote search_label_rehab">
                <select id="withdraw_type" name="withdraw_type2" class="select_gallery" style="width:150px;">
                    <option value="">结算类型</option>
                    <option value="1">正常计算</option>
                    <option value="2">补点</option>
                </select>
            </div>

            <div class="input-list">
                <input type="text" readonly id="time_start" name="time_start" class="" value="{:I('time_start')}" placeholder="结算开始时间" />
                &nbsp;-&nbsp;
                <div class="input-append date" id="datetimepicker"  style="display:inline-block">
                    <input type="text" readonly id="time_end" name="time_end" class="" value="{:I('time_end')}" placeholder="结算结束时间" />
                    <span class="add-on"><i class="icon-th"></i></span>
                </div>
            </div>
            
            <div class="input-list input-list-promote search_label_rehab">
                <select id="verify_status" name="verify_status" class="select_gallery" >
                    <option value="">请选择审批状态</option>
                    <foreach name="verify_status" item="vo" >
                        <option  value="{$key}" <?php echo (isset($_REQUEST['verify_status']) && $key == I('verify_status')) ? 'selected':''; ?>  >{$vo}</option>
                    </foreach>
                </select>
            </div>

            <div class="input-list">
                <input type="text" readonly id="time_start2" name="time_start2" class="" value="{:I('time_start2')}" placeholder="操作开始时间" />
                &nbsp;-&nbsp;
                <div class="input-append date" id="datetimepicker2"  style="display:inline-block">
                    <input type="text" readonly id="time_end2" name="time_end2" class="" value="{:I('time_end2')}" placeholder="操作结束时间" />
                    <span class="add-on"><i class="icon-th"></i></span>
                </div>
            </div>
            <div class="input-list">
                <a class="sch-btn" href="javascript:;" id="search" url="{:U('AggregateFinanceStatement/lists','model='.$model['name'] .'&row='.I('row'),false)}">搜索</a>
            </div>

        </div>
    </div>

    <div class="butnbox" >
        <div class="butnlist jscheckbutn" style="margin-left: 2px">
            <foreach name="menubtn" item="vo" >
                {$vo}
             </foreach>
        </div>
    </div>

    <!-- 数据列表 -->
    <div class="data_list">
            <div class="">
                <table>
                    <!-- 表头 -->
                    <thead>
                    <tr>
                        <th><input class="check-all" type="checkbox"></th>
                        
                        <th>合作公司</th>
                        <th>结算日期</th>
                        <th>结算周期</th>
                        <th>结算类型</th>
                        <th>结算流水</th>
                        <th>结算金额</th>
                        <th>审批状态</th>
                        <th>操作人</th>
                        <th>操作时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>

                    <!-- 列表 -->
                    <tbody>
                    <if condition = "empty($data)">
                        <tr>
                            <td colspan="16" class="text-center">aOh! 暂时还没有内容!</td>
                        </tr>
                    </if>
                    <notemtpy name = "data">
                        <volist name="data" id="data">
                            <tr>
                                <td ><input class="ids" type="checkbox" data-status="{$data.verify_status}" value="{$data['id']}" name="ids[]" >
                                </td>
                                <td>{$data.channel_name}</td>
                                <td>{$data.begintime}-{$data.endtime}</td>
                                <td>{$data.withdraw_type_1}</td>
                                <td>{$data.withdraw_type_2}</td>
                                <td>{$data.pay_money}</td>
                                <td>{$data.ratio_money}</td>
                                <td>{$data.verify_status_str}</td>
                                <td>{$data.admin_name}</td>
                                <td>{$data.create_time}</td>
                                <td>
                                    <foreach name="data.oplist" item="vo" >
                                       {$vo}
                                    </foreach>
                                </td>
                                
                            </tr>
                        </volist>
                        <tr><td>合计</td><td colspan="10">结算总流水:{$pay_money}元(补点单据不重复计算在内)&#12288;&#12288;&#12288;&#12288;结算总金额:{$total}元 </td></tr>
                    </notemtpy>
                    </tbody>
                </table>
            </div>

    </div>
    <div class="page">
        {$_page|default=''}
    </div>
</block>

<block name="script">
    <link href="__STATIC__/datetimepicker/css/datetimepicker.css" rel="stylesheet" type="text/css">
    <php>if(C('COLOR_STYLE')=='blue_color') echo '
        <link href="__STATIC__/datetimepicker/css/datetimepicker_blue.css" rel="stylesheet" type="text/css">
        ';
    </php>
    <link href="__STATIC__/datetimepicker/css/dropdown.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="__STATIC__/datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
    <script type="text/javascript" src="__STATIC__/datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"
            charset="UTF-8"></script>
    <script src="__STATIC__/layer/layer.js" type="text/javascript"></script>
    <script src="__STATIC__/layer/extend/layer.ext.js"></script>
    <script type="text/javascript">
    </script>
    <script>
        <volist name=":I('get.')" id="vo">
            Think.setValue('{$key}',"{$vo}");
        </volist>
        $(".select_gallery").select2();
    </script>
    <script type="text/javascript">
        //导航高亮
        highlight_subnav("{:U('AggregateFinanceStatement/lists')}");
        function reload() {
            window.location.reload(); 
        }

        $(function(){
            $("#export").click(function () {
                var text = $("input:checkbox[name='ids[]']:checked").map(function(index,elem) {
                    return $(elem).val();
                }).get();

                if(text.length > 20){
                    layer.msg("<font style='color:white'>" + '暂不支持超过20条的批量导出' + "</font>");
                    return;
                }
                if(text.length < 1){
                    layer.msg("<font style='color:white'>" + '请先选择要导出的结算单' + "</font>");
                    return;
                }
                for (var index = 0; index < text.length; index++) {
                    var id = text[index];
                    var url ="{:U('export')}"+"&id="+id;
                    var iframeExcel = "<iframe src='"+url+"' width='0px' height='0px' style='z-index:-1;position: absolute;top: -999999px;'></iframe>"//添加下载的地址到iframe,这里是公司信息,我用**表示了。
                    $("body").append(iframeExcel)
                }
            })
            //审批通过
            $("#agreeApply").on("click",function(){
                var flag = false;
                var text = $("input:checkbox[name='ids[]']:checked").map(function(index,elem) {
                    if($(elem).data("status") != 1){
                        flag = true;
                    }else{
                        return $(elem).val();
                    }
                }).get().join(",");
                if(text == ''){
                    layer.msg("<font style='color:white'>" + '无需要审批通过的结算单,仅[已申请发票]状态可以进行审批通过' + "</font>");
                    return;
                }
                if(flag){
                    layer.confirm("审批成功仅会对 [已申请发票] 结算单处理,选择中包含其他的状态的结算单将被忽略,点击取消停止操作",{title:false}, function(index){    
                        _doAgreeApply();                
                        layer.close(index);
                    });  
                }else{
                    _doAgreeApply();
                }
                function _doAgreeApply(){
                    //执行
                    $.ajax({
                        type: "POST",
                        url: "{:U('agreeApply')}",
                        dataType: 'json',
                        async: false,
                        data: {ids:text},
                        success:function(data){
                            if(data.status==1){
                                layer.msg("<font style='color:white'>" + data.info + "</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },1500);
                            }
                        }
                    });
                }
            })
             //审批拒绝
            $("#refuseApply").on("click",function(){
                var flag = false;
                var text = $("input:checkbox[name='ids[]']:checked").map(function(index,elem) {
                    if($(elem).data("status") != 1){
                        flag = true;
                    }else{
                        return $(elem).val();
                    }
                }).get().join(",");
                if(text == ''){
                    layer.msg("<font style='color:white'>" + '无需要批拒绝的结算单,仅[已申请发票]状态可以进行审批拒绝' + "</font>");
                    return;
                }
                if(flag){
                    layer.confirm("审批拒绝仅会对 [已申请发票] 结算单处理,选择中包含其他的状态的结算单将被忽略,点击取消停止操作",{title:false}, function(index){    
                        _doRefuseApply();                
                        layer.close(index);
                    });  
                }else{
                    _doRefuseApply();
                }
                function _doRefuseApply(){
                    //执行
                    $.ajax({
                        type: "POST",
                        url: "{:U('refuseApply')}",
                        dataType: 'json',
                        async: false,
                        data: {ids:text},
                        success:function(data){
                            if(data.status==1){
                                layer.msg("<font style='color:white'>" + data.info + "</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },1500);
                            }
                        }
                    });
                }
            })
            //已开票
            $("#Invoiced").on("click",function(){
                var flag = false;
                var text = $("input:checkbox[name='ids[]']:checked").map(function(index,elem) {
                    if($(elem).data("status") != 2){
                        flag = true;
                    }else{
                        return $(elem).val();
                    }
                }).get().join(",");
                if(text == ''){
                    layer.msg("<font style='color:white'>" + '无需要进行已开票的结算单,仅[审批同意]状态可以进行已开票' + "</font>");
                    return;
                }
                if(flag){
                    layer.confirm("已开票仅会对 [审批同意] 结算单处理,选择中包含其他的状态的结算单将被忽略,点击取消停止操作",{title:false}, function(index){    
                        _doInvoiced();                
                        layer.close(index);
                    });  
                }else{
                    _doInvoiced();
                }
                function _doInvoiced(){
                    //执行
                    $.ajax({
                        type: "POST",
                        url: "{:U('Invoiced')}",
                        dataType: 'json',
                        async: false,
                        data: {ids:text},
                        success:function(data){
                            if(data.status==1){
                                layer.msg("<font style='color:white'>" + data.info + "</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },1500);
                            }
                        }
                    });
                }
            })
            //已到账
            $("#Received").on("click",function(){
                var flag = false;
                var text = $("input:checkbox[name='ids[]']:checked").map(function(index,elem) {
                    if($(elem).data("status") == 3 || $(elem).data("status") == 4){
                        return $(elem).val();
                    }else{
                        flag = true;
                    }
                }).get().join(",");
                if(text == ''){
                    layer.msg("<font style='color:white'>" + '无需要进行已到账的结算单,仅[已开票][已上传凭证]状态可以进行已到账' + "</font>");
                    return;
                }
                if(flag){
                    layer.confirm("已到账仅会对 [已开票]或[已上传凭证] 结算单处理,选择中包含其他的状态的结算单将被忽略,点击取消停止操作",{title:false}, function(index){    
                        _doReceived();                
                        layer.close(index);
                    });  
                }else{
                    _doReceived();
                }
                function _doReceived(){
                    //执行
                    $.ajax({
                        type: "POST",
                        url: "{:U('Received')}",
                        dataType: 'json',
                        async: false,
                        data: {ids:text},
                        success:function(data){
                            if(data.status==1){
                                layer.msg("<font style='color:white'>" + data.info + "</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },1500);
                            }
                        }
                    });
                }
            })
            //重算金额
            $("#updateStatement").on("click",function(){
                var text = $("input:checkbox[name='ids[]']:checked").map(function(index,elem) {
                    if($(elem).data("status") == 0 || $(elem).data("status") == 1 || $(elem).data("status") == -1){
                        return $(elem).val();
                    }else{
                        flag = true;
                    }
                    // return $(elem).val();
                }).get().join(",");
                if(text == ''){
                    layer.msg("<font style='color:white'>" + '无需要进行重新结算的结算单,仅[未申请发票][申请开票][审批拒绝]状态可以进行重新结算' + "</font>");
                    return;
                }
                layer.confirm("重算结算金额会对结算单回退到未申请发票状态,请慎重处理,点击取消停止操作",{title:false}, function(index){    
                    _doReceived();                
                    layer.close(index);
                });  
                function _doReceived(){
                    //执行
                    $.ajax({
                        type: "POST",
                        url: "{:U('updateStatement')}",
                        dataType: 'json',
                        async: false,
                        data: {ids:text},
                        success:function(data){
                            if(data.status==1){
                                layer.msg("<font style='color:white'>" + data.info + "</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },1500);
                            }
                        }
                    });
                }
            })
            //查看开票信息
            $(".viewStatement").click(function () {
                var id = $(this).data("id");
                layer.open({
                    type: 2,
                    title: "查看开票信息",
                    shadeClose: true,
                    shade: 0.8,
                    area: ['70%', '80%'],
                    content:'/admin.php?s=/AggregateFinanceStatement/viewStatement/id/'+id
                });
            });
            //申请开票
            $(".createStatement").click(function () {
                var id = $(this).data("id");
                layer.open({
                    type: 2,
                    title: "申请开票",
                    shadeClose: false,
                    shade: 0.8,
                    area: ['70%', '80%'],
                    content:'/admin.php?s=/AggregateFinanceStatement/createStatement/id/'+id
                });
            });
            //开票编辑
            $(".editStatement").click(function () {
                var id = $(this).data("id");
                layer.open({
                    type: 2,
                    title: "开票编辑",
                    shadeClose: false,
                    shade: 0.8,
                    area: ['70%', '80%'],
                    content:'/admin.php?s=/AggregateFinanceStatement/editStatement/id/'+id
                });
            });
            //上传凭证
            $(".uploadVoucher").click(function () {
                var id = $(this).data("id");
                layer.open({
                    type: 2,
                    title: "上传凭证",
                    shadeClose: true,
                    shade: 0.8,
                    area: ['60%', '60%'],
                    content:'/admin.php?s=/AggregateFinanceStatement/uploadVoucher/id/'+id,
                    end:function(){
                        reload();
                    }
                });
            });
            //查看凭证
            $(".viewVoucher").click(function () {
                var img = $(this).data("img");
                layer.open({
                    type: 1,
                    title:false,
                    content:"<img src='"+img+"' style='width: auto;height: 450px;margin: auto;'>"
                });
            });
            //编辑凭证
            $(".editVoucher").click(function () {
                var id = $(this).data("id");
                layer.open({
                    type: 2,
                    title: "编辑凭证",
                    shadeClose: true,
                    shade: 0.8,
                    area: ['60%', '60%'],
                    content:'/admin.php?s=/AggregateFinanceStatement/editVoucher/id/'+id,
                    end:function(){
                        reload();
                    }
                });
            });
            //新增批注
            $(".addRemark").click(function(){
                var id = $(this).data("id");
                layer.prompt({
                    formType: 2,
                    value: '',
                    title: '输入备注',
                    area: ['360px', '300px'] //自定义文本域宽高
                    }, function(value, index, elem){
                        if(value != ''){
                            $.ajax({
                                type: "POST",
                                url: "{:U('saveRemark')}",
                                dataType: 'json',
                                async: false,
                                data: {id:id,remark:value},
                                success:function(data){
                                    if(data.status==1){
                                        layer.msg("<font style='color:white'>" + data.info + "</font>");
                                        setTimeout(function(){
                                            window.location.reload();
                                        },1500);
                                    }
                                }
                            });
                        }else{
                            layer.closeAll();
                        }
                    });
            })
            //编辑批注
            $(".editRemark").click(function(){
                var id = $(this).data("id");
                var remark = $(this).data("remark");
                layer.prompt({
                    formType: 2,
                    title: '修改备注',
                    value:remark,
                    area: ['360px', '300px'],
                    btnAlign: 'c',
                    id: 'prompt',
                    yes: function (index, layero) {
                        // 获取文本框输入的值
                        var value = layero.find(".layui-layer-input").val();
                        $.ajax({
                            type: "POST",
                            url: "{:U('saveRemark')}",
                            dataType: 'json',
                            async: false,
                            data: {id:id,remark:value},
                            success:function(data){
                                if(data.status==1){
                                    layer.msg("<font style='color:white'>" + data.info + "</font>");
                                    setTimeout(function(){
                                        window.location.reload();
                                    },1500);
                                }
                            }
                        });
                    }
                });
            })
            //查看批注
            $(".viewRemark").click(function () {
                var remark = $(this).data("remark");
                layer.open({
                    type: 1,
                    title:false,
                    content:"<div style='width: 300;height:auto;margin: auto;padding:20px;'>"+remark+"</div>"
                });
            });
            //撤销审批
            $(".cancelVerify").click(function(){
                var id = $(this).data("id");
                layer.confirm("撤销审批将回退到[已申请开票]状态,点击取消停止操作",{title:false}, function(index){    
                    $.ajax({
                        type: "POST",
                        url: "{:U('cancelVerify')}",
                        dataType: 'json',
                        async: false,
                        data: {id:id},
                        success:function(data){
                            if(data.status==1){
                                layer.msg("<font style='color:white'>" + data.info + "</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },1500);
                            }
                        }
                    });             
                    // layer.close(index);
                }); 
            })
            //撤销申请
            $(".cancelApply").click(function(){
                var id = $(this).data("id");
                layer.confirm("撤销申请将回退到[未申请开票]状态,点击取消停止操作",{title:false}, function(index){    
                    $.ajax({
                        type: "POST",
                        url: "{:U('cancelApply')}",
                        dataType: 'json',
                        async: false,
                        data: {id:id},
                        success:function(data){
                            if(data.status==1){
                                layer.msg("<font style='color:white'>" + data.info + "</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },1500);
                            }
                        }
                    });             
                    // layer.close(index);
                }); 
            })
            

            $(".statement_del").click(function () {
                var del = $(this).data();
                layer.confirm('【警告】撤销后将不可回退,如需继续请点击确认', {
                    btn: ['确认','取消'],
                    title:false
                }, function(){
                    if(del.type == 0){
                        var url = "/admin.php?s=/Statement/delCpStatement/id/"+del.id;
                    }else{
                        var url = "/admin.php?s=/StatementMangement/delStatementData/id/"+del.id;
                    }
                    layer.load(2);
                    $.ajax({
                        url: url,
                        type: "get",
                        dataType: "json",
                        success: function (data) {
                            if(data.code == 0){
                                layer.closeAll('loading');
                                layer.msg("<font style='color:#fff'>删除成功</font>");
                                setTimeout(function(){
                                    window.location.reload();
                                },350);
                            }else{
                                alert("删除失败,请联系管理员")
                            }
                        }
                    })

                }, function(){
                    
                });

                

            });

            $("#search").click(function(){

                var start = $("#time_start").val();
                var end =   $("#time_end").val();
                if(start !='' && end != ''){
                    if (Date.parse(start) > Date.parse(end)){
                        layer.msg('开始时间必须小于等于结束时间');
                        return false;
                    }
                }

                var url = $(this).attr('url');
                var query  = $('.jssearch').find('input').serialize();
                query  += "&"+$('.jssearch').find('select').serialize();
                query = query.replace(/(&|^)(\w*?\d*?\-*?_*?)*?=?((?=&)|(?=$))/g,'');
                query = query.replace(/^&/g,'');
                if( url.indexOf('?')>0 ){
                    url += '&' + query;
                }else{
                    url += '?' + query;
                }
                window.location.href = url;
            });

            $('#time_start').datetimepicker({
                format: 'yyyy-mm-dd',
                language: "zh-CN",
                minView: 2,
                autoclose: true
            });

            $('#datetimepicker').datetimepicker({
                format: 'yyyy-mm-dd',
                language: "zh-CN",
                minView: 2,
                autoclose: true,
                pickerPosition: 'bottom-left'
            })

            $('#time_start2').datetimepicker({
                format: 'yyyy-mm-dd',
                language: "zh-CN",
                minView: 2,
                autoclose: true
            });

            $('#datetimepicker2').datetimepicker({
                format: 'yyyy-mm-dd',
                language: "zh-CN",
                minView: 2,
                autoclose: true,
                pickerPosition: 'bottom-left'
            })
            //回车自动提交
            $('.jssearch').find('input').keyup(function(event){
                if(event.keyCode===13){
                    $("#search").click();
                }
            });
        })


    </script>
</block>