<?php
class message
{
	public function cids($pid = 0, $status = 0)
	{
		if ($status > 0)
			$where = "status=$status";
		else
			$where = "1=1";
		$order = $_POST['order'] != '' ? $_POST['order'] : 'asc';
		$data = db()->select_all("product_type", "id, title, pid", $where . ' order by sort ' . $order . ', id desc');
		$res = reSort($data, $pid, 0, TRUE);
		$arr = [];
		foreach ($res as $key => $value) {
			$arr[$value['id']] = $value['title'];
		}
		return $arr;
	}
	function listwhere($where)
	{
		if ($_POST['name']) {
			$where .= " and `name` like '%" . $_POST['name'] . "%'";
		}
		if ($_POST['tel']) {
			$where .= ' and tel=' . $_POST['tel'];
		}
		if ($_POST['pname']) {
			$pids = db()->select_all('product', 'id', 'title like "%' . $_POST['pname'] . '%"');
			$arr = array();
			foreach ($pids as $v) {
				$arr[] = $v['id'];
			}
			$pids = implode(',', $arr);
			$where .= ' and pid IN (' . $pids . ')';
		}
		if ($_POST['tid']) {
			$pids = db()->select_all('product', 'id', 'cid = '. $_POST['tid']);
			$arr = array();
			foreach ($pids as $v) {
				$arr[] = $v['id'];
			}
			$pids = implode(',', $arr);
			$where .= ' and pid IN (' . $pids . ')';
		}
		if ($_POST['sdate'] || $_POST['edate']) {
			$sday = $_POST['sdate'] ? strtotime($_POST['sdate'] . ' 00:00:00') : 0;
			$eday = $_POST['edate'] ? strtotime($_POST['edate'] . ' 23:59:59') : TIME;
			$where .= ' and addtime>"' . $sday . '" and addtime<"' . $eday . ' "';
		}
		return $where;
	}

	function list($list)
	{
		foreach ($list as $key => $value) {
			$pInfo = db()->get_field("product", $value['pid'], 'title, cid');
			$list[$key]['ptitle'] = $pInfo['title'];
			$list[$key]['pttitle'] = db()->get_field("product_type", $pInfo['cid']);
		}
		return $list;
	}
}
