<style>
    .submit-check { margin: 0 auto; width: 200px; }

    /* 提交审核弹框label */
    .submit-check label { display: inline-block; width: 120px; text-align: right;}
</style>

<p style="padding-left: 5px;">人工审单</p>
<div class="tabs-box">
    <div class="order-change-main">
        @if (($order_info['order_goods_type'] == 1 && $order_info['status'] == 1) || ($order_info['order_goods_type'] != 1 &&in_array($order_info['status'], [1, 2]) && $order_info['adjust_count'] < 2))
        <form id="checkOrderForm" class="form-horizontal table-responsive">
            <input type="hidden" name="order_sn" value="{{$order_info['order_sn']}}">
            <input type="hidden" name="user_id" value="{{$order_info['user_id']}}">
            <input type="hidden" name="order_id" value="{{$order_info['order_id']}}">
            <input type="hidden" name="order_goods_type" value="{{$order_info['order_goods_type']}}">
            <input type="hidden" name="sale_id" value="{{$order_info['sale_id']}}">
            <input type="hidden" name="goods_amount" value="{{$order_price_info['goods_price']}}">
            <input type="hidden" name="order_amount" value="{{$order_info['order_amount']}}">
            <input type="hidden" name="cancel_reason" value="{{isset($order_info['cancle_reason']) ? $order_info['cancle_reason'] : ''}}">
            <input type="hidden" name="user_mobile" value="{{$user_mobile}}">
            <input type="hidden" name="check_failed" value="">
            <input type="hidden" name="check_failed_info" value="">
            <input type="hidden" name="change_pay_type" value="">
            
            <?php $isNewClient = App\Http\Controllers\isNewClient($order_info['order_goods_type'], $order_info['user_id'], $order_info['create_time']); ?>
            @if ($isNewClient)
            <!-- 用户来源 -->
            <p>用户来源</p>
            <table class="table table-bordered table-hover check-table">
                <tr class="client_source_row">
                    <td class="check-table-title" width="20%">用户来源:</td>
                    <td>  
                        <div class="multi-reason">
                            <label><input type="radio" name="client_source" value="QQ群">QQ群</label>
                            <label><input type="radio" name="client_source" value="微信群">微信群</label>
                            <label><input type="radio" name="client_source" value="微信公众号">微信公众号</label>
                            <label><input type="radio" name="client_source" value="朋友介绍">朋友介绍</label>
                            <label><input type="radio" name="client_source" value="线下广告">线下广告</label>
                            <label><input type="radio" name="client_source" value="地推">地推</label>
                            <label><input type="radio" name="client_source" value="1">其他</label>
                        </div>  

                        <div class="client-input-other" style="display: none;">
                            <textarea name="input-other-source" id="input-other-source" placeholder="请填写其他来源"></textarea> 
                        </div>             
                    </td>
                </tr>
            </table>
            @endif

            <!-- 审核 -->
            @if ($order_info['order_goods_type'] == 1)
            <p>审核</p>
            <table class="table table-bordered table-hover check-table">
                <tr>
                    <td class="check-table-title" width="20%">付款类型:</td>
                    <td class="check-select">
                        <label><input class="order_all_pay" type="radio" name="order_pay_type" value="1">全额付款</label>                        
                        <!-- 销售类型为现卖时展示 -->
                        @if ($order_info['sale_type'] == 1)
                            <label><input class="order_advance_pay" type="radio" name="order_pay_type" value="2">预付首款</label>
                            <label><input class="order_account_period" type="radio" name="order_pay_type" value="3">账期</label>
                            <label><input class="order_cod" type="radio" name="order_pay_type" value="4">货到猎芯付款</label>
                        @endif
                    </td>
                </tr>
                <tr>
                    <td class="check-table-title" width="20%">调价审核:</td>
                    <td class="check-select">
                        <label><input type="radio" name="order_status" value="2" checked>审核通过</label>
                        <label><input type="radio" name="order_status" class="input-cancel-reason" value="-1" data-oid="{{$order_info['order_id']}}" data-type="3">审核不通过</label>
                    </td>
                </tr>
            </table>
            
            <hr/>
            @else 
            <table class="table table-bordered table-hover check-table">
                <tr>
                    <td class="check-table-title" width="20%">付款类型:</td>
                    <td class="check-select">
                        <label><input class="order_all_pay" type="radio" name="order_pay_type" value="1">全额付款</label>    
                        <label><input class="order_account_period" type="radio" name="order_pay_type" value="3">账期</label>
                    </td>
                </tr>
            </table>
            @endif

            <!-- 调价 -->
            <div>
                <p style="float: left;">调价</p>
                <a class="btn btn-info add_goods" style="float: right; margin-bottom: 5px;">新增商品</a>
            </div>
            
            <table class="table table-bordered table-hover order-change-table">
                <thead>
                    <th width="20%">供应商</th>
                    <th>型号</th>
                    <th>调整数量</th>
                    <th>调整单价</th>
                    <!-- 销售类型为预售时展示 -->
                    @if ($order_info['sale_type'] == 2)
                        <th>交期时间/天</th>
                        <th>锁定库存</th>
                    @endif
                </thead>

                <tbody>
                    @foreach ($order_items_info as $v)
                        <tr data-sup="{{$v['canal'] ? $v['supplier_id'].'.'.$v['canal'] : $v['supplier_id']}}">
                            <td>
                                <p>{{$v['supplier_name']}}</p>
                            </td>
                            <td>
                                <input type="hidden" name="change_info[{{$v['rec_id']}}][goods_name]" value="{{$v['goods_name']}}">
                                <p>{{$v['goods_name']}}</p>
                            </td>
                            <td>
                                <input class="only_positive_nubme only_number num" name="change_info[{{$v['rec_id']}}][goods_number]" value="{{$v['goods_number']}}">
                            </td>
                            <td>
                                <span>{{$currency}}</span>
                                <input class="price only_number" name="change_info[{{$v['rec_id']}}][goods_price]" data-origin="{{$v['original_price']}}" value="{{$v['goods_price']}}">
                            </td>
                            <!-- 销售类型为预售时展示 -->
                            @if ($order_info['sale_type'] == 2)
                                <td>
                                    <input type="text" class="only_number" name="change_info[{{$v['rec_id']}}][delivery_time]" value="{{$v['delivery_time']}}" placeholder="请填写天数">
                                </td>
                                <td>
                                    <!-- 未锁库存 -->
                                    @if (!$v['is_lock']) 
                                    <label>
                                        <input type="checkbox" name="change_info[{{$v['rec_id']}}][is_lock]" value="{{$v['goods_id']}}">
                                        <span>锁定库存,可用库存:<b class="text-danger">{{ App\Http\Controllers\getSelfStock($v['goods_id']) }}</b></span>
                                    </label>
                                    @endif
                                </td>
                            @endif
                        </tr>
                    @endforeach
                </tbody>
            </table>

            <textarea name="kefu_remark" id="kefu_remark" class="form-control" placeholder="客服备注信息" style="resize: none;">{{$order_temp_info['kefu_remark']}}</textarea>
            
            @if ($order_info['order_goods_type'] == 2)
            <table class="table table-bordered table-hover" style="margin-top:10px;">
                <tr>
                    <th width="20%">批量调价</th>
                    <td>
                        <input type="text" class="set_goods_price" name="set_goods_price" value="1">
                        <span class="text-danger">(批量设置单价,默认值1,折扣范围 0 < 值 <= 1)</span>
                    </td>
                </tr>
            </table>
            @endif
            
            <?php $extend_fee_items = json_decode($order_temp_info['extend_fee_items'], true); ?>
            
            <!-- 供应商附加费 -->
            @if ($extend_fee_items)
                <hr/>
                <p>附加费设置</p>
                <table class="table table-bordered table-hover extend-fee-table">
                    <thead>
                        <th width="20%">供应商/渠道</th>
                        <th>商品金额</th>
                        <th>调整附加费</th>
                    </thead>

                    <tbody>
                        @foreach ($extend_fee_items as $k => $v) 
                            <tr data-sup="{{$k}}">
                                <td>
                                    @if (strpos($k, '.') !== false)
                                        <p>
                                            <?php 
                                                $sid = explode('.', $k);
                                                // echo App\Http\Controllers\getSupplierName($sid); 
                                                
                                                if ($sid[0] == 17) { // 联营供应商
                                                    echo $sid[1];
                                                } else {
                                                    echo $v['supplier_name'];
                                                }
                                            ?>
                                        </p>  
                                    @else
                                        <p>{{$v['supplier_name']}}</p>
                                    @endif
                                    <input type="hidden" name="change_extend_fee[{{$k}}][supplier_name]" value="{{$v['supplier_name']}}">
                                </td>
                                <td>
                                    <input type="hidden" name="change_extend_fee[{{$k}}][amount]" class="sup_amount" value="{{$v['amount']}}">
                                    <p class="sup_amount_val">{{$v['amount']}}</p>
                                </td>
                                <td>
                                    <span>{{$currency}}</span>
                                    <input class="adjust_extend_fee only_number" name="change_extend_fee[{{$k}}][extend_fee]" value="{{$v['extend_fee']}}">
                                </td>

                                <input type="hidden" name="change_extend_fee[{{$k}}][count]" value="{{$v['count']}}">

                                @if (isset($v['ladder_max']))
                                    <input type="hidden" name="change_extend_fee[{{$k}}][ladder_max]" value="{{$v['ladder_max']}}">
                                @endif
                            </tr>   
                        @endforeach
                    </tbody>
                </table>
            @endif

            <hr/>
            <table class="table table-bordered table-hover">
                <tr>
                    <th width="20%">商品总金额</th>
                    <td><p class="text-danger">{{$currency}}<span class="goods_total">{{$order_price_info['goods_price']}}</span></p></td>
                </tr>
                
                <!-- 销售类型为现卖时展示 -->
                @if ($order_info['sale_type'] == 1 && $order_price_info['ext_price'])
                    <tr>
                        <th width="20%">附加费金额</th>
                        <td>
                            <p class="text-danger">{{$currency}}<span class="extra_fee">{{$order_price_info['ext_price'] ? $order_price_info['ext_price'] : 0}}</span></p>
                            <input type="hidden" name="extra_fee" autocomplete="off" value="{{$order_price_info['ext_price'] ? $order_price_info['ext_price'] : 0}}">
                        </td>
                    </tr>
                @endif

                @if ($order_info['order_goods_type'] == 2)
                    <tr>
                        <th>运费</th>
                        <td>
                            <span>{{$currency}}</span>
                            <input class="only_number freight_fee" type="text" name="freight_fee" value="{{ $order_price_info['shipping_price'] ? $order_price_info['shipping_price'] : 0 }}">
                        </td>
                    </tr>
                @endif

                <tr>
                    <th width="20%">订单总额</th>
                    <td>
                        <p class="text-danger">{{$currency}}<span class="total">{{$order_info['order_amount']}}</span></p>
                    </td>
                </tr>
            </table>

            <div class="show-advance-pay">
                <hr/>
                <p>定金调整</p>
                <table class="table table-bordered table-hover">
                    <tr>
                        <th width="20%">定金金额</th>
                        <td>
                            <p class="text-danger">{{$currency}}
                            <?php 
                                if (!$order_info['advance_amount'] OR $order_info['advance_amount']==0) {
                                    $price = number_format($order_info['order_amount']*(3/10), 2, '.', '');

                                    echo '<input type="text" name="deposit_amount" class="only_number advance" value="'.$price.'">';
                                } else {
                                    echo '<input type="text" name="deposit_amount" class="only_number advance" value="'.$order_info['advance_amount'].'">';
                                }                          
                            ?>
                            </p>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="show-pay-time">
                <p>付款时间</p>
                <table class="table table-bordered table-hover change-table">
                    <tr>
                        <th width="20%">选择付款时间</th>
                        <td>
                            <select name="payTime" class="payTime">
                                <option value="">请选择</option>
                                <option value="1">1天</option>
                                <!-- 预售订单默认2天 -->
                                @if ($order_info['sale_type'] == 2)
                                    <option value="2" selected>2天</option>
                                @endif
                                <option value="3">3天</option>
                                <option value="7">7天</option>
                                <option value="15">15天</option>
                                <option value="other">其他</option>
                            </select>    

                            <div class="customArea">
                                <input type="text" name="payTimeOther" class="payTimeOther" value="" style="width: 50px;">&nbsp;天 <span class="error">(自定义天数,限制1~30)</span>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            
            @if ($order_info['order_goods_type'] != 1 && in_array($order_info['status'], [1, 2]) && $order_temp_info['status'] == 1)
                <a class="btn btn-primary checkAdjusted">查看调价信息</a>
            @else
                <a class="btn btn-primary checkOrder">提交</a>
            @endif
        </form>
        @endif 

        @if (($order_info['order_goods_type'] == 1 && $order_info['status'] == 2) || ($order_info['order_goods_type'] != 1 && $order_info['adjust_count'] == 2))
        <div class="checked_section">
            <div class="checked_left">
                <p>交易状态:&nbsp;审核通过,请联系客户及时付款!
                    @if ($order_info['pay_time'])
                    <span>订单付款时间截止至:{{date('Y-m-d H:i:s', $order_info['pay_time'])}}</span>
                    @endif
                </p>

                <?php 
                    if ($order_info['order_goods_type'] == 2) {
                        $adjustPriceUrl = "/adjustPrice/".$order_info['order_id']."?tags=self";
                    } else {
                        $adjustPriceUrl = "/adjustPrice/".$order_info['order_id'];
                    }
                ?>

                @if ($order_info['order_goods_type'] == 2 && $order_info['adjust_count'] < 2)
                    <a class="btn btn-default" href="{{$adjustPriceUrl}}">再次调价</a><span class="warn-tip">每笔订单仅有一次“再次调价”机会</span>
                @elseif ($order_info['order_goods_type'] == 1)
                    <a class="btn btn-default" href="{{$adjustPriceUrl}}">再次调价</a>
                @endif
            </div> 
            
            <!-- 非账期订单显示剩余时间 -->
            @if ($order_info['order_pay_type'] != 3)
            <div class="checked_right">
                <p class="showtime">
                    <span class="error">剩余付款时间:</span>
                    <span class="count_time count_day"></span>天
                    <span class="count_time count_hour"></span>时
                    <span class="count_time count_min"></span>分
                    <span class="count_time count_sec"></span>秒
                </p>

                <div class="showtime delay_time">
                    <span class="delay_time_title">延长付款时间:</span>
                            
                    <select name="payTime" class="payTime">
                        <option value="">请选择</option>
                        <option value="1">1天</option>
                        <option value="3">3天</option>
                        <option value="7">7天</option>
                        <option value="15">15天</option>
                        <option value="other">其他</option>
                    </select>    

                    <div class="customArea">
                        <input type="text" name="payTimeOther" class="payTimeOther" value="" style="width: 50px;">&nbsp;天 <span class="error">(自定义天数,限制1~30)</span>
                    </div>

                    <button class="btn btn-info btn-xs delay_confirm">确认</button>
                </div>
            </div>
            @endif
        </div>
        @endif

        @if ($order_info['status'] == -1)
            @if ($order_info['cancel_reason'] != '')
                <p>交易状态:&nbsp;审核不通过</p>
                <p>审核不通过原因:&nbsp;{{$order_info['cancel_reason']}}</p>
            @else
                <p>交易状态:&nbsp;用戶取消</p>
            @endif
        @endif
    </div>
