<?php 
namespace support\TagLib;
class Html extends \think\template\TagLib{

    /**
     * 定义标签列表
     */
    protected $tags   =  [
        'switch'       => ['attr' => 'name,yes,no,value', 'close' => 0],
        'radio'       => ['attr' => 'name,options,value', 'close' => 0],
        'checkbox'       => ['attr' => 'name,options,value', 'close' => 0],
        'upload'       => ['attr' => 'name,value,mimetype,multiple,url,maxsize,maxcount,params', 'close' => 0]
    ];
    /**
     * 上传
     * @param mixed $tag
     * @return string
     */
    public function tagUpload($tag)
    {
        $value = isset($tag['value'])?$tag['value']:'';unset($tag['value']);
        $name = isset($tag['name'])?$tag['name']:'';unset($tag['name']);
        $mimetype = isset($tag['mimetype'])?$tag['mimetype']:'';unset($tag['mimetype']);
        $multiple = isset($tag['multiple'])?$tag['multiple']:'';unset($tag['multiple']);
        $maxcount = isset($tag['maxcount'])?$tag['maxcount']:'';unset($tag['maxcount']);
        $maxsize = isset($tag['maxsize'])?$tag['maxsize']:'';unset($tag['maxsize']);
        $url = isset($tag['url'])?$tag['url']:'';unset($tag['url']);
        // 支持用函数传数组
        $parseStr = '';
        $name = $this->buildvar($name,$parseStr,'name');
        $value = $this->buildvar($value,$parseStr,'value');
        $mimetype = $this->buildvar($mimetype,$mimetype,'mimetype');
        $multiple = $this->buildvar($multiple,$multiple,'multiple');
        $maxcount = $this->buildvar($maxcount,$maxcount,'maxcount');
        $maxsize = $this->buildvar($maxsize,$maxsize,'maxsize');
        $url = $this->buildvar($url,$url,'url');
        $extendData = $this->buildExtendData($tag);
        $parseStr .= '<input id="c-{'.$name.'}" class="form-control" size="50" name="{'.$name.'}" type="hidden" value="{'.$value.'}">';
        $parseStr .= '<ul class="list-inline clearfix lyear-uploads-pic" data-template="preview" id="p-{'.$name.'}">';
        $parseStr .= '    <li nodelete class="col-xs-4 col-sm-3 col-md-2">';
        $parseStr .= '        <a class="pic-add faupload" style="height: auto;border: 0;"';
        $parseStr .= '            permission="app.admin.files.upload" id="add-pic-btn" href="#!" title="点击上传"';
        $parseStr .= '            data-input-id="c-{'.$name.'}"';
        $parseStr .= '            data-mimetype="{'.$mimetype.'}"';
        $parseStr .= '            data-multiple="{'.$multiple.'}" ';
        $parseStr .= '            data-maxsize="{'.$maxsize.'}" ';
        $parseStr .= '            data-maxcount="{'.$maxcount.'}" ';
        $parseStr .= '            data-url="{'.$url.'}" ';
        $parseStr .= '            '.$extendData.' ';
        $parseStr .= '            data-preview-id="p-{'.$name.'}"></a>';
        $parseStr .= '        <a class="pic-add fachoose" style="height: auto;border: 0;"';
        $parseStr .= '            permission="app.admin.files.list" ';
        $parseStr .= '            id="choose-pic-btn" href="#!" title="选择文件"';
        $parseStr .= '            data-input-id="c-{'.$name.'}" ';
        $parseStr .= '            data-mimetype="{'.$mimetype.'}" ';
        $parseStr .= '            data-multiple="{'.$multiple.'}" ';
        $parseStr .= '            data-maxsize="{'.$maxsize.'}" ';
        $parseStr .= '            data-maxcount="{'.$maxcount.'}" ';
        $parseStr .= '            data-url="{'.$url.'}" ';
        $parseStr .= '            '.$extendData.' ';
        $parseStr .= '            data-preview-id="p-{'.$name.'}"></a>';
        $parseStr .= '    </li>';
        $parseStr .= '</ul>';
        return $parseStr;
    }
    /**
     * switch
     */
    public function tagSwitch($tag)
    {
        $yes = isset($tag['yes'])?$tag['yes']:1;unset($tag['yes']);
        $no = isset($tag['no'])?$tag['no']:0;unset($tag['no']);
        $value = isset($tag['value'])?$tag['value']:1;unset($tag['value']);
        $name = isset($tag['name'])?$tag['name']:'';unset($tag['name']);
        // 支持用函数传数组
        $parseStr = '';
        $name = $this->buildvar($name,$parseStr,'name');
        $value = $this->buildvar($value,$parseStr,'value');
        $no = $this->buildvar($no,$parseStr,'no',0);
        $yes = $this->buildvar($yes,$parseStr,'yes',1);
        $extendData = $this->buildExtendData($tag);
        $parseStr .= '<?php ';
        $parseStr .= $yes.'='.$yes.'??1;'.PHP_EOL;
        $parseStr .= $no.'='.$no.'??0;'.PHP_EOL;
        $parseStr .= $value.'='.$yes.'=='.$value .' ? '.$yes.' : '.$no.';'.PHP_EOL;
        $parseStr .= '?><label data-role="switcher" class="lyear-switch switch-success switch-light switch-outline">';
        $parseStr .= '    <input type="hidden" data-yes="{'.$yes.'}" data-no="{'.$no.'}" id="c-{'.$name.'}" name="{'.$name.'}" value="{'.$value.'}"/>';
        $parseStr .= '    <input id="c-{'.$name.'}-switch" type="checkbox" {if '.$yes .'=='. $value.'}checked{/if} '.$extendData.' />';
        $parseStr .= '    <span></span>';
        $parseStr .= '</label>';
        return $parseStr;
    }
    /**
     * radio
     */
    public function tagRadio($tag)
    {
        $value = isset($tag['value'])?$tag['value']:1;unset($tag['value']);
        $name = isset($tag['name'])?$tag['name']:'';unset($tag['name']);
        $option = isset($tag['options'])?$tag['options']:[];unset($tag['options']);
        // 支持用函数传数组
        $parseStr = '';
        $name = $this->buildvar($name,$parseStr,'name');
        $value = $this->buildvar($value,$parseStr,'value');
        $options = $this->buildvar($option,$parseStr,'option');
        $extendData = $this->buildExtendData($tag);
        $parseStr .= '<?php foreach ('.$options. ' as $_key=>$_val){?>';
        $parseStr .= '<label class="lyear-radio radio-inline radio-primary">';
        $parseStr .= '    <input name="{'.$name.'}" value="{$_key}" id="c-{'.$name.'}-radio" type="radio" {if $_key=='.$value.'}checked{/if} '.$extendData.' />';
        $parseStr .= '    <span>{$_val}</span>';
        $parseStr .= '</label>';
        $parseStr .= '<?php }?>';
        return $parseStr;
    }
    /**
     * checkbox
     */
    public function tagCheckbox($tag)
    {
        $value = isset($tag['value'])?$tag['value']:1;unset($tag['value']);
        $name = isset($tag['name'])?$tag['name']:'';unset($tag['name']);
        $option = isset($tag['options'])?$tag['options']:[];unset($tag['options']);
        // 支持用函数传数组
        $parseStr = '';
        $name = $this->buildvar($name,$parseStr,'name');
        $value = $this->buildvar($value,$parseStr,'value');
        $options = $this->buildvar($option,$parseStr,'option');
        $extendData = $this->buildExtendData($tag);
        $parseStr .= '<?php foreach ('.$options. ' as $_key=>$_val){?>';
        $parseStr .= '<label class="lyear-checkbox checkbox-inline checkbox-primary">';
        $parseStr .= '    <input name="{'.$name.'}[]" value="{$_key}" id="c-{'.$name.'}-{$_key}" type="checkbox" {if in_array($_key,'.$value.')}checked{/if} '.$extendData.' />';
        $parseStr .= '    <span>{$_val}</span>';
        $parseStr .= '</label>';
        $parseStr .= '<?php }?>';
        return $parseStr;
    }

    protected function buildvar($name='',&$parseStr='',$defaultName='',$default=''){
        $var  = '$'.$defaultName.'_' . uniqid();
        $parseStr .= '<?php ';
        if(null == $name){
            $parseStr .= $var . '="'.$default.'"; '.PHP_EOL;
        }elseif (in_array(substr($name, 0, 1),[':','$'])) {
            $name = $this->autoBuildVar($name);
            $parseStr .= $var . '=' . $name . '; '.PHP_EOL;
        }else{
            $parseStr .= $var . '="' . $name . '"; '.PHP_EOL;
        }
        $parseStr .='?>';
        return $var;
    }
    protected function buildExtendData($arr=[]){
        $parseStr = '';
        foreach($arr as $key=>$val){
            $parseStr .= $key . '="' . $val . '"';
        }
        return $parseStr;
    }
    
}