<?php
/**
 * 对话框相关集
 * 
 */
class mod_mall_dialog extends mod_mall {
	function get_goods() {
		$arr_return = array("list" => array());
		$obj_db = cls_obj::db();
		$arr_where = array();
		$arr_where_s = array();
		$lng_issearch = 0;
		//取排序字段
		$arr_config_info = tab_sys_user_config::get_info(".mall.goods1"  , $this->app_dir );
		$lng_pagesize = $arr_config_info["pagesize"];
		//取分页信息
		$str_where = "";
		$lng_page = (int)fun_get::get("page");
		$sort = " order by detail_id desc";
		//取查询参数
		$arr_search_key = array(
			'key' => fun_get::get("s_key"),
		);
		if( $arr_search_key['key'] != '' ) $arr_where_s[] = "goods_name like '%" . $arr_search_key['key'] . "%' or detail_name like '%" . $arr_search_key['key'] . "%'"; 
		$arr_where = array_merge($arr_where , $arr_where_s);
		if(count($arr_where)>0) $str_where = " where " . implode(" and " , $arr_where);

		$arr_return["pageinfo"] = $obj_db->get_pageinfo(cls_config::DB_PRE."mall_goods a left join " . cls_config::DB_PRE . "mall_goods_detail b on a.goods_id=b.detail_goods_id" , $str_where , $lng_page , $lng_pagesize);

		$obj_result = $obj_db->select("SELECT goods_name,goods_id,detail_id,detail_name,detail_pic,goods_pic_small FROM ".cls_config::DB_PRE."mall_goods a left join " . cls_config::DB_PRE . "mall_goods_detail b on a.goods_id=b.detail_goods_id" . $str_where . $sort . $arr_return['pageinfo']['limit']);
		while( $obj_rs = $obj_db->fetch_array($obj_result) ) {
			$obj_rs["name"] = $obj_rs["goods_name"];
			if(!empty($obj_rs["detail_name"])) $obj_rs["name"] .= " " . $obj_rs["detail_name"];
			$obj_rs["pic"] = $obj_rs["detail_pic"];
			if(empty($obj_rs["pic"])) $obj_rs["pic"] = $obj_rs["goods_pic_small"];
			$arr_return["list"][] = $obj_rs;
		}
		if( count($arr_where_s) > 0 ) $lng_issearch = 1;
		$arr_return['issearch'] = $lng_issearch;
		$arr_return['pagebtns']   = $this->get_pagebtns($arr_return['pageinfo']);
		return $arr_return;
	}

