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

class c_myphrases extends Admin{

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

	}

	//添加
	public function save(){

		$msg = ForceStringFrom('msg');
		$lang = ForceIntFrom('lang');

		if(!$msg) $errors[] = '请填写快捷回复中文内容!';
		if(isset($errors)) Error($errors, '添加快捷回复');

		$myid = $this->admin['aid'];

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

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

		Success('myphrases');
	}

	//添加
	public function add(){

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

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

		$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('myphrases/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()));

		TableFooter();

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


	//批量更新快捷回复
	public function updatemyphrases(){
		$myid = $this->admin['aid'];

		$page = ForceIntFrom('p', 1);   //页码
		$search = ForceStringFrom('s');
		$type = ForceIntFrom('t');
		$order = ForceStringFrom('o');

		if(IsPost('updatemyphrases')){
			$sorts   = $_POST['sorts'];
			$activateds   = $_POST['activateds'];
			$msgs   = $_POST['msgs'];
			$pids = Iif(isset($_POST['pids']), $_POST['pids'], array());

			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' AND aid='$myid'");
			}
		}else{
			$deletepids = Iif(isset($_POST['deletepids']), $_POST['deletepids'], array());

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

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


	public function index(){
		$myid = $this->admin['aid'];

		$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 'lang.down':
				$orderby = " lang DESC ";
				break;

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

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

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

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

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

		TableRow('<center><form method="post" action="'.BURL('myphrases').'" name="searchmyphrases" style="display:inline-block;"><label>关键字:</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){
			$searchsql = " WHERE (msg LIKE '%$search%') AND aid = '$myid' ";
			$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 aid = '$myid' AND activated = " . Iif($type == 1, 1, 0)." ";
				$title = "全部 <span class=note>" .Iif($type == 1, '可用的快捷回复', '已禁用的快捷回复'). "</span> 列表";
			}
		}else{
			$searchsql = " WHERE aid = '$myid' ";
			$title = '我的全部快捷回复列表';
		}

		$getmyphrases = 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('myphrases/updatemyphrases').'" name="myphrasesform">
		<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="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($getmyphrases)){
				TableRow(array('<input type="hidden" name="pids[]" value="'.$phrase['pid'].'">' . $this->admin['fullname'] . " (ID: $myid)",

				'<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('myphrases'), $totalpages, $page, 10, 's', urlencode($search), 't', $type, 'o', $order));
			}

		}

		TableFooter();

		echo '<div class="submit"><input type="submit" name="updatemyphrases" value="保存更新" class="cancel" style="margin-right:28px"><input type="submit" name="deletemyphrases" 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("myphrases") . FormatUrlParam(array('p'=>$page, 's'=>urlencode($search), 't'=>$type)) . '";

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

	}

} 

?>