<?php
namespace Manage\Src\Lib;

use Dever;

class Input
{
    public static function __callStatic($name, $param = array())
    {
        $method = '_' . $name;

        $result = call_user_func_array(array(__CLASS__, $method), $param);
        if ($result && is_string($result) && $name != 'pageLeft') {

            if (layadmin()) {
                if (strstr($result, '<select')) {
                    $result = str_replace('form-control', 'form-control layui-input layui-select', $result);
                    $result = str_replace('<select', '<select lay-ignore ', $result);
                } elseif (strstr($result, 'radio')) {
                    $result = str_replace('form-control', 'form-control layui-radio', $result);
                } elseif (strstr($result, 'checkbox')) {
                    $result = str_replace('form-control', 'form-control layui-checkbox', $result);
                } else {
                    $result = str_replace('form-control', 'form-control layui-input', $result);
                }
                
                if (isset($param[1]) && !$param[1] && ((isset($param[0]['update']) && $param[0]['update'] != 'hidden') || !isset($param[0]['update']))) {
                    $result = '<div class="layui-inline"><div class="layui-input-inline">'.$result.'</div></div>';
                } else {
                    $result = '<div>'.$result.'</div>';
                }
            } else {
                if (isset($param[0]['update']) && $param[0]['update'] == 'hidden') {
                    
                } elseif (isset($param[1]) && !$param[1]) {
                    $result = '<div style="display:inline;margin-right: 10px;">'.$result.'</div>';
                }
            }
            
        }
        return $result;
    }

    /**
     * 地图
     *
     * @return string
     */
    public static function _map($param)
    {
        $url = Dever::url('map', 'manage');

        $html = self::text($param);

        $id = $param['name'] . '_value';
        $iframe_id = $id . '_map';

        $html .= '<iframe id="'.$iframe_id.'" src="'.$url.'" style="margin-top:10px;display: block;" width="800" height="500" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes" onload="mapLoad()"></iframe>';

        $html .= '<script>$(function()
        {
            $("#'.$id.'").blur(function()
            {
                layui.layer.msg("搜索中...");
                $("#'.$iframe_id.'")[0].contentWindow.search($(this).val());
            });
        });
        function mapLoad(){
            if ($("#'.$id.'").val()) {
                $("#'.$iframe_id.'")[0].contentWindow.showMaker($("#'.$id.'").val());
            }
        }
        function closeMsg() {
            layui.layer.closeAll();
        }
        function setMap(x, y, z) {
                var val = $("#'.$id.'").val();
                if (val) {
                    var temp = val.split(",");
                    val = temp[0];
                }
                $("#'.$id.'").val(val + "," + x + "," + y + "," + z);
            }</script>';

