<?php

include("../includes/common.php");

if($islogin==1){}else exit('{"code":-3,"msg":"No Login"}');

$act=isset($_GET['act'])?daddslashes($_GET['act']):null;

@header('Content-Type: application/json; charset=UTF-8');

if(!checkRefererHost())exit('{"code":403}');

switch($act){
	case 'sort':
		$ids = $_POST['ids'];
		if(!is_array($ids))exit('{"code":-1,"msg":"参数错误"}');
		foreach($ids as $k=>$v){
			$k = intval($k);
			$v = intval($v);
			$DB->exec("UPDATE `pre_class` SET `sort`=:sort WHERE `cid`=:cid", [':sort'=>$k,':cid'=>$v]);
		}
		exit('{"code":0,"msg":"排序保存成功"}');
		break;
	case 'get':
		$rs=$DB->query("SELECT a.*,b.name as parent_name FROM pre_class a LEFT JOIN pre_parent_class b ON a.parent_cid=b.cid WHERE 1 order by sort asc");
		$data = array();
		while($res = $rs->fetch(PDO::FETCH_ASSOC)){
			$data[] = $res;
		}
		exit(json_encode(['code'=>0, 'msg'=>'succ', 'data'=>$data]));
		break;
	case 'getParentClass':
		$rs=$DB->query("SELECT * FROM pre_parent_class ORDER BY sort ASC");
		$data = array();
		while($res = $rs->fetch(PDO::FETCH_ASSOC)){
			$data[] = $res;
		}
		exit(json_encode(['code'=>0, 'msg'=>'succ', 'data'=>$data]));
		break;
	case 'get_class':
		$cid=intval($_GET['cid']);
		$row=$DB->getRow("SELECT * FROM pre_class WHERE cid='$cid' LIMIT 1");
		if(!$row) 
			exit('{"code":-1,"msg":"分类不存在"}');
		$rs=$DB->query("SELECT * FROM pre_parent_class ORDER BY sort ASC");
		$parent_class = array();
		while($res = $rs->fetch(PDO::FETCH_ASSOC)){
			$parent_class[] = $res;
		}
		$result = ['code'=>0, 'msg'=>'succ', 'data'=>$row, 'parent_class'=>$parent_class];
		exit(json_encode($result));
		break;
	case 'add':
		$parent_cid=intval($_POST['parent_cid']);
		$name=trim($_POST['name']);
		$sort=intval($_POST['sort']);
		if($name==NULL) exit('{"code":-1,"msg":"分类名称不能为空"}');
		if($DB->exec("INSERT INTO `pre_class` (`parent_cid`,`name`,`sort`,`active`) VALUES ('{$parent_cid}','{$name}','{$sort}','1')")!==false)
			exit('{"code":0,"msg":"添加分类成功！"}');
		else
			exit('{"code":-1,"msg":"添加分类失败！'.$DB->error().'"}');
		break;
	case 'edit':
		$cid=intval($_POST['cid']);
		$parent_cid=intval($_POST['parent_cid']);
		$name=trim($_POST['name']);
		$sort=intval($_POST['sort']);
		if($name==NULL) exit('{"code":-1,"msg":"分类名称不能为空"}');
		if($DB->exec("UPDATE `pre_class` SET `parent_cid`='{$parent_cid}',`name`='{$name}',`sort`='{$sort}' WHERE `cid`='{$cid}'")!==false)
			exit('{"code":0,"msg":"修改分类成功！"}');
		else
			exit('{"code":-1,"msg":"修改分类失败！'.$DB->error().'"}');
		break;
	case 'delete':
		$cid=intval($_GET['cid']);
		$DB->beginTransaction();
		try{
			$DB->exec("DELETE FROM pre_tools WHERE cid='$cid'");
			if($DB->exec("DELETE FROM pre_class WHERE cid='$cid'")!==false){
				$DB->commit();
				exit('{"code":0,"msg":"删除分类及其商品成功！"}');
			}else{
				$DB->rollBack();
				exit('{"code":-1,"msg":"删除分类失败！'.$DB->error().'"}');
			}
		}catch(Exception $e){
			$DB->rollBack();
			exit('{"code":-1,"msg":"删除失败！'.$e->getMessage().'"}');
		}
		break;
	case 'addClass':
		adminpermission('shop', 2);
		$name=trim($_POST['name']);
		if($name==null)
			exit('{"code":-1,"msg":"分类名不能为空"}');
		$rows=$DB->getRow("select * from pre_class where name='$name' limit 1");
		if($rows)
			exit('{"code":-1,"msg":"当前分类名称已存在"}');
		$sort = $DB->getColumn("select sort from pre_class order by sort desc limit 1");
		$sql="insert into `pre_class` (`name`,`sort`,`active`) values ('".$name."','".($sort+1)."','1')";
		if($DB->exec($sql)!==false){
			$cid=$DB->lastInsertId();
			exit('{"code":0,"msg":"添加分类成功！"}');
		}else
			exit('{"code":-1,"msg":"添加分类失败！'.$DB->error().'"}');
		break;
	case 'editClass':
		adminpermission('shop', 2);
		$cid=intval($_GET['cid']);
		$rows=$DB->getRow("select * from pre_class where cid='$cid' limit 1");
		if(!$rows)
			exit('{"code":-1,"msg":"分类不存在"}');
		$name=$_POST['name'];
		if($name==null)
			exit('{"code":-1,"msg":"分类名不能为空"}');
		if($DB->exec("update pre_class set name='$name' where cid='{$cid}'")!==false)
			exit('{"code":0,"msg":"修改分类成功！"}');
		else
			exit('{"code":-1,"msg":"修改分类失败！'.$DB->error().'"}');
		break;
	case 'delClass':
		adminpermission('shop', 2);
		$cid=intval($_GET['cid']);
		$DB->exec("DELETE FROM pre_tools WHERE cid='$cid'");
		$sql="DELETE FROM pre_class WHERE cid='$cid'";
		if($DB->exec($sql)!==false)
			exit('{"code":0,"msg":"删除分类成功！"}');
		else
			exit('{"code":-1,"msg":"删除分类失败！'.$DB->error().'"}');
		break;
	case 'editClassAll':
		adminpermission('shop', 2);
		foreach($_POST['name'] as $cid=>$name){
			if(isset($_POST['sort'][$cid]) && $_POST['sort'][$cid]>0){
				$sort = intval($_POST['sort'][$cid]);
				$DB->exec("update pre_class set name='$name',sort='$sort' where cid='{$cid}'");
			}else{
				$DB->exec("update pre_class set name='$name' where cid='{$cid}'");
			}
		}
		exit('{"code":0,"msg":"修改分类成功！"}');
		break;
	case 'editClassImages':
		adminpermission('shop', 2);
		foreach($_POST['img'] as $k=>$v){
			$DB->exec("update pre_class set shopimg='$v' where cid='{$k}'");
		}
		exit('{"code":0,"msg":"修改分类成功！"}');
		break;
	case 'getClassImage':
		$cid=intval($_GET['cid']);
		$rows=$DB->getRow("select shopimg from pre_tools where cid='$cid' and shopimg is not null limit 1");
		if(!$rows)
			exit('{"code":-1,"msg":"分类不存在"}');
		exit('{"code":0,"msg":"succ","url":"'.$rows['shopimg'].'"}');
		break;
	case 'setBlock':
		adminpermission('shop', 2);
		$cid=intval($_POST['cid']);
		$data=trim($_POST['data']);
		if($DB->exec("update pre_class set block='$data' where cid='{$cid}'")!==false)
			exit('{"code":0,"msg":"设置成功"}');
		else
			exit('{"code":-1,"msg":"设置失败！'.$DB->error().'"}');
		break;
	case 'getBlock':
		$cid=intval($_POST['cid']);
		$rows=$DB->getRow("select * from pre_class where cid='$cid' limit 1");
		if(!$rows)
			exit('{"code":-1,"msg":"当前分类不存在！"}');
		$result=array("code"=>0,"msg"=>"succ","data"=>$rows['block']);
		exit(json_encode($result));
		break;
	case 'setBlockPay':
		adminpermission('shop', 2);
		$cid=intval($_POST['cid']);
		$paytype=implode(',',$_POST['paytype']);
		if($DB->exec("update pre_class set blockpay='$paytype' where cid='{$cid}'")!==false)
			exit('{"code":0,"msg":"设置成功"}');
		else
			exit('{"code":-1,"msg":"设置失败！'.$DB->error().'"}');
		break;
	case 'getBlockPay':
		$cid=intval($_POST['cid']);
		$rows=$DB->getRow("select * from pre_class where cid='$cid' limit 1");
		if(!$rows)
			exit('{"code":-1,"msg":"当前分类不存在！"}');
		$result=array("code"=>0,"msg"=>"succ","data"=>explode(',',$rows['blockpay']));
		exit(json_encode($result));
		break;
	case 'setClass': //分类上下架
		adminpermission('shop', 2);
		$cid=intval($_GET['cid']);
		$active=intval($_GET['active']);
		$DB->exec("update pre_class set active='$active' where cid='{$cid}'");
		exit('{"code":0,"msg":"succ"}');
		break;
	case 'setClassSort': //排序操作
		adminpermission('shop', 2);
		$cid=intval($_GET['cid']);
		$sort=intval($_GET['sort']);
		if(setClassSort($cid,$sort)){
			exit('{"code":0,"msg":"succ"}');
		}else{
			exit('{"code":-1,"msg":"失败"}');
		}
		break;
	case 'setDiscount': //设置商品分类折扣
		adminpermission('shop', 2);
		$cid=intval($_POST['cid']);
		$discount=trim($_POST['discount']);
		if($discount==''){
			$DB->exec("update pre_class set discount=100 where cid='{$cid}'");
		}else{
			$discount=round($discount);
			$DB->exec("update pre_class set discount='$discount' where cid='{$cid}'");
		}
		exit('{"code":0,"msg":"设置折扣成功"}');
		break;
	case 'delete_batch':
		adminpermission('shop', 2);
		$ids = $_POST['ids'];
		if(!is_array($ids))exit('{"code":-1,"msg":"参数错误"}');
		$ids = array_map('intval', $ids);
		$ids = implode(',', $ids);
		$DB->beginTransaction();
		try{
			$DB->exec("DELETE FROM pre_tools WHERE cid IN ($ids)");
			if($DB->exec("DELETE FROM pre_class WHERE cid IN ($ids)")!==false){
				$DB->commit();
				exit('{"code":0,"msg":"批量删除分类及其商品成功！"}');
			}else{
				$DB->rollBack();
				exit('{"code":-1,"msg":"批量删除分类失败！'.$DB->error().'"}');
			}
		}catch(Exception $e){
			$DB->rollBack();
			exit('{"code":-1,"msg":"删除失败！'.$e->getMessage().'"}');
		}
		break;
	case 'move_batch':
		adminpermission('shop', 2);
		$ids = $_POST['ids'];
		$parent_cid = intval($_POST['parent_cid']);
		if(!is_array($ids))exit('{"code":-1,"msg":"参数错误"}');
		$ids = array_map('intval', $ids);
		$ids = implode(',', $ids);
		if($DB->exec("UPDATE pre_class SET parent_cid='$parent_cid' WHERE cid IN ($ids)")!==false)
			exit('{"code":0,"msg":"批量移动分类成功！"}');
		else
			exit('{"code":-1,"msg":"批量移动分类失败！'.$DB->error().'"}');
		break;
	default:
		exit('{"code":-4,"msg":"No Act"}');
		break;
}