<?php

namespace Heanup\Frame\Helper;

/**
 * 格式转换
 */
class Html extends \Heanup\Frame\Helper
{

    /**
     * @param null $msg
     * @param null $url
     * @param array $get
     * @param int $wait
     */
    public static function location($msg = null, $url = null,$isParent=FALSE, $get = Array(), $wait = 1)
    {
        $url = self::getSiteUrl($url, $get);
        $target=$isParent?".parent":"";
        $str = "<script type='text/javascript'>function flash(){window".$target.".location.href='$url';}setTimeout('flash()','" . intval($wait) * 500 . "');</script>";
        if (!is_string($msg)) {
            $msg = $msg ? "操作成功！" : "操作失败！";
        }
        echo $str . $msg . "&nbsp;&nbsp;正在<a href='$url'>跳转中</a>.....";
        die();
    }

    /**
     * 取得当前页地址
     * @return mixed
     */
    public static function self()
    {
        return $_SERVER['REQUEST_URI'];
    }

    /**
     * 调整url自动生成规则
     * @param string $action
     * @param array $get
     * @param bool $mergeGet
     * @return string
     */
    public static function getSiteUrl($action = '', Array $get = array(), $mergeGet = false)
    {
        $url = '/' . trim($action, '/');
        if ($mergeGet) {
            $get = array_merge($_GET, $get);
        }
        if ($get) {
            $get = urldecode(http_build_query($get));
        }
        if ($get) {
            $url .= '?' . $get;
        }
        return $url;
    }

    /**
     * 链接生成器
     * @param $url
     * @param array $get
     * @param null $text
     * @param null $attributes
     * @return string
     */
    public static function anchor($url, Array $get = array(), $text = null, $attributes = null)
    {
        $url = '/' . trim($url, "/") . '/?' . http_build_query($get);
        $text = strlen($text) <= 0 ? $url : $text;
        $string = "<a href='$url' $attributes>$text</a>";
        return $string;
    }

    /**
     * 简易分页函数
     * @param string $url 跳转地址
     * @param int $totalPage 总页数
     * @param int $current 当前页
     * @param int $records 总记录数
     * @param int $displayCount 总共显示的页数
     * @return string
     */
    public static function pagination($url, $totalPage = 10, $current = 1, $records = 100, $displayCount = 10)
    {
        $current = ($current < $totalPage) ? $current : $totalPage;
        $current = ($current >= 1) ? $current : 1;
        $first = 1;
        $half = ceil($displayCount / 2);
        $end = $totalPage;

        $prev = $current - 1;
        $next = $current + 1;
        foreach ($_GET as $key => $value) {
            if($value === ''){
                unset($_GET[$key]);
            }
        }
        $linkSum = "<li><span class='sum'>总数：$records</span></li>";
        $linkFirst = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $first)), '&laquo;') . '</li>';
        $linkPrev = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $prev)), '&#8249;') . '</li>';
        $linkNext = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $next)), '&#8250;') . '</li>';
        $linkEnd = '<li>' . self::anchor($url, array_merge($_GET, array('page' => $end)), '&raquo;') . '</li>';


        if ($current <= $half) {
            $linkFirst = '';
            $last = $displayCount;
            if ($last > $end) {
                $last = $end;
            }
            if ($current == 1) {
                $linkPrev = '';
            }
            if ($current == $end) {
                $linkNext = '';
            }
        } elseif ($current > ($end - $half) AND $current <= $end) {
            $first = $current - $half + 1;
            $last = $end;
            $linkEnd = '';
            if ($current == $end) {
                $linkNext = '';
            }
        } else {
            $first = $current - $half + 1;
            $last = $current + $half;
        }

        $string = null;
        for ($i = $first; $i <= $last; $i++) {
            if ($i == $current) {
                $string .= '<li class="active">' . self::anchor($url, array_merge($_GET, array('page' => $i)), "$i", "class='active'") . '</li>';
            } else {
                $string .= '<li>' . self::anchor($url, array_merge($_GET, array('page' => $i)), "$i") . '</li>';
            }
        }

        return $linkSum . $linkFirst . $linkPrev . $string . $linkNext . $linkEnd;
    }

    /**
     * 下拉菜单生成器
     * @param $list
     * @param $key
     * @param $text
     * @param null $attributes
     * @return null|string
     */
    public static function loopOutputSelect($list, $key = '', $text = '', $attributes = null)
    {
        if (!is_array($list) OR empty($list)) {
            return null;
        }
        $string = null;
        foreach ($list as $k => $v) {
            $selected = null;
            if ($k == $key) {
                $selected = "selected='selected'";
            }
            $string .= "<option value='$k' $selected >$v</option>\n";
        }
        if (!empty($text)) {
            $string = "<select name='$text' id='$text' $attributes>" . $string . "</select>";
        }
        return $string;
    }

    /**
     * 单选按钮生成器
     * @param $list
     * @param $key
     * @param $text
     * @param null $attributes
     * @return null|string
     */
    public static function loopOutputRadio($list, $key = '', $text = '', $attributes = null)
    {
        if (!is_array($list) OR empty($list)) {
            return null;
        }
        $i = 0;
        $string = null;
        foreach ($list as $k => $v) {
            $checked = null;
            if ($k == $key) {
                $checked = "checked='checked'";
            }
            $string .= "<input type='radio' name='$text' value='$k' id='$text-$i' $checked $attributes /><label for='$text-$i'>$v</label>\n";
            $i++;
        }
        return $string;
    }

    /**
     * 多选框生成器
     * @param $list
     * @param string $key
     * @param string $text
     * @param string $attributes
     * @param string $separator
     * @return bool|string
     */
    public static function loopOutputCheckbox($list, $key = "", $text = '', $attributes = "", $separator = "\n")
    {
        if (!is_array($list) || empty($list)) {
            return false;
        }
        $string = '';

        if (!is_array($key)) {
            $key = array($key);
        }
        foreach ($list as $k => $v) {
            $string .= "<input type='checkbox' name='$text" . "[]' value='$k' ";
            $string .= in_array($k, $key) ? " checked ='checked'" : "";
            $string .= $attributes;
            $string .= " />$v" . $separator;
        }
        return $string;
    }
    
    /**
     * 获取强密码
     * @param int $length
     * @return string
     */
    public static function getStrongCode($length = 8)
    {
        $length = $length > 8 ? intval($length) : 8;
        $arr = array();
        $arr[] = self::getRandoms($length - 4);
        $arr[] = strtoupper(self::getRandoms(1, 'char'));
        $arr[] = strtolower(self::getRandoms(1, 'char'));
        $arr[] = mt_rand(0, 9);
        $special = '!@#$%^*()';
        $arr[] = $special{mt_rand(0, 11)};
        shuffle($arr);
        return implode('', $arr);
    }
    /**
     * 获取随机数
     *
     * @param int $length
     * @param string $type (可选)
     * @return string
     */
    public static function getRandoms($length = 4, $type = '')
    {
        $returnStr = '';
        $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        if ($type == 'char') {
            $start = 10;
            $end = 61;
        } elseif ($type == 'num') {
            $start = 0;
            $end = 9;
        } else {
            $start = 0;
            $end = 61;
        }
        for ($i = 0; $i < $length; $i++) {
            $returnStr .= $pattern{mt_rand($start, $end)}; // 生成php随机数
        }
        return $returnStr;
    }

}