        return $html;
    }


    /**
     * desc
     *
     * @return string
     */
    public static function _modal($modal, $desc)
    {
        return '<a href="javascript:;" onclick="showAlert($(this).next().html());initLayui();">' . $modal . '</a><div style="display:none;">' . $desc . '</div>';
    }

    /**
     * desc
     *
     * @return string
     */
    public static function _desc($desc)
    {
        return '';
        return $desc;
    }

    /**
     * hr 分割线
     *
     * @return string
     */
    public static function _hr($param)
    {
        if (layadmin()) {
            
            $col = '';
            if (isset($param['right'])) {
                $col = '</div><div class="layui-col-md6">';
            }
            $html = '</div></div></div>'.$col.'<div class="layui-card"><div class="layui-card-header">[' . $param['name'] . ']</div><div class="layui-card-body"><div>';
            return $html;
        }
        return '<div class="dever_form_hr ' . (isset($param['class']) ? $param['class'] : '') . '" ' . (isset($param['attr']) ? $param['attr'] : '') . '>' . $param['name'] . '</div>';
    }

    /**
     * cue
     *
     * @return string
     */
    public static function _cue($param)
    {
        $param['cue'] = isset($param['cue']) ? $param['cue'] : '';
        if (strpos($param['name'], '-') !== false) {
            $temp = explode('-', $param['name']);
            $param['name'] = $temp[0];
            $param['cue'] = $temp[1];
        } elseif (strpos($param['name'], '(') !== false) {
            $temp = explode('(', $param['name']);
            $param['name'] = $temp[0];
            $param['cue'] = str_replace(')', '', $temp[1]);
        } elseif (strpos($param['name'], '（') !== false) {
            $temp = explode('（', $param['name']);
            $param['name'] = $temp[0];
            $param['cue'] = str_replace('）', '', $temp[1]);
        }

        return $param;
    }

    /**
     * pageLeft
     *
     * @return string
     */
    public static function _pageLeft($param)
    {
        $param = self::cue($param);

        if ($param['cue']) {
            $param['cue'] = '<span style="font-size:12px;font-weight:normal;font-color:#bababa;margin-left:5px;">[' . $param['cue'] . ']</span>';
        }

        $class = '';
        $style = '';
        if (layadmin()) {
            //$class = 'layui-form-label';
            if (isset($param['update']) && is_array($param['update'])) {
                $style = ' style="margin:10px;"';
            }
        }
        
        $html = '<div '.$style.'><label style="font-size:14px;font-weight:bold;" class="'.$class.'">' . $param['name'] . $param['cue'] . '</label></div>';

        return $html;
    }

    /**
     * textarea
     *
     * @return string
     */
    public static function _textarea($param)
    {
        return '<textarea style="height:100px;" update_type="value" class="update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" >' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '</textarea>';

        return '<textarea style="height:100px;" update_type="value" class="update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" value="' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '">' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '</textarea>';
    }

    private function uploadHandle($param)
    {
        $html = $style = '';
        $div = array('', '');
        $type = 'button';
        if (isset($param['upload']) && $param['upload'] == 'qiniu') {
            $upload = Dever::project('upload');
            if ($upload && !strstr($upload['path'], 'http')) {
                $upload = Dever::load('upload/qiniu.token?result=1&key=' . $param['key']);
            } else {
                $upload = Dever::load('upload/qiniu.token?key=' . $param['key']);
            }
            
            if (isset($upload['domain'])) {
                $domain = $upload['domain'];
                $token = $upload['uptoken'];
                $host = $upload['host'];
                $cover = 1;
                if (isset($param['cover'])) {
                    $cover = $param['cover'];
                }

                $date = explode('-', date("Y-m-d"));
                $path = $param['key'] . '/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
                $html = ' m="qiniu" cover="'.$cover.'" domain="'.$domain.'" path="'.$path.'" host="'.$host.'" token="'.$token.'" ';
            }
        }

        if (isset($param['large']) && $param['large'] && isset($upload['domain'])) {
            $html .= ' l="1" ';
            $type = 'file';
            $div = array('<div class="layui-input"  style=" position: relative;
    display: inline-block;
    border-radius: 2px;
    color: black;
    text-decoration: none;
    text-indent: 0;
    line-height: 37px;
    height: 37px;
    width: 97px;
    padding-left: 19px;">', '本地上传</div>');
            $style = 'position: absolute;
    font-size: 21px;
    right: 0;
    top: 0;
    opacity: 0;';
        }

        return array($html, $type, $div, $style);
    }

    /**
     * image
     *
     * @return string
     */
    public static function _image($param)
    {
        if (isset($param['value']) && $param['value']) {
            $pic = 'src="' . $param['value'] . '"';
        } elseif (isset($param['places'])) {
            $pic = 'src="http://placehold.it/' . $param['place'] . '"';
        } else {
            $pic = 'src="" style=display:none;';
        }
        $class = 'width:100px;display:inline;';
        if (layadmin()) {
            $class .= 'padding-left:0px;';
        }
        $upload = self::uploadHandle($param);

        $button = $upload[2][0] . '<input type="'.$upload[1].'" style="'.$class.$upload[3].'" class="form-control image_upload" '.$upload[0].' v="' . $param['name'] . '_' . $param['index'] . '" id="file_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="本地上传"/>' . $upload[2][1];
        return $button .'

        <input type="button" style="'.$class.'" class="form-control upload_show" v="' . $param['name'] . '_' . $param['index'] . '" id="file_show_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="图库"/>
        <div style="margin-top:10px;display:none;" class="layui-progress layui-progress-big" id="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-filter="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-showPercent="true">
          <div class="layui-progress-bar" lay-percent="0%"></div>
        </div>

        <div style="margin:10px;">
        <img ' . $pic . ' class="update_value" update_type="src" id="show_' . $param['name'] . '_' . $param['index'] . '" width="150" /></div>
        <input type="text" class="update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" id="' . $param['name'] . '_' . $param['index'] . '" value="' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '">';
    }

    /**
     * images
     *
     * @return string
     */
    public static function _images($param)
    {
        $pic = '';
        if (isset($param['value']) && $param['value']) {
            $temp = explode(',', $param['value']);
            foreach ($temp as $k => $v) {
                $pic .= '<a href="javascript:;" title="点此删除" onclick="picDel($(this), \'' . $param['name'] . '_' . $param['index'] . '\')"><img src="' . $v . '" id="show_' . $param['name'] . '_' . $param['index'] . '_' . $k . '" width="150"  style="margin-left:5px;" alt="点此删除"/></a>';
            }
        }
        $class = 'width:100px;display:inline;';
        if (layadmin()) {
            $class .= 'padding-left:0px;';
        }

        $upload = self::uploadHandle($param);

        $button = $upload[2][0] . '<input type="'.$upload[1].'" style="'.$class.$upload[3].'" class="form-control image_upload" '.$upload[0].' mul="yes" v="' . $param['name'] . '_' . $param['index'] . '" id="file_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="本地上传"/>' . $upload[2][1];
        return $button . '

        <input type="button" style="'.$class.'" class="form-control upload_show" mul="yes" v="' . $param['name'] . '_' . $param['index'] . '" id="file_show_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="图库"/>

        <div style="margin-top:10px;display:none;" class="layui-progress layui-progress-big" id="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-filter="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-showPercent="true">
          <div class="layui-progress-bar" lay-percent="0%"></div>
        </div>


        <div style="margin:10px;" update_type="html"  class="update_value ' . $param['name'] . '_' . $param['index'] . '_mul">' . $pic . '</div>

        <input type="text" class="update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" id="' . $param['name'] . '_' . $param['index'] . '" value="' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '">';
    }

    /**
     * upload
     *
     * @return string
     */
    public static function _upload($param)
    {
        $class = 'width:100px;display:inline;';
        if (layadmin()) {
            $class .= 'padding-left:0px;';
        }

        $upload = self::uploadHandle($param);

        $button = $upload[2][0] . '<input type="'.$upload[1].'" style="'.$class.$upload[3].'" class="form-control image_upload" '.$upload[0].' t="file" v="' . $param['name'] . '_' . $param['index'] . '" id="file_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="本地上传"/>' . $upload[2][1];
        return $button .'

        <input type="button" style="'.$class.'" class="form-control upload_show" t="file" v="' . $param['name'] . '_' . $param['index'] . '" id="file_show_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="文件库"/>

        <div style="margin-top:10px;display:none;" class="layui-progress layui-progress-big" id="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-filter="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-showPercent="true">
          <div class="layui-progress-bar" lay-percent="0%"></div>
        </div>

        <div style="margin-top:10px;"><input type="text" class="update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" id="' . $param['name'] . '_' . $param['index'] . '" value="' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '">
        </div>';
        //return '<input type="file" class="form-control" value="' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '" name="' . $param['name'] . '"/>';
    }

    /**
     * upload
     *
     * @return string
     */
    public static function _uploads($param)
    {
        $file = '';
        if (isset($param['value']) && $param['value']) {
            $temp = explode(',', $param['value']);
            foreach ($temp as $k => $v) {
                $upload = Dever::load('upload/file-check', array('option_key' => md5($v)));
                $file .= '<li><a href="' . $v . '" rel="box" title="点此下载" >' . $upload['source_name'] . '</a>&nbsp;&nbsp;<a href="javascript:;" style="color:red;" title="点此删除" onclick="fileDel($(this), \'' . $param['name'] . '_' . $param['index'] . '\')">[删除]</a></li>';
            }
        }

        $class = 'width:100px;display:inline;';
        if (layadmin()) {
            $class .= 'padding-left:0px;';
        }

        $upload = self::uploadHandle($param);

        $button = $upload[2][0] . '<input type="'.$upload[1].'" style="'.$class.$upload[3].'" class="form-control image_upload" '.$upload[0].' t="file" mul="yes" v="' . $param['name'] . '_' . $param['index'] . '" id="file_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="本地上传"/>' . $upload[2][1];
        return $button .'

        <input type="button" style="'.$class.'" class="form-control upload_show" t="file" mul="yes" v="' . $param['name'] . '_' . $param['index'] . '" id="file_' . $param['name'] . '_' . $param['index'] . '" key="' . $param['key'] . '" value="文件库"/>

        <div style="margin-top:10px;display:none;" class="layui-progress layui-progress-big" id="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-filter="file_' . $param['name'] . '_' . $param['index'] . '_progress" lay-showPercent="true">
          <div class="layui-progress-bar" lay-percent="0%"></div>
        </div>

        <ul style="margin:10px;" update_type="html"  class="update_value ' . $param['name'] . '_' . $param['index'] . '_mul">' . $file . '</ul>

        <input type="hidden" class="update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" id="' . $param['name'] . '_' . $param['index'] . '" value="' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '">';
        //return '<input type="file" class="form-control" value="' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '" name="' . $param['name'] . '"/>';
    }

    /**
     * editor
     *
     * @return string
     */
    public static function _editor($param)
    {
        $width = '100%';
        $html = '';
        if (isset($param['editor'])) {
            $width = '70%';
            $html .= '<div class="layui-col-lg6" style="display:inline;position: absolute;left: 740px;"><div><label style="font-size:14px;font-weight:bold;" class="">'.$param['editor']['name'].'</label></div><div>';

            foreach ($param['editor']['button'] as $k => $v) {
                if (isset($v['type'])) {
                    if ($v['type'] == 'image') {
                        $param['key'] = $v['key'];
                    } elseif ($v['type'] == 'media') {
                        $param['media'] = $v['key'];
                    }

                    $v['attr'] = 'editor_tool" tool="' . $v['type'];
                    
                } else {
                    $v['attr'] = 'upload_show_editor';
                }


                if (isset($v['type']) && $v['type'] == 'update') {

                    $v['attr'] = 'update_show_editor';
                    $v['key'] = Dever::url($v['key']);

                    $html .= '<input type="button" style="width:100px;padding-left:0px;margin:5px;" class="form-control layui-input '.$v['attr'].'" editor_id="' . $param['name'] . '_editor" key="'.$v['key'].'" value="'.$v['name'].'">';
                } else {
                    $html .= '<input type="button" style="width:100px;display:inline;padding-left:0px;margin:5px;" class="form-control layui-input '.$v['attr'].'" editor_id="' . $param['name'] . '_editor" key="'.$v['key'].'" value="'.$v['name'].'">';
                }
                
            }



            $html .= '</div></div>';
        }

        $upload = self::uploadHandle($param);

        return $html . '<textarea '.$upload[0].' style="'.$width.';display: inline"  update_type="value" class=" update_value editor" id="' . $param['name'] . '_editor" key="' . (isset($param['key']) ? $param['key'] : 1) . '" media="' . (isset($param['media']) ? $param['media'] : "") . '" name="' . $param['name'] . '" rows="18" cols="100" >' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '</textarea>';
    }

    /**
     * markdown
     *
     * @return string
     */
    public static function _markdown($param)
    {
        return '<textarea style="height:300px;" update_type="value" key="' . (isset($param['key']) ? $param['key'] : 1) . '" class="maze-note update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" >' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '</textarea>';
    }

    /**
     * selector
     *
     * @return string
     */
    public static function _selector($param, $type = '')
    {
        $html = '';

        if (isset($param['key']) && strpos($param['key'], '.css') !== false) {
            $param['key'] = '<link rel="stylesheet" href="' . $param['key'] . '"/>';
        }

        if (isset($param['option'])) {
            foreach ($param['option'] as $k => $v) {
                $check = '';

                if ((isset($param['value']) && $param['value'] == $k) || (empty($param['value']) && isset($param['default']) && $param['default'] == $k)) {
                    $check = 'checked';
                }

                if (is_array($v)) {
                    $value = $v['name'];
                } else {
                    $value = $v;
                }

                $html .= '<a href="javascript:;" onclick="selector($(this))" class="' . $param['name'] . '_selector selector" data-check="' . $check . '" data-name="' . $param['name'] . '" data-value="' . $k . '">' . $value . '</a>&nbsp;&nbsp;';
            }

            $html .= self::hidden($param);
        }
        return '<div class="am-btn-group" >' . $html . '</div>';
    }

    /**
     * radio
     *
     * @return string
     */
    public static function _radio($param, $type = '')
    {
        $html = '';

        if (isset($param['option'])) {
            if (isset($param['option']['state']) && $param['option']['state'] >= 1) {
                if (isset($param['value'])) {
                    $param['value'] = explode(',', $param['value']);
                } else {
                    $param['value'] = array();
                }
                # 处理比较复杂的多维数组
                unset($param['option']['state']);
                $j = -1;
                foreach ($param['option'] as $k => $v) {
                    $check = '';
                    if (isset($param['update_parent'])) {
                        $input_type = $param['update_parent'];
                    } else {
                        $input_type = $param['update'];
                    }

                    //{check}
                    $html .= '<input update_type="checked" class="update_value checkbox-checkall ' . (isset($param['valid']) ? $param['valid'] : '') . '" type="' . $input_type . '" name="temp[]" value="'.$param['name'] . '-' . $k . '"/> -----【' . $v['name'] . '】-----&nbsp;&nbsp;<br />';

                    $span = array('', '');

                    if (isset($v['child']) && $v['child']) {
                        //$i = 1;
                        
                        $html .= '<ul style="list-style:none;margin:0px;display:;" class="">';
                        foreach ($v['child'] as $ki => $vi) {
                            if (!is_array($vi)) {
                                $temp = $vi;
                                $vi = array();
                                $vi['name'] = $temp;
                                $vi['id'] = $ki;
                            }
                            if (isset($v['url'])) {
                                $span = array('<span class="edit" data-url="' . Dever::url($v['url'] . '?id=' . $vi['id']) . '">', '</span>');
                            }
                            $check = '';
                            $disabled = 'disabled="true"';
                            if (isset($param['value']) && in_array($ki, $param['value'])) {
                                $check = 'checked';
                                $disabled = '';
                                $j++;
                            } elseif (isset($param['default']) && is_array($param['default']) && (in_array($k . '_' . $ki, $param['default']))) {
                                $check = 'checked';
                                $disabled = '';
                                $j++;
                            }

                            $input = $input_value = '';

                            if (isset($param['update_input'])) {
                                if (isset($param['update_input_data']) && isset($param['update_input_data'][$j])) {
                                    $input_value = $param['update_input_data'][$j];
                                    if ($disabled) {
                                        $input_value = '';
                                    }
                                } else {
                                    $input_value = isset($param['update_input_default']) ? $param['update_input_default'] : '';
                                }

                                if ($param['update_input']) {
                                    $input = '[' . $param['update_input'] . ']';
                                }

                                $input = $input . '： <input class="form-control" name="' . $param['name'] . '_input[]" type="text" value="' . $input_value . '" style="width:20%;margin:5px;display:inline" placeholder="' . $param['update_input'] . '" ' . $disabled . '/>';
                            }

                            $html .= '<li><input update_type="checked" class="update_value checkbox-checkall-' . $param['name'] . '-'. $k . ' ' . (isset($param['valid']) ? $param['valid'] : '') . '" type="' . $param['update'] . '" name="' . $param['name'] . '[]" value="' . $ki . '" ' . $check . '/> ' . $span[0] . $vi['name'] . $span[1] . '&nbsp;&nbsp;' . $input . '</li>';

                            //++$i;
                        }
                        $html .= '</ul>';
                    }

                    $html .= '<br />';
                }
            } else {
                $js = '';
                if (isset($param['control']) && is_string($param['control'])) {
                    //print_r($param);die;
                    $js = ' show_id="' . $param['control'] . '" onclick="inputShow($(this), \'' . $param['control'] . '\', \'input\')"';
                } elseif (isset($param['bind'])) {
                    $param['bind'][2] = isset($param['bind'][2]) ? str_replace('"', '\'', json_encode($param['bind'][2], true)) : false;
                    $js = $param['bind'][0] . '="' . $param['bind'][1] . '(\'' . $param['name'] . '\', $(this), ' . $param['bind'][2] . ')" ';

                    $js .= ' onload="' . $param['bind'][1] . '(\'' . $param['name'] . '\', $(this), ' . $param['bind'][2] . ')" ';
                }

                $param['parent'] = isset($param['parent']) ? $param['parent'] : '0';
                $param['parent'] .= '_' . $param['name'];

                if ($param['update'] == 'checkbox' && empty($param['checkall'])) {
                    $html .= '<input update_type="checked" class="checkbox-checkall ' . $param['name'] . ' update_value" type="' . $param['update'] . '" name="temp_' . $param['name'] . '[]" value="' . $param['parent'] . '"/> 全选 &nbsp;&nbsp;';
                }

                foreach ($param['option'] as $k => $v) {
                    $check = '';
                    if (is_array($v)) {
                        $value = $v['name'];
                        isset($v['id']) && $k = $v['id'];
                    } else {
                        $value = $v;
                    }

                    if (isset($param['bit'][$k])) {
                        if (isset($param['value']) && ($param['value'] & $k) > 0) {
                            $check = 'checked';
                        }
                    } elseif (isset($param['value'])) {
                        if (is_string($param['value']) && strpos(',' . $param['value'] . ',', ',' . $k . ',') !== false) {
                            $check = 'checked';
                        } elseif (is_array($param['value']) && in_array($k, $param['value'])) {
                            $check = 'checked';
                        }
                    } elseif (isset($param['default'])) {
                        if (is_string($param['default']) && strpos(',' . $param['default'], ',' . $k) !== false) {
                            $check = 'checked';
                        } elseif (is_array($param['default']) && in_array($k, $param['default'])) {
                            $check = 'checked';
                        }
                    }

                    $cjs = $js;
                    if (isset($param['control']) && is_string($param['control']) && $param['control'] != $param['name']) {
                        $cjs = str_replace('update_', '', str_replace($param['control'], $param['name'], $js));
                    }
                    $html .= '<input update_type="checked" class="autoload checkbox-checkall-' . $param['parent'] . ' ' . $param['name'] . ' update_value ' . (isset($param['valid']) ? $param['valid'] : '') . '" type="' . $param['update'] . '" name="' . $param['name'] . '[]" value="' . $k . '" ' . $check . ' ' . $cjs . '/> ' . $value . ' &nbsp;&nbsp;';

                    if (isset($param['bind'])) {
                        //$html .= '<script>$(document).ready(function(){$(".checkbox-checkall-'.$param['parent'].'").click();})</script>';
                    }
                }
            }
        }
        return '<div class="am-btn-group" >' . $html . '</div>';
    }

    /**
     * group
     *
     * @return string
     */
    public static function _group($param, $class = '', $hidden = '', $change = '')
    {
        $html = $class ? '<div class="' . $class . '">' : '';

        $html .= $hidden . '<select class="update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '"  ' . $change . '>';

        if (isset($param['option'])) {
            foreach ($param['option'] as $k => $v) {
                if ($v['id'] <= 0) {
                    $html .= '<option value="0" selected>' . $v['name'] . '</option>';
                } else {
                    $html .= '<optgroup label="' . $v['name'] . '">';

                    if (isset($v['child'])) {
                        foreach ($v['child'] as $k1 => $v1) {
                            $selected = '';

                            if ((isset($param['value']) && $param['value'] == $v1['id']) || (empty($param['value']) && isset($param['default']) && $param['default'] == $v1['id'])) {
                                $selected = 'selected';
                            }
                            $html .= '<option value="' . $v1['id'] . '" ' . $selected . '>' . $v1['name'] . '</option>';
                        }
                    }

                    $html .= '</optgroup>';
                }
            }
        }

        $html .= '</select>';

        $html .= $class ? '</div>' : '';

        return $html;
    }

    /**
     * linkage select 多级联动
     *
     * @return string
     */
    public static function _linkage($param, $class = '', $hidden = '', $change = '')
    {
        $div = 'div';
        $w = 'width:20%;';
        if ($class == 'span') {
            $div = 'span';
            $w = '';
        }

        if (!isset($param['search_state'])) {
            $param['search_state'] = false;
        }

        # 当前级别
        $level = 1;
        # 传入的参数
        $level_param = '';
        if (isset($param['linkage'])) {
            $level_param = $param['linkage'];
        }
        
        $id = 'dever-linkage-' . $param['name'];

        $html = '<' . $div . ' class="' . $class . '" id="' . $id . '" >';

        # 默认值
        if (isset($param['value']) && is_array($param['value'])) {
            $param['value'] = implode(',', $param['value']);
        }

        $html .= '<script>function linkage_' . $param['name'] . '(level){select_linkage(level, "' . $id . '","' . $param['option'] . '", "' . $param['name'] . '", "' . (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : '')) . '", "' . (isset($param['valid']) ? $param['valid'] : '') . '", "' . $w . '", false, "'.$param['search_state'].'", "'.$level_param.'")};';

        $html .= '$(document).ready(function(){linkage_' . $param['name'] . '(' . $level . ')});</script>';

        $html .= '</' . $div . '>';

        return $html;
    }

    /**
     * select
     *
     * @return string
     */
    public static function _selects($param, $class = '', $hidden = '', $change = '')
    {
        return self::_select($param, $class, $hidden, $change);
    }

    /**
     * select
     *
     * @return string
     */
    public static function _select($param, $class = '', $hidden = '', $change = '')
    {
        $html = $class ? '<div class="' . $class . '">' : '';

        $child_class = '';
        if (isset($param['child'])) {
            $child_param['name'] = 'update_' . $param['child_name'];
            isset($param['value']) && $param['value'] = $param['value'];
            isset($param['child_value']) && $child_param['value'] = $param['child_value'];

            $change = 'style="width:45%" id="' . $param['name'] . '" data-child="' . $child_param['name'] . '_child" ';
            $child_class = ' change';
            //$param['name'] .= '_parent';
        }

        if (isset($param['control']) && is_string($param['control'])) {
            //print_r($param);die;
            $change = ' show_id="' . $param['control'] . '" onchange="inputShow($(this), \'' . $param['control'] . '\')"';
        } elseif (isset($param['bind'])) {
            $param['bind'][2] = isset($param['bind'][2]) ? str_replace('"', '\'', json_encode($param['bind'][2], true)) : false;
            $change = $param['bind'][0] . '="' . $param['bind'][1] . '($(this), ' . $param['bind'][2] . ')" ';

            $child_class .= ' change';
        }
        $xm = '';

        if ($change) {
            $xm = '';
        } else {
            
        }
        
        if (!$change && isset($param['update'])) {
            $xm = 'xm-select="' . $param['name'] . '" xm-select-skin="normal" xm-select-search="" xm-select-search-type="dl"';

            if ($param['update'] != 'selects') {
                $xm .= ' xm-select-radio=""';
            }

            $search_value = '';
            if (isset($param['value'])) {
                $search_value = $param['value'];
            } elseif (isset($param['default'])) {
                $search_value = $param['default'];
            }

            if (isset($param['update_search'])) {
                $xm = str_replace('xm-select-search=""', 'xm-select-search="'.Dever::url($param['update_search'] . '&yes='.$search_value.'&json=1').'"', $xm);
            }

            if (isset($param['update_create'])) {
                $xm .= ' xm-select-create=""';
            }

            if ($param['match'] != 'option') {
                $xm .= ' lay-verify="' . $param['name'] . '"';
            }
        } else {
            $xm .= ' xm-select-radio=""';
        }

        if (isset($param['option']) && isset($param['update']) && $param['update'] == 'select') {
            $total = count($param['option']);
            if ($total <= 5) {
                $xm = '';
            }
        }
        
        $html .= $hidden . '<select '.$xm.' class="update_value form-control ' . $child_class . '  ' . (isset($param['valid']) ? $param['valid'] : '') . '" name="' . $param['name'] . '" id="' . $param['name'] . '"  ' . $change . '>';

        if (isset($param['option'])) {
            if (isset($param['lang']) && $param['lang']) {
                $html .= '<option value="0" selected>' . $param['lang'] . '</option>';
            }
            foreach ($param['option'] as $k => $v) {
                if (is_array($v)) {
                    $value = $v['name'];
                    isset($v['id']) && $k = $v['id'];
                } else {
                    $value = $v;
                }
                $check = '';

                if ((isset($param['value']) && strstr(','. $param['value'] . ',', ',' . $k . ',')) || (isset($param['bit'][$k]) && isset($param['value']) && ($param['value'] & $k) > 0) || (empty($param['value']) && isset($param['default']) && strstr(',' . $param['default'] . ',', ',' . $k . ','))) {
                    $check = 'selected';
                }

                $parent = '';
                if (isset($param['parent']) && $param['parent'] && isset($v[$param['parent']])) {
                    $parent = $v[$param['parent']];
                }

                if (is_array($v)) {
                    $value = isset($v['name']) ? $v['name'] : $v['lang'];
                } else {
                    $value = $v;
                }

                $html .= '<option parent="'.$parent.'" value="' . $k . '" ' . $check . '>' . $value . '</option>';
            }
        }

        $html .= '</select>';

        if (isset($param['child'])) {
            $child = $child_param['name'];
            $hidden = self::hidden(array('name' => $child, 'class' => $child . '_child_value'));
            foreach ($param['child'] as $k => $v) {
                $child_param['name'] = 'child-' . $k;
                $child_param['option'] = $v;

                $html .= self::select($child_param, $child . '_child ' . $child . '_child_' . $k . '" parent="' . $k . '" style="display:none;margin-top: -35px;margin-left: 47%;"', $hidden);
            }
        }

        $html .= $class ? '</div>' : '';
        return $html;
    }

    /**
     * radio
     *
     * @return string
     */
    public static function _checkbox($param)
    {
        return self::radio($param);
    }

    /**
     * text
     *
     * @return string
     */
    public static function _text($param, $class = '')
    {
        if ($class) {
            return '<div class="">' . self::text($param, false) . '</div>';
        } else {
            $auto = $input = '';
            $value = (isset($param['value']) ? $param['value'] : (isset($param['default']) ? $param['default'] : ''));
            if (isset($param['autocomplete'])) {
                if (empty($param['autocomplete'][1])) {
                    $param['autocomplete'][1] = 'value';
                }
                if ($param['autocomplete'][1] == 'id') {
                    $input = self::hidden(array('name' => $param['name'], 'value' => $value));
                    $input = '<script>var autocomplete_value = {};autocomplete_value[\''.$param['name'].'_value\'] = [];</script>';
                    if (isset($param['autocomplete'][2]) && $value) {
                        # 重新定义value
                        $value = Dever::load($param['autocomplete'][2], $value);
                        if (is_array($value)) {
                            
                            $html = '<div style="margin-top: 10px;width: 60%;overflow: auto;max-height: 500px;"><table id="'.$param['name'].'_value_show" class="table table-small-font table-bordered">';
                            foreach ($value as $k => $v) {

                                $link = '';
                                $attr = '';
                                $pic = '';
                                if (isset($v['pic']) && $v['pic']) {
                                    $pic = '<a href="javascript:;"><img src="'.$v['pic'].'" width="100"></a>&nbsp;&nbsp;';
                                }
                                if (isset($v['link']) && $v['link']) {
                                    $link = $v['link'];
                                    $attr = ' target="_blank"';
                                }

                                $html .= '<tr id="autocomplate_check_'.$param['name'].'_value"><td width="90%"><input type="checkbox" checked name="'.$param['name'].'[]" value="'.$v['id'].'"/>&nbsp;&nbsp;'.$pic.'<a href="'.$link.'" '.$attr.' title="'.$v['value'].'">'.$v['value'].'</td><td width="10%"><a href="javascript:;" onclick="autocomplete_del($(this), \''.$param['name'].'_value\', \''.$v['id'].'\', \''.$param['name'].'\')">[删除]</a></td></tr><script>autocomplete_value[\''.$param['name'].'_value\'].push('.$v['id'].');</script>';
                            }

                            $html .= '</table></div>';

                            $input .= $html;

                            $value = '';
                        }
                    }

                    $param['name'] = $param['name'] . '_temp';
                }
                $auto = 'complete="' . Dever::url($param['autocomplete'][0]) . '" complete_callback="' . $param['autocomplete'][1] . '" ';

                if (isset($param['autocomplete'][3])) {
                    $auto .= ' complete_cate="' . $param['autocomplete'][3] . '" ';
                }
            } elseif (isset($param['searchbox'])) {
                if (empty($param['searchbox'][1])) {
                    $param['searchbox'][1] = 'value';
                }
                if ($param['searchbox'][1] == 'id') {
                    $input = self::hidden(array('name' => $param['name'], 'value' => $value));
                    $input = '<script>var searchbox_value = [];searchbox_value[\''.$param['name'].'_value\'] = [];</script>';
                    if (isset($param['searchbox'][2]) && $value) {
                        # 重新定义value
                        $value = Dever::load($param['searchbox'][2], $value);
                        if (is_array($value)) {
                            
                            $html = '<div style="margin-top: 10px;width: 60%;overflow: auto;max-height: 500px;"><table id="'.$param['name'].'_value_show" class="table table-small-font table-bordered">';
                            foreach ($value as $k => $v) {

                                $link = '';
                                $attr = '';
                                $pic = '';
                                if (isset($v['pic']) && $v['pic']) {
                                    $pic = '<a href="javascript:;"><img src="'.$v['pic'].'" width="100"></a>&nbsp;&nbsp;';
                                }
                                if (isset($v['link']) && $v['link']) {
                                    $link = $v['link'];
                                    $attr = ' target="_blank"';
                                }

                                $html .= '<tr id="searchbox_check_'.$param['name'].'_value_'.$v['id'].'"><td width="90%"><input type="checkbox" checked name="'.$param['name'].'[]" value="'.$v['id'].'"/>&nbsp;&nbsp;'.$pic.'<a href="'.$link.'" '.$attr.' title="'.$v['value'].'">'.$v['value'].'</td><td width="10%"><a href="javascript:;" onclick="searchbox_del($(this), \''.$param['name'].'_value\', \''.$v['id'].'\', \''.$param['name'].'\')">[删除]</a></td></tr><script>searchbox_value[\''.$param['name'].'_value\'].push('.$v['id'].');</script>';
                            }

                            $html .= '</table></div>';

                            $input .= $html;

                            $value = '';
                        }
                    }

                    $param['name'] = $param['name'] . '_temp';
                }
                $auto = 'searchbox="' . Dever::url($param['searchbox'][0]) . '" searchbox_callback="' . $param['searchbox'][1] . '" ';

                if (isset($param['searchbox'][3])) {
                    $auto .= ' searchbox_cate="' . $param['searchbox'][3] . '" ';
                }

                $search = '';
                if (isset($param['searchbox'][4])) {

                    $temp = explode(',', $param['searchbox'][3]);
                    $link = explode(',', $param['searchbox'][4]);
                    $p = array();
                    $p['option'] = array();
                    foreach ($temp as $k => $v) {
                        $p['name'] = $v;
                        $p['option'] = Dever::load($link[$k]);
                        $search .= self::_select($p, '', '', ' style="width: 15%;display: inline;margin-left:5px;"');
                    }
                }

                $auto .= ' style="display: inline;"';

                $input = $search . '<button type="button" onclick="searchbox(\''.$param['name'].'\', $(this))" class="btn btn-primary layui-btn layuiadmin-btn-list" style="margin-left:10px;margin-top: -2px;"><i class="fa fa-save"></i> 搜索</button>' . $input;
            }
            if (isset($param['bind'])) {
                $param['bind'][2] = isset($param['bind'][2]) ? str_replace('"', '\'', json_encode($param['bind'][2], true)) : false;
                $auto .= $param['bind'][0] . '="' . $param['bind'][1] . '($(this), ' . $param['bind'][2] . ')" ';

                $auto .= ' onload="' . $param['bind'][1] . '($(this), ' . $param['bind'][2] . ')" ';
            }
            
            return '<input ' . $auto . ' type="' . (isset($param['update']) ? $param['update'] : 'text') . '" class="autoload_text update_value ' . (isset($param['class']) ? $param['class'] : 'form-control') . ' ' . (isset($param['valid']) ? $param['valid'] : '') . '" value="' . $value . '" name="' . $param['name'] . '" id="' . $param['name'] . '_value" autocomplete="new'.time().'" placeholder="' . (isset($param['placeholder']) ? $param['placeholder'] : '') . '"/>' . $input;
        }
    }

    /**
     * color
     *
     * @return string
     */
    public static function _color($param)
    {
        $param['class'] = 'dever-color form-control';
        $param['update'] = 'text';
        $param['default'] = '#000000';
        return self::text($param, '');
    }

    /**
     * password
     *
     * @return string
     */
    public static function _password($param)
    {
        $param['value'] = '';
        return self::text($param);
    }

    /**
     * hidden
     *
     * @return string
     */
    public static function _hidden($param)
    {
        $param['update'] = 'hidden';
        return self::text($param, '');
    }

    /**
     * time
     *
     * @return string
     */
    public static function _time($param)
    {
        return '<input type="text" value="' . ((isset($param['value']) && $param['value'] > 0) ? date('Y-m-d', $param['value']) : ((isset($param['default']) && $param['default'] > 0) ? date('Y-m-d', $param['default']) : '')) . '" placeholder="' . (isset($param['lang']) ? $param['lang'] : '') . '" name="' . $param['name'] . '" class="manage_time update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" autocomplete="off"/>';
    }

    /**
     * date
     *
     * @return string
     */
    public static function _date($param)
    {
        return '<input type="text" value="' . ((isset($param['value']) && $param['value'] > 0) ? date('Y-m-d H:i:s', $param['value']) : ((isset($param['default']) && $param['default'] > 0) ? date('Y-m-d H:i:s', $param['default']) : '')) . '" placeholder="' . (isset($param['lang']) ? $param['lang'] : '') . '" name="' . $param['name'] . '" class="manage_date update_value form-control ' . (isset($param['valid']) ? $param['valid'] : '') . '" autocomplete="off"/>';
    }

    /**
     * div
     *
     * @return string
     */
    public static function _node($param, $type = 'div')
    {
        $result = array('', '');
        foreach ($param as $k => $v) {
            $result[0] .= '<' . $type . ' ' . $v . '>';
            $result[1] .= '</' . $type . '>';
        }
        return $result;
    }
}