</div>

<!-- 自营订单客服调价低于80% 显示经理审核 -->
@if ($order_info['order_goods_type'] != 1 && in_array($order_info['status'], [1, 2]) && $order_temp_info['status'] == 1)
<script>
    $('#checkOrderForm').find('input').attr('disabled', true);
    $('#checkOrderForm').find('select').attr('disabled', true);
</script>

<div class="tabs-box adjusted_order" style="display: none;">
    <div class="order-change-main">
        <form id="adjustedOrderForm" class="form-horizontal table-responsive">
            <input type="hidden" name="order_id" value="{{$order_info['order_id']}}">
            <input type="hidden" name="sale_id" value="{{$order_info['sale_id']}}">
            <input type="hidden" name="extra_fee" value="{{$order_temp_info['extra_fee']}}">
            <input type="hidden" name="freight_fee" value="{{$order_temp_info['freight_fee']}}">
            <input type="hidden" name="goods_amount" value="{{$order_temp_info['goods_amount']}}">
            <input type="hidden" name="deposit_amount" value="{{$order_temp_info['deposit_amount']}}">
            <input type="hidden" name="order_pay_type" value="{{$order_temp_info['order_pay_type']}}">
            <input type="hidden" name="pay_time_limit" value="{{$order_temp_info['pay_time_limit']}}">
            <input type="hidden" name="cancel_reason" value="{{isset($order_info['cancle_reason']) ? $order_info['cancle_reason'] : ''}}">

            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>型号</th>
                        <th>调整数量</th>
                        <th>调整单价</th>
                    </tr>
                </thead>

                <tbody>
                    @if (isset($order_temp_info['change_info']))
                        @foreach ($order_temp_info['change_info'] as $k => $v)
                            <tr>
                                <td>
                                    <input type="hidden" name="change_info[{{$k}}][goods_name]" value="{{$v['goods_name']}}">
                                    <p>{{$v['goods_name']}}</p>
                                </td>
                                <td>
                                    <input type="hidden" name="change_info[{{$k}}][goods_number]" value="{{$v['goods_number']}}">
                                    <p>{{$v['goods_number']}}</p>
                                </td>
                                <td>
                                    <input type="hidden" name="change_info[{{$k}}][goods_price]" value="{{$v['goods_price']}}">
                                    <p>{{$v['goods_price']}}</p>
                                </td>
                            </tr>
                        @endforeach
                    @endif
                </tbody>
            </table>
            <hr>
            <table class="table table-bordered table-hover">
                <tr>
                    <th width="20%">订单类型:</th>
                    <td>
                        @if ($order_temp_info['order_pay_type'] == 1)
                            全款
                        @elseif ($order_temp_info['order_pay_type'] == 2)
                            预付款
                        @else
                            账期
                        @endif
                    </td>
                </tr>

                <tr>
                    <th>商品总额:</th>
                    <td>{{$order_temp_info['goods_amount']}}</td>
                </tr>

                <tr>
                    <th>优惠金额:</th>
                    <td>{{$order_price_info['discount_amount']}}</td>
                </tr>

                <tr>
                    <th>附加费:</th>
                    <td>{{$order_temp_info['extra_fee']}}</td>
                </tr>

                <tr>
                    <th>运费:</th>
                    <td>{{$order_temp_info['freight_fee']}}</td>
                </tr>
                
                <?php $order_amount_temp = $order_temp_info['goods_amount'] + $order_price_info['discount_amount'] + $order_temp_info['extra_fee'] + $order_temp_info['freight_fee']; ?>
                <tr>
                    <th>订单总额:</th>
                    <td>{{$order_amount_temp}}</td>
                </tr>
                
                @if ($order_info['order_goods_type'] == 2)
                <tr>
                    <th>支付优惠:</th>
                    <td>{{$order_price_info['pay_preferential'] ? $order_price_info['pay_preferential'] : 0}}</td>
                </tr>

                <tr>
                    <th>实际支付金额:</th>
                    <td>{{$order_amount_temp + $order_price_info['pay_preferential']}}</td>
                </tr>
                @endif

                @if ($order_temp_info['order_pay_type'] == 2)
                <tr>
                    <th>定金:</th>
                    <td>{{$order_temp_info['deposit_amount']}}</td>
                </tr>
                @endif

                <tr>
                    <th>付款时间:</th>
                    <td>{{$order_temp_info['pay_time_limit'].'天'}}</td>
                </tr>
            </table>
        </form>
    </div>