	/* 取当前系统所有店铺：名称与id
	 * 
	 */
	function get_shop() {
		$arr_return = array("list" => array());
		$obj_db = cls_obj::db();
		$arr_where = array();
		$arr_where_s = array();
		$lng_issearch = 0;
		//取排序字段
		$arr_config_info = tab_sys_user_config::get_info(".mall.shop1"  , $this->app_dir );
		$lng_pagesize = $arr_config_info["pagesize"];
		//取分页信息
		$str_where = "";
		$lng_page = (int)fun_get::get("page");
		$sort = " order by shop_id desc";
		//取查询参数
		$arr_search_key = array(
			'key' => fun_get::get("s_key"),
		);
		if( $arr_search_key['key'] != '' ) $arr_where_s[] = "shop_name like '%" . $arr_search_key['key'] . "%' or shop_linkname like '%" . $arr_search_key['key'] . "%' or shop_tel like '%" . $arr_search_key['key'] . "%' or shop_linktel like '%" . $arr_search_key['key'] . "%' or shop_address like '%" . $arr_search_key['key'] . "%'"; 
		$arr_where = array_merge($arr_where , $arr_where_s);
		if(count($arr_where)>0) $str_where = " where " . implode(" and " , $arr_where);

		$arr_return["pageinfo"] = $obj_db->get_pageinfo(cls_config::DB_PRE."mall_shop" , $str_where , $lng_page , $lng_pagesize);

		$obj_result = $obj_db->select("SELECT shop_id,shop_name FROM ".cls_config::DB_PRE."mall_shop" . $str_where . $sort . $arr_return['pageinfo']['limit']);
		while( $obj_rs = $obj_db->fetch_array($obj_result) ) {
			$arr_return["list"][] = $obj_rs;
		}
		if( count($arr_where_s) > 0 ) $lng_issearch = 1;
		$arr_return['issearch'] = $lng_issearch;
		$arr_return['pagebtns']   = $this->get_pagebtns($arr_return['pageinfo']);
		return $arr_return;
	}
	/* 取当指定父级id子分类
	 * 
	 */
	function get_goods_category($pid) {
		$arr_return = array("list" => array() , 'path' => '<a href="javascript:thisjs.go_pid(0);">顶级</a>：' , 'pathval' =>'' , "attribute" => array());
		$obj_db = cls_obj::db();
		$arr_id = array();
		$obj_result = $obj_db->select("select * from " . cls_config::DB_PRE . "mall_goods_category where category_pid='" . $pid . "'");
		while($obj_rs = $obj_db->fetch_array($obj_result)) {
			$arr_id[] = $obj_rs["category_id"];
			$arr_return["list"][] = $obj_rs;
		}
		//取属性
		if(count($arr_id)>0) {
			$str_id = implode("," , $arr_id);
			$arr = array();
			$obj_result = $obj_db->select("select attribute_name,attribute_val,attribute_default,attribute_type,attribute_category_id from " . cls_config::DB_PRE . "mall_goods_category_attribute where attribute_category_id in(" . $str_id . ")");
			while($obj_rs = $obj_db->fetch_array($obj_result)) {
				$obj_rs["attribute_val"] = tab_mall_goods_category_attribute::format_val($obj_rs["attribute_val"]);
				$arr["id_".$obj_rs["attribute_category_id"]]["id"] = $obj_rs["attribute_category_id"];
				$arr["id_".$obj_rs["attribute_category_id"]]["val"][] = array("name"=>$obj_rs["attribute_name"],"list"=>$obj_rs["attribute_val"],"type"=>$obj_rs["attribute_type"],"default"=>$obj_rs["attribute_default"]);
			}
			foreach($arr as $item => $key) {
				$arr_return["attribute"][] = array("id"=> $key['id'], "json" => fun_format::json($key['val']));
			}
		}
		if(!empty($pid)) {
			$arr = tab_mall_goods_category::get_path($pid);
			foreach($arr as $item) {
				$arr_path[] = "<a href=\"javascript:thisjs.go_pid(" . $item['id'] . ");\">" . $item['name'] . "</a>";
				$arr_pathval[] = $item["name"];
			}
			$arr_return["path"] .= implode("->" , $arr_path);
			$arr_return["pathval"] = implode("->" , $arr_pathval);
		}

		return $arr_return;
	}

	function get_brand() {
		$arr_return = array("list" => array());
		$obj_db = cls_obj::db();
		//取排序字段
		$arr_config_info = tab_sys_user_config::get_info(".mall.brand"  , $this->app_dir );
		$lng_pagesize = $arr_config_info["pagesize"];
		//取分页信息
		$str_where = "";
		$lng_issearch = 0;
		$lng_page = (int)fun_get::get("page");
		$sort = " order by brand_sort,brand_id desc";
		$arr_return["pageinfo"] = $obj_db->get_pageinfo(cls_config::DB_PRE."mall_brand" , $str_where , $lng_page , $lng_pagesize);

		$obj_result = $obj_db->select("SELECT * FROM ".cls_config::DB_PRE."mall_brand" . $str_where . $sort . $arr_return['pageinfo']['limit']);
		while( $obj_rs = $obj_db->fetch_array($obj_result) ) {
			$arr_return["list"][] = $obj_rs;
		}
		$arr_return['pagebtns']   = $this->get_pagebtns($arr_return['pageinfo']);
		return $arr_return;
	}

}