<?php

namespace Heanup\Frame;

use Heanup\Config\Lang\Lang;
/**
 * 简易模板类
 */
class Template
{
    private $data = array();
    private $path;
    private $tpl = null;
    private $suffix = '.phtml';


    public function __isset($name)
    {
        return isset($this->data[$name]);
    }

    public function __set($name, $value = null)
    {
        $this->data[$name] = $value;
    }

    public function __get($name)
    {
        return isset($this->data[$name]) ? $this->data[$name] : null;
    }

    /**
     * 设置模板路径
     * @param $path
     * @return $this
     */
    public function setPath($path)
    {
        if ($path) {
            $this->path = rtrim($path, '/') . DIRECTORY_SEPARATOR;
        }
        return $this;
    }

    /**
     * 设置模板后缀
     * @param $suffix
     * @return $this
     */
    public function setSuffix($suffix)
    {
        if ($suffix) {
            $this->suffix = $suffix;
        }
        return $this;
    }

    /**
     * 设置模板名称
     * @param $tpl
     * @return $this
     */
    public function setTpl($tpl)
    {
        if ($tpl) {
            $this->tpl = $tpl;
        }
        return $this;
    }

    /**
     * 变量赋值, 可以单独赋值,也可以批量赋值
     * @param $name
     * @param null $value
     * @return $this
     */
    public function assign($name, $value = null)
    {
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                $this->data[$k] = $v;
            }
        } else {
            $this->data[$name] = $value;
        }
        return $this;
    }

    /**
     * 获取全部数据
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * 载入其他模板
     * @param $tpl
     * @return null
     */
    public function load($tpl)
    {
        $tpl = $this->path . $tpl . $this->suffix;
        if (is_readable($tpl)) {
            include($tpl);
        } else {
            return null;
        }
    }

    /**
     * 渲染页面
     * @param bool $isReturn
     * @param string $layout
     * @return string
     */
    public function render($isReturn = false, $layout = '')
    {
        $filename = $this->path . $this->tpl . $this->suffix;
        // 判断模板是否存在
        if (!is_readable($filename)) {
            Logger::error($filename, 'template.not_found');
            return '';
        }
        // 判断layout是否存在
        if ($layout && is_readable($this->path . $layout . $this->suffix)) {
            $filename = $this->path . $layout . $this->suffix;
        }
        if ($isReturn == false) {
            include($filename);
        } else {
            ob_start();
            include($filename);
            $content = ob_get_contents();
            ob_end_clean();
            return $content;
        }
    }

    /**
     * 输出ajax信息
     * @param bool $result
     * @param null $code
     * @param null $msg
     * @param null $data
     */
    public function output($result = true, $code = null, $msg = null, $data = null)
    {
        if ($result instanceof Exception) {
            $code = $result->getCode();
            $msg = $result->getMessage();
            $data = $result->__toString();
            $result = false;
        }
        if ($result AND !is_bool($result) AND is_null($data)) {
            $data = $result;
        }
        $result = (bool)$result;
        $code = (int)$code;
        $msg = (!empty($msg)) ? $msg : '';
        $data = (!empty($data)) ? $data : '';

        $data = array(
            'result' => $result,
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        );

        header('Content-type: application/json');
        echo json_encode($data);
    }

    /**
     * 获取同域名下的其他子域名
     * @param $domain
     * @return string
     */
    protected function getDomainHost($domain)
    {
        $domainHost = $_SERVER['HTTP_HOST'];
        $domainHost = $domainHost."/".$domain;
        return $domainHost;
    }
    /**
     * 获取多语言配置文件
     * @param string $key key
     * @param string $default 默认值
     */
    protected function getLang($key,$default="",$app="Admin",$lang=NULL){
        return Lang::getData($key,$default,$app,$lang);
    }

    protected function getPager($pager){
//        if($pager['total_page'] == 0 || $pager['total_page'] <= $pager['page_size']){
//            return '';
//        }
        $current_start = ($pager['current_page']-1)*$pager['page_size']+1;
        $current_end = $pager['current_page']*$pager['page_size'] < $pager['total_count'] ? $pager['current_page']*$pager['page_size']:$pager['total_count'];
        $html = '<div class="row">
        <div class="col-sm-5">
        <div class="dataTables_info">当前显示 '.$current_start.' 到 '.$current_end.' 条，共 '.$pager['total_count'].' 条记录</div>
        </div><div class="col-sm-7">
        <div class="text-right">
        <ul class="pagination" style="margin:0">';
        $start = 1;
        $skip = 0;
        $end = $pager['total_page'];
        if($pager['current_page'] - 3 > $start){
            $start = $pager['current_page'] - 3;
            $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page=1&'.$pager['filter'].'">首页</a></li>';
        }else{
            $skip = 4 - $pager['current_page'];
        }
        if($pager['current_page'] + 3 < $end){
            $end = $pager['current_page'] + 3 + $skip;
        }
        for($i = $start; $i <= $end; $i++){
            if($i == $pager['current_page']){
                $html .= '<li class="paginate_button active"><a href="javascript:;">'.$i.'</a></li>';
            }else{
                $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page='.$i.'&'.$pager['filter'].'">'.$i.'</a></li>';
            }
        }
        if($end < $pager['total_page']){
            $html .= '<li class="paginate_button"><a href="'.$pager['url'].'?page='.$pager['total_page'].'&'.$pager['filter'].'">末页</a></li>';
        }
        $html .= '</ul></div>';
        return $html;
    }

}