</div>
@endif

@include('detail.addGoods')

<script>
    var is_manager       = "{{ in_array($role, Config('perm_args.is_manager_perm')) ? true : false }}"; 

    var isNewClient      = "{{isset($isNewClient) ? $isNewClient : 0}}"; 
    // var order_amount  = "{{$order_info['order_amount']}}"; 
    var currency         = "{{$order_info['currency']}}"; // 币种
    var order_pay_type   = "{{$order_info['order_pay_type']}}"; // 付款类型
    var discount_amount  = "{{ $order_price_info['discount_amount'] ? $order_price_info['discount_amount'] : 0 }}"; // 优惠券金额
    var pay_preferential = "{{ $order_price_info['pay_preferential'] ? $order_price_info['pay_preferential'] : 0 }}"; // 支付优惠(钱包)
    var new_client_price = "{{ $order_price_info['new_client_price'] }}";

    var rest_time = "{{$order_info['pay_time'] ? $order_info['pay_time'] - time() : NULL}}";

    if (rest_time) {
        countDown(rest_time);
    } else {
        $('.checked_right').hide();
    } 

    // 倒计时
    function countDown(times){
        var timer=null;
        timer = setInterval(function(){
            var day=0,
                hour=0,
                minute=0,
                second=0;//时间默认值

            if (times > 0) {
                day = Math.floor(times / (60 * 60 * 24));
                hour = Math.floor(times / (60 * 60)) - (day * 24);
                minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
                second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
            }

            if (day <= 9) day = '0' + day;
            if (hour <= 9) hour = '0' + hour;
            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;

            $('.count_day').text(day);
            $('.count_hour').text(hour);
            $('.count_min').text(minute);
            $('.count_sec').text(second);
           
            times--;
        }, 1000);

        if (times <= 0 ) {
            clearInterval(timer);
        }
    } 

    // 延长付款时间
    $('.delay_confirm').click(function(){
        var order_id = "{{$order_info['order_id']}}"; 
        var payTime = $('.payTime').val();
        var payTimeOther = $('.payTimeOther').val();
        var delay_time;

        if (payTime == '') {
            layer.msg('请选择付款时间');
            return false;
        }

        if (payTime == 'other' && payTimeOther == '') {
            layer.msg('请填写自定义时间');
            return false;
        }

        if (payTimeOther) {
            var reg = /\d/;
            if (!reg.test(payTimeOther)) {
                layer.msg('格式错误');
                return false;
            }

            if (payTimeOther < 0 || payTimeOther > 30) {
                layer.msg('自定义时间限制在1-30天');
                return false;
            }
        } 

        if (payTime != '' && payTime != 'other') {
            payTimeOther = '';
        }

        delay_time = payTimeOther ? payTimeOther : payTime;

        // 弹出提示
        layer.open({
            title: '提示信息',
            content: '确定延长付款时间吗?',
            btn:['取消','确定'],
            yes: function(index, layero){
                layer.close(index);
            },
            btn2: function(index, layero){
                $.ajax({
                    url : '/ajaxDelayTime',
                    type: 'post',
                    data: {order_id:order_id, delay_time:delay_time},
                    dataType: 'json',
                    success: function (resp) {console.log(resp)
                        if(resp.errcode === 0){
                            layer.msg(resp.errmsg || '操作成功');
                            setTimeout(function(){
                                location.reload();
                            }, 1000);
                        } else {
                            layer.alert(resp.errmsg || '网络异常');
                        }
                    },

                    error: function (res) {
                        console.log(res);
                    }
                })
            }
        });
    })
</script>