<?php
namespace Home\Controller;
use Think\Controller;
class DailiController extends Controller
{
	public function login()
	{
		$user = i("post.user", "", "get_safe_str");
		$pwd = i("post.pwd", "", "get_safe_str");
		$pwd = md5(md5($pwd) . md5("815856515"));

		if ($_POST["do"] == "login") {
			if (is_numeric($user)) {
				$where = "(user='$user' or phone='$user') and pwd='$pwd'";
			}
			else {
				$where = "(user='$user' or mail='$user') and pwd='$pwd'";
			}

			if (!$row = m("users")->field("*")->where($where)->find()) {
				exit("<script language='javascript'>alert('用户名/手机号/邮箱与密码不匹配');history.go(-1);</script>");
			}
			else if (!$row[login]) {
				exit("<script language='javascript'>alert('你已经被限制登陆，请联系管理员！');history.go(-1);</script>");
			}
			else if (!$row[active]) {
				exit("<script language='javascript'>alert('请先激活账号，请联系管理员！');history.go(-1);</script>");
			}
			else {
				$newsid = md5(uniqid() . rand(1, 1000));
				$data["sid"] = $newsid;
				$data["lastip"] = get_client_ip(0, true);
				$data["lasttime"] = date("Y-m-d H:i:s");
				m("users")->where("uid='{$row["uid"]}'")->save($data);

				if ($check) {
					setcookie("xiha_sid", $newsid, time() + (3600 * 24 * 14), "/");
				}
				else {
					setcookie("xiha_sid", $newsid, time() + (3600 * 24 * 2), "/");
				}

				exit("<script language='javascript'>alert('登陆成功,进入代理管理中心。');window.location.href='" . u("index") . "';</script>");
			}
		}

		$this->display("daili/login");
	}

	public function logout()
	{
		if ($user = $this->get_user_info()) {
			$newsid = md5(uniqid() . rand(1, 1000));
			m("users")->where("uid='" . $user[uid] . "'")->setField("sid", $newsid);
		}

		setcookie("xiha_sid", "", -1, "/");
		@header("Location:/");
	}

	public function index()
	{
		$this->assign("user", $this->loginuser);
		$this->display("daili/index");
	}

	public function kmlist()
	{
		$uid = $this->loginuser["uid"];
		$kid = (is_numeric($_GET["kid"]) ? $_GET["kid"] : "0");
		if ($kid && !$kmrow = m("kms")->field("*")->where("kid='$kid' and daili='$uid'")->find()) {
			echo "<script language='javascript'>alert('卡密不存在！');</script>";
			$kid = 0;
		}

		if (($_GET["do"] == "del") && $kid) {
			if (!$kmrow["isuse"]) {
				$kmms = $kmrow["ms"];

				if ($kmrow["kind"] == 1) {
					if ($kmms == "127") {
						$thrmb = c("daili_0peie");
					}
					else if ($kmms == "10") {
						$thrmb = c("daili_10peie");
					}
					else if ($kmms == "3") {
						$thrmb = c("daili_3peie");
					}
					else {
						$thrmb = c("daili_1peie");
					}
				}
				else if ($kmms == "127") {
					$thrmb = c("daili_0vip");
				}
				else if ($kmms == "12") {
					$thrmb = c("daili_12vip");
				}
				else if ($kmms == "6") {
					$thrmb = c("daili_6vip");
				}
				else if ($kmms == "3") {
					$thrmb = c("daili_3vip");
				}
				else {
					$thrmb = c("daili_1vip");
				}

				m("users")->where("uid='$uid'")->setInc("rmb", $thrmb);
				echo "<script language='javascript'>alert('删除卡密成功，由于改卡密没有使用，成功退还" . $thrmb . "元到你账户！！');</script>";
			}

			m("kms")->where("kid='$kid'")->delete();
		}

		if ($_POST["do"] == "add") {
			$num = (is_numeric($_POST["num"]) ? $_POST["num"] : "1");
			$ms = (is_numeric($_POST["ms"]) ? $_POST["ms"] : "1");
			$kind = ($_GET["list"] == "peie" ? 1 : 0);

			if ($kind == 1) {
				if ($ms == 127) {
					$rmb = c("daili_0peie");
				}
				else if ($ms == 10) {
					$rmb = c("daili_10peie");
				}
				else if ($ms == 3) {
					$rmb = c("daili_3peie");
				}
				else {
					$ms = 1;
					$rmb = c("daili_1peie");
				}
			}
			else if ($ms == 127) {
				$rmb = c("daili_0vip");
			}
			else if ($ms == 12) {
				$rmb = c("daili_12vip");
			}
			else if ($ms == 6) {
				$rmb = c("daili_6vip");
			}
			else if ($ms == 3) {
				$rmb = c("daili_3vip");
			}
			else {
				$ms = 1;
				$rmb = c("daili_1vip");
			}

			$msg = "<ul class='list-group'>\r\n\t\t\t<li class='list-group-item active'>成功生成以下卡密</li>";

			for ($i = 0; $i < $num; $i++) {
				if ($rmb < m("users")->where("uid='$uid'")->getField("rmb")) {
					$data = array();
					$data["kind"] = $kind;
					$data["daili"] = $uid;
					$data["km"] = $this->getkm(12);
					$data["ms"] = $ms;
					$data["isuse"] = 0;
					$data["addtime"] = date("Y-m-d H:i:s");

					if (m("kms")->data($data)->add()) {
						m("users")->where("uid='$uid'")->setDec("rmb", $rmb);
						$msg .= "<li class='list-group-item'>$data[km]</li>";
					}
				}
				else {
					$msg .= "<li class='list-group-item' style='color:red;'>账户余额不足！</li>";
				}
			}

			$msg .= "</ul>";
			$this->assign("msg", $msg);
		}

		if (($_POST["do"] == "search") && ($s = i("post.key", "", "get_safe_str"))) {
			$where = "daili='$uid' and km like '%$s%'";

			if ($_GET["list"] == "peie") {
				$where .= " and kind=1";
			}
			else {
				$where .= " and kind=0";
			}

			$rows = m("kms")->field("*")->where($where)->limit(10)->order("kid desc")->select();
			$p = 1;
			$pages = 1;
			$count = 1;
		}
		else {
			$p = (is_numeric($_GET["p"]) ? $_GET["p"] : "1");
			$start = 10 * ($p - 1);
			$next = $p + 1;
			$limit = "$start,10";

			if ($_GET["list"] == "peie") {
				$where = "kind=1";
			}
			else {
				$where = "kind=0";
			}

			$where .= " and daili='$uid'";
			$count = m("kms")->where($where)->count("kid");
			$pages = ceil($count / 10);
			$rows = m("kms")->field("*")->where($where)->limit($limit)->order("kid desc")->select();
		}

		$this->assign("page", $p);
		$this->assign("pages", $pages);
		$this->assign("count", $count);
		$this->assign("list", $rows);
		$this->display("daili/kmlist");
	}

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

		if (ACTION_NAME != "login") {
			if (!$this->loginuser = $this->get_user_info()) {
				@header("HTTP/1.1 301 Moved Permanently");
				@header("Location:" . u("login"));
			}
			else if (0 < $this->loginuser["daili"]) {
				return $this->loginuser;
			}
			else {
				exit("<script language='javascript'>alert('你不是代理！');window.location.href='/';</script>");
			}
		}
	}

	private function getkm($len = 12)
	{
		$str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		$strlen = strlen($str);
		$randstr = "";

		for ($i = 0; $i < $len; $i++) {
			$randstr .= $str[mt_rand(0, $strlen - 1)];
		}

		return $randstr;
	}
}


?>
