<?php
namespace Smi\Controller;
use Think\Controller;
header("Content-Type:text/html;CharSet=UTF-8");
class ManyStoreController extends BaseController
{
	//子门店列表
	public function SubsidiaryStore(){
		$adminId=$_SESSION['admin_id'];
		$where = "parent_id = {$adminId}";

		$data = M('Admin') -> field('id,username,business_name,status,create_time') -> where($where) -> select();

		$this -> assign('data',$data);

		$this -> display('subsidiary_store');

	}

	//添加子门店
	public function AddSubsidiaryStore(){
		if(IS_POST){
			$data['parent_id'] = $_SESSION['admin_id'];
			$data['username'] = I('post.username');
			$data['password'] = md5(I('post.password'));
			$data['group_id'] = 2;
			$data['create_time'] = time();
			$data['is_business'] = 1;

			$success = M("Admin") -> add($data);
			if($success){
				$this->success('Add a success', U('SubsidiaryStore'));die;
			}else{
				$this->error('Add failure');die;
			}

		}else{

			$this -> display('add_subsidiary_store');
		}
	}

	//修改子门店
	public function AlterSubsidiaryStore(){
		$adminId=$_SESSION['admin_id'];
		if(IS_POST){
			$id = I('post.id');
			$data['username'] = I('post.username');
			if(I('post.password')){
				$data['username'] = md5(I('post.password'));
			}
			$data['status'] = I('post.status');
			$data['index_status'] = I('post.index_status');

			$success = M('Admin') -> where("parent_id = {$adminId} AND id = {$id}") -> save($data);
			if($success){
				$this->success('Modify the success', U('SubsidiaryStore'));die;
			}else{
				$this->error('The content did not change or add failed');die;
			}

		}else{
			$id = I('get.id');
			$data = M('Admin') -> field('id,username,status,index_status') -> where("parent_id = {$adminId} AND id = {$id}") -> find();
			if(!$data){
				die;
			}

			$this -> assign('data',$data);
			$this -> display('alter_subsidiary_store');
		}
	}

	//删除子门店
	public function DelSubsidiaryStore(){
		$adminId=$_SESSION['admin_id'];
		$id = I('get.id');

		$success = M('Admin') -> where("parent_id = {$adminId} AND id = {$id}") -> delete();
		if($success){
			$this->success('Delete the success', U('SubsidiaryStore'));die;
		}else{
			die;
		}
	}
}