<?php

//分页函数
function show_page($page) {
    $pageSize = $page['pageSize']; //每页展示条数
    $total = $page['total'];   //数据总条数
    $url = $page['url'];     //页码链接
    $pageNumber = ceil($total / $pageSize); //总页数
    $currentPage = $page['current'] ? $page['current'] : 1; //当前页码

    $before = 0;  //开始展示分页的页码
    $after = 0;  //最多展示到该页码
    $max_show_count = 10; //一次最多展示10页
    $nowview = ceil(($currentPage + 1) / $max_show_count); //第几个10页
    $before = $nowview == 1 ? ($nowview - 1) * $max_show_count + 1 : ($nowview - 1) * $max_show_count;
    if ($pageNumber >= $nowview * $max_show_count) {
        $after = $nowview * $max_show_count;
    } else {
        $after = $pageNumber;
    }
    if (($after - $before) < 9) {
        $before = $after - 8;
    }

    $html = '';
    if ($nowview > 1)
        $before--;
    //如果结果超过一页，则拼接分页html
    if ($pageSize <= $total) {
        //拼接第$before页到第$after页的html结构
        for ($i = $before; $i <= $after; $i++) {
            if ($i < 1) {
                continue;
            }
            if ($i == $currentPage) {
                $html .= "<li><a class=\"active\" href=\"javascript:void(0)\">{$i}</a></li>";
            } else {
                $html .= "<li><a href=\"{$url}/{$i}\">$i</a></li>";
            }
        }
        //页数过多时展示首页和尾页
        if ($currentPage >= $max_show_count) {
            $html = "<li><a href=\"{$url}/1\">1</a></li>" . ' ...... ' . $html;
        }
        if ($pageNumber > ($before + $max_show_count + 1)) {
            $html .=' ...... ' . "<li><a href=\"{$url}/{$pageNumber}\">$pageNumber</a></li>";
        }

        //上一页和下一页默认不可用
        $prev_page_url = 'javascript:void(0);';
        $next_page_url = 'javascript:void(0);';
        $prev_disabled = $next_disabled = 'disabled';
        //当前页码大于1时上一页才可用
        if ($currentPage > 1) {
            $prev_disabled = '';
            $prev_page_url = $url . '/' . ($currentPage - 1);
        }
        //当前页码小于总页数时下一页才可用
        if ($currentPage < $pageNumber) {
            $next_disabled = '';
            $next_page_url = $url . '/' . ($currentPage + 1);
        }
        //拼接上一页和下一页的html
        $html = '<li class="' . $prev_disabled . '"><a href="' . $prev_page_url . '">上一页</a></li>' . $html;
        $html.='<li class="' . $next_disabled . '"><a href="' . $next_page_url . '">下一页</a></li>';
    }
    $res['htmls'] = $html;
    $res['prepage'] = $currentPage - 1;
    $res['nextpage'] = $currentPage + 1;
    $res['totalpage'] = $pageNumber;
    return $res;
}

function MakeOrderNumber() {
    //return str_replace('.', '', sprintf('%11.4f', gettimeofday(TRUE)));
    return time();
}

function convert_to_rmb_by_char($money, $char) {
    $ci = &get_instance();
    $currency_rate = $ci->lang->line('currency_rate');
    $char = strtoupper($char);
    $rate = isset($currency_rate[$char]) ? $currency_rate[$char] : 1;
    return ceil($money * $rate);
}

function get_btn_status($o_status, $key, $current_step) {
    $str = '';
    if ($o_status == '不成行' || $o_status == '无效订单') {
        $str = 'disabled="disabled"';
    } elseif (strpos($o_status, '我的订单') !== false && $key == 3) {
        $str = '';
    } elseif ($key != $current_step + 1) {
        $str = 'disabled="disabled"';
    }
    return $str;
}

function new_get($variable, $default = '') {
    $result = (isset($variable) && !empty($variable)) ? $variable : $default;
    return $result;
}

function set_rate($a, $b) {
    if ($a == 0) {
        $result = '-';
    } else {
        $result = ($b / $a) * 100;
        $result = (sprintf('%.2f', $result)) . '%';
    }
    return $result;
}

function last_month_today($time) {
    $last_month_time = mktime(date("G", $time), date("i", $time), date("s", $time), date("n", $time), 0, date("Y", $time));
    $last_month_t = date("t", $last_month_time);
    if ($last_month_t < date("j", $time)) {
        return date("Y-m-t H:i:s", $last_month_time);
    }
    return date(date("Y-m", $last_month_time) . "-d H:i:s", $time);
}

function biz_orderstatus($status_code) {
    $status_code_arr = array('1' => '新的预定'
        , '4' => '下计划'
        , '7' => '已确认'
        , '40' => '不成行订单'
        , '9' => '成功订单'
        , '30' => '取消预定'
        , '8' => '已收款'
        , '50' => '无效订单'
        , '60' => '未订先联系'
        , '61' => '已收款并出票'
        , '62' => '已收款未出票'
        , '999' => '预订中'
        , '11' => '我的订单'
    );
    return isset($status_code_arr[$status_code]) ? $status_code_arr[$status_code] : '';
}
