<?php

//decode by http://www.yunlu99.com/
namespace App\Lib\DataTable;

class DataTable extends \App\Lib\Facade
{
	private $dataId;
	//表格ID
	private $dataSource = '';
	private $cols = array();
	private $lang = '';
	private $toolbar = -1;
	//array('view','edit','delete');
	private $toolbarCols = array();
	private $priKey = 'id';
	private $enableCheckBoxed = false;
	static function init()
	{
		if (S('dataTable') == -1) {
			S('dataTable', new self());
		}
		return S('dataTable');
	}
	function __construct()
	{
		$this->cols = array(array('type' => 'checkbox'));
		$this->toolbarCols = array('width' => 100, 'align' => 'center', 'toolbar' => '#toolbar', 'title' => trans('common.operation'));
	}
	/**
	 *添加表头字段
	 * [addColsFields description]
	 * @param [type] $field  [description]
	 * @param array  $params [description]
	 */
	public function addColsFields($field, $params = array())
	{
		if (!empty($this->lang)) {
			$title = trans($this->lang . '.' . $field);
		} else {
			$title = $field;
		}
		$cols = array('field' => $field, 'title' => $title, 'sort' => true, 'minWidth' => 120);
		$cols = array_merge($cols, $params);
		$this->addCols($cols);
	}
	/**
	 * 开启checkbox
	 *
	 * @return void
	 */
	function enableCheckBox()
	{
		$this->enableCheckBoxed = true;
	}
	/**
	 * 关闭checkbox
	 *
	 * @return void
	 */
	function disableCheckBox()
	{
		$this->enableCheckBoxed = false;
	}
	/**
	 * 设置主键
	 * [setPriKey description]
	 * @param [type] $id [description]
	 */
	public function setPriKey($id)
	{
		$this->priKey = $id;
	}
	/**
	 *添加表头
	 * [addCols description]
	 * @param [type] $item [description]
	 */
	public function addCols($item)
	{
		$this->cols[] = $item;
	}
	/**
	 *设置左边栏操作
	 * [setToolBar description]
	 * @param string $toolbar [description]
	 */
	public function setToolBar($toolbar = '', $args = array())
	{
		if (empty($toolbar)) {
			$this->toolbar = -1;
			return;
		}
		if (is_array($toolbar) && count($toolbar) < 1) {
			$this->toolbar = -1;
			return;
		}
		$this->toolbar = $toolbar;
		$this->toolbarCols = array_merge($this->toolbarCols, $args);
	}
	/**
	 *设置语言包
	 * [setLang description]
	 * @param [type] $lang [description]
	 */
	public function setLang($lang)
	{
		$this->lang = $lang;
	}
	/**
	 *设置表头
	 * [setCols description]
	 * @param [type] $data [description]
	 */
	public function setCols($data)
	{
		$this->cols[] = $data;
		return $this;
	}
	/**
	 *设置表格id
	 * [setDataId description]
	 * @param [type] $id [description]
	 */
	public function setDataId($id)
	{
		$this->dataId = $id;
		return $this;
	}
	/**
	 *调用数据接口地址
	 * [setDataSource description]
	 * @param [type] $url [description]
	 */
	public function setDataSource($url)
	{
		$this->dataSource = $url;
		return $this;
	}
	/**
	 *返回数据表格的渲染信息
	 * [render description]
	 * @return [type] [description]
	 */
	public function render($arr = array())
	{
		if (empty($this->dataId)) {
			$this->dataId = "demo";
		}
		if (empty($this->dataSource)) {
			$this->dataSource = '/' . S('MODULE') . '/' . S("CONTROLLER") . '/dataSource';
		}
		if (!$this->enableCheckBoxed) {
			unset($this->cols[0]);
		}
		if ($this->toolbar != -1) {
			// $tmp = $this->cols;
			// $this->cols = array();
			$this->cols[] = $this->toolbarCols;
			// $this->cols = array_merge($this->cols, $tmp);
		}
		$data = array('dataId' => $this->dataId, 'dataUri' => $this->dataSource, 'dataCols' => array($this->cols), 'dataToolBar' => $this->toolbar, 'primaryKey' => $this->priKey, 'dataTableLang' => $this->lang);
		$data = array_merge($arr, $data);
		return $data;
	}
	/**
	 *返回json数据
	 * [toJson description]
	 * @param  [type]  $data  [description]
	 * @param  integer $count [description]
	 * @param  integer $code  [description]
	 * @param  string  $msg   [description]
	 * @return [type]         [description]
	 */
	public function toJson($data, $count = 1, $code = 0, $msg = '', $extras = array())
	{
		if (is_array($msg)) {
			$extras = $msg;
			$msg = '';
		}
		return $this->responseToJson(array('code' => $code, 'msg' => $msg, 'count' => $count, 'data' => $data, 'extras' => $extras));
	}
	public function responseToJson($params)
	{
		return json_encode($params);
	}
}