<?php if(!defined('ROOT')) die('Access denied.');

class c_phrases extends Admin{

	public function __construct($path){
		parent::__construct($path);

		$this->CheckAction();
	}

	//添加
	public function save(){
		$aids = $_POST['aids'];
		$nums = count($aids);
		$msg = ForceStringFrom('msg');
		$lang = ForceIntFrom('lang');

		if($nums < 1) $errors[] = '请选择所属客服人员!';
		if(!$msg) $errors[] = '请填写快捷回复中文内容!';

		if(isset($errors)) Error($errors, '添加快捷回复');

		for($i = 0; $i < $nums; $i++){
			$aid = ForceInt($aids[$i]);

			if(!$aid) continue;

			APP::$DB->exe("INSERT INTO " . TABLE_PREFIX . "phrase (aid, activated, lang, msg) VALUES ('$aid', 1, '$lang', '$msg')");

			$lastid = APP::$DB->insert_id;
			APP::$DB->exe("UPDATE " . TABLE_PREFIX . "phrase SET sort = '$lastid' WHERE pid = '$lastid'");
		}

		Success('phrases');
	}

	//添加
	public function add(){

		SubMenu('添加快捷回复', array(array('快捷回复列表', 'phrases'), array('添加快捷回复', 'phrases/add', 1)));

		$need_info = '&nbsp;&nbsp;<font class=red>* 必填项</font>';

		$admins = array();
		$admin_list = "<div>";
		$getadmins = APP::$DB->query("SELECT a.aid, a.grid, a.fullname, gr.groupname FROM " . TABLE_PREFIX . "admin a LEFT JOIN " . TABLE_PREFIX . "group gr ON gr.id = a.grid WHERE a.activated=1 ORDER BY a.grid,  a.aid");

		$groupid = 0;

		while($a = APP::$DB->fetch($getadmins)){
			if($a['grid'] <> $groupid){
				$admin_list .= "</div>";
				$groupid = $a['grid'];
				$admin_list .= '<div style="line-height:28px;padding-bottom:10px;"><input name="aids[]" value="0" type="checkbox" class="checkAll_group" id="checkgroup_'. $a['grid'] . '">&nbsp;<label class=greyb style="padding:4px 8px;border-radius:6px;background:#eeeeee;cursor:pointer;" for="checkgroup_'. $a['grid'] . '">' . $a['groupname'] . ':</label><br>';
			}
			$admin_list .= '<input class="son" type="checkbox" value="' . $a['aid'] . '" name="aids[]" id="chbx' . $a['aid'] . '"> <label for="chbx' . $a['aid'] . '" style="margin-right:30px;">' . $a['fullname'] . '</label>';
		}

		$admin_list .= "</div>";

		$Radio = new Radio;
		$Radio->Name = 'lang';
		$Radio->SelectedID = 1;
		$Radio->AddOption(1, '<font class=redb>中文</font>', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
		$Radio->AddOption(0, '<font class=blueb>英文</font>', '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');

		echo '<form method="post" action="'.BURL('phrases/save').'">';

		TableHeader('快捷回复信息:');

		TableRow(array('<b>提示:</b>', '<font class="orange" style="font-size:16px;line-height:26px;">1. 每个客服的快捷回复相互独立, 互不影响<br>2. 每个客服均可在“客服操作窗口”快速添加、删除自己的快捷回复</font>'));

		TableRow(array('<b>短语内容:</b>', '<input type="text" name="msg" value="" size="80">' . $need_info));
		TableRow(array('<b>访客语言</b>', $Radio->Get()));
		TableRow(array('<b>所属客服:</b>&nbsp;&nbsp;&nbsp;<input type="checkbox" id="checkAll" for="aids[]"> <label for="checkAll">全选</label>', $admin_list));

		TableFooter();

		echo '<script type="text/javascript">
			$(function(){
				$(".checkAll_group").click(function(e){
					$(this).parent().find("input[class=\'son\']").prop("checked", $(this).prop("checked"));
				});
			});
		</script>';

		PrintSubmit('添加快捷回复');
	}


	//批量更新快捷回复
	public function updatephrases(){
		$page = ForceIntFrom('p', 1);   //页码
		$search = ForceStringFrom('s');
		$type = ForceIntFrom('t');
		$order = ForceStringFrom('o');

		if(IsPost('updatephrases')){

			if(isset($_POST['pids'])){
				$sorts   = $_POST['sorts'];
				$activateds   = $_POST['activateds'];
				$msgs   = $_POST['msgs'];

				$pids = $_POST['pids'];

				for($i = 0; $i < count($pids); $i++){
					$pid = ForceInt($pids[$i]);
					APP::$DB->exe("UPDATE " . TABLE_PREFIX . "phrase SET sort = '" . ForceInt($sorts[$i]) . "',
						activated = '" . ForceInt($activateds[$i]) . "',
						msg = '" . ForceString($msgs[$i]) . "'	
						WHERE pid = '$pid'");
				}
			}

		}else{
			$deletepids = array();
			if(isset($_POST['deletepids'])) $deletepids = $_POST['deletepids'];

			for($i = 0; $i < count($deletepids); $i++){
				$pid = ForceInt($deletepids[$i]);
				APP::$DB->exe("DELETE FROM " . TABLE_PREFIX . "phrase WHERE pid = '$pid'");
			}
		}

		Success('phrases?p=' . $page. FormatUrlParam(array('s'=>urlencode($search), 't'=>$type, 'o'=>$order)));
	}


	public function index(){
		$NumPerPage = 10;
		$page = ForceIntFrom('p', 1);
		$search = ForceStringFrom('s');
		$type = ForceStringFrom('t');

		if(IsGet('s')) $search = urldecode($search);

		$start = $NumPerPage * ($page-1);

		//排序
		$order = ForceStringFrom('o');
        switch($order)
        {
            case 'activated.down':
				$orderby = " activated DESC ";
				break;

            case 'activated.up':
				$orderby = " activated ASC ";
				break;

            case 'sort.down':
				$orderby = " sort DESC ";
				break;

            case 'sort.up':
				$orderby = " sort ASC ";
				break;

            case 'lang.down':
				$orderby = " lang DESC ";
				break;

            case 'lang.up':
				$orderby = " lang ASC ";
				break;

            case 'aid.up':
				$orderby = " aid ASC, sort DESC ";
				break;

			default:
				$orderby = " aid DESC, sort DESC ";			
				$order = "aid.down";
				break;
		}

		$admins = array();
		$getadmins = APP::$DB->query("SELECT a.aid, a.fullname, gr.groupname FROM " . TABLE_PREFIX . "admin a LEFT JOIN " . TABLE_PREFIX . "group gr ON gr.id = a.grid");
		while($a = APP::$DB->fetch($getadmins)){
			$admins[$a['aid']] = $a;
		}

		SubMenu('快捷回复列表', array(array('快捷回复列表', 'phrases', 1), array('添加快捷回复', 'phrases/add')));

		TableHeader('搜索快捷回复');

		TableRow('<center><form method="post" action="'.BURL('phrases').'" name="searchphrases" style="display:inline-block;"><label>客服ID、关键字:</label>&nbsp;<input type="text" name="s" size="14"  value="'.$search.'">&nbsp;&nbsp;&nbsp;<label>状态:</label>&nbsp;<select name="t"><option value="0">全部</option><option value="1" ' . Iif($type == '1', 'SELECTED') . '>可用</option><option value="2" ' . Iif($type == '2', 'SELECTED') . ' class=red>已禁用</option></select>&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="搜索快捷回复" class="cancel"></form></center>');
		
		TableFooter();

		if($search){
			if(preg_match("/^[1-9][0-9]*$/", $search)){
				$s = ForceInt($search);
				$searchsql = " WHERE aid = '$s' "; //按ID搜索
				$title = "搜索ID号为: <span class=note>$s</span> 的快捷回复";
			}else{
				$searchsql = " WHERE (msg LIKE '%$search%') ";
				$title = "搜索: <span class=note>$search</span> 的快捷回复列表";
			}

			if($type) {
				if($type == 1 OR $type == 2){
					$searchsql .= " AND activated = " . Iif($type == 1, 1, 0)." ";
					$title = "在 <span class=note>" .Iif($type == 1, '可用的快捷回复', '已禁用的快捷回复'). "</span> 中, " . $title;
				}
			}
		}else if($type){
			if($type == 1 OR $type == 2){
				$searchsql = " WHERE activated = " . Iif($type == 1, 1, 0)." ";
				$title = "全部 <span class=note>" .Iif($type == 1, '可用的快捷回复', '已禁用的快捷回复'). "</span> 列表";
			}
		}else{
			$searchsql = '';
			$title = '全部快捷回复列表';
		}

		$getphrases = APP::$DB->query("SELECT * FROM " . TABLE_PREFIX . "phrase ".$searchsql." ORDER BY {$orderby} LIMIT $start,$NumPerPage");

		$maxrows = APP::$DB->getOne("SELECT COUNT(pid) AS value FROM " . TABLE_PREFIX . "phrase ".$searchsql);

		echo '<form method="post" action="'.BURL('phrases/updatephrases').'" name="phrasesform">
		<input type="hidden" name="p" value="'.$page.'">
		<input type="hidden" name="s" value="'.$search.'">
		<input type="hidden" name="t" value="'.$type.'">
		<input type="hidden" name="o" value="'.$order.'">';

		TableHeader($title.'('.$maxrows['value'].'个)');
		TableRow(array('<a class="do-sort" for="aid">所属客服</a>', '客服组', '<a class="do-sort" for="sort">排序</a>', '<a class="do-sort" for="activated">状态</a>', '<a class="do-sort" for="lang">语言</a>', '快捷回复', '<input type="checkbox" id="checkAll" for="deletepids[]"> <label for="checkAll">删除</label>'), 'tr0');

		if($maxrows['value'] < 1){
			TableRow('<center><BR><font class=redb>未搜索到任何快捷回复!</font><BR><BR></center>');
		}else{
			while($phrase = APP::$DB->fetch($getphrases)){
				TableRow(array('<input type="hidden" name="pids[]" value="'.$phrase['pid'].'"><a title="编辑客服" href="' . BURL('users/edit?aid='.$phrase['aid']) . '">' . $admins[$phrase['aid']]['fullname'] . ' (ID: ' . $phrase['aid'] . ')</a>',

				"<font class=grey>" . $admins[$phrase['aid']]['groupname'] . "</font>",

				'<input type="text" name="sorts[]" value="' . $phrase['sort'] . '" size="4">',

				'<select name="activateds[]"' . Iif(!$phrase['activated'], ' class=red'). '><option value="1">可用</option><option class="red" value="0" ' . Iif(!$phrase['activated'], 'SELECTED') . '>禁用</option></select>',

				Iif($phrase['lang'], "中文", "<font class=grey>EN</font>"),

				'<input type="text" name="msgs[]" value="' . $phrase['msg'] . '" size="60">',

				'<input type="checkbox" name="deletepids[]" value="' . $phrase['pid'] . '">'));
			}

			$totalpages = ceil($maxrows['value'] / $NumPerPage);

			if($totalpages > 1){
				TableRow(GetPageList(BURL('phrases'), $totalpages, $page, 10, 's', urlencode($search), 't', $type, 'o', $order));
			}

		}

		TableFooter();

		echo '<div class="submit"><input type="submit" name="updatephrases" value="保存更新" class="cancel" style="margin-right:28px"><input type="submit" name="deletephrases" value="删除快捷回复" class="save" onclick="var _me=$(this);showDialog(\'确定删除所选快捷回复吗?\', \'确认操作\', function(){_me.closest(\'form\').submit();});return false;"></div></form>
		<script type="text/javascript">
			$(function(){
				var url = "' . BURL("phrases") . FormatUrlParam(array('p'=>$page, 's'=>urlencode($search), 't'=>$type)) . '";

				format_sort(url, "' . $order . '");
			});		
		</script>';

	}

} 

?>