<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\model\Person as PsModel;

/**
 * Description of Person
 *
 * @author sgq
 */
class Person extends AdminController {

    /**
     * @@auth {{/person/gotoPersonPage}}
     * @return type
     */
    public function index() {
        return view("");
    }

    public function list() {
        $where = [];
        $params = $this->request;
        $order = trim($params["order"]) ?: "desc";
        $offset = trim($params["offset"]) ?: 0;
        $limit = trim($params["limit"]) ?: 10;
        $where[] = ["type", "=", $this->user["type"]];
        if (\StrUtil::isNotEmpAndNull($params["username"])) {
            $where[] = ["username", "like", "%" . $params["username"] . "%"];
        }
        if (\StrUtil::isNotEmpAndNull($params["name"])) {
            $where[] = ["name", "like", "%" . $params["name"] . "%"];
        }
        if ($params["sex"]) {
            $where[] = ["sex", "=", $params["sex"]];
        }
        if (\StrUtil::isNotEmpAndNull($params["idCard"])) {
            $where[] = ["idCard", "like", "%" . $params["idCard"] . "%"];
        }
        if (\StrUtil::isNotEmpAndNull($params["phone"])) {
            $where[] = ["phone", "like", "%" . $params["phone"] . "%"];
        }
        $count = PsModel::where($where)->count();
        $rows = PsModel::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        $res = ["total" => $count, "rows" => $rows];
        return json($res);
    }

    /**
     * @@auth {{/person/gotoPersonDetailPage}}
     * @return type
     */
    public function detail() {
        $id = \StrUtil::getRequestDecodeParam($this->request, "id");
        $person = PsModel::find($id);
        if ($person["sex"]) {
            $person["sexName"] = $person["sex"] == 1 ? "男" : "女";
        }
        if ($person["active"]) {
            $person["activeName"] = $person["active"] == 1 ? "账号有效" : "冻结/拉黑";
        }
        return view("", ["person" => $person]);
    }

    /**
     * @auth {{/person/setActive}}
     * @return type
     */
    public function setActive() {
        if ($this->request->isAjax()) {
            $response = new \stdClass();
            $response->code = 500;

            $id = \StrUtil::getRequestDecodeParam($this->request, "id");
            $active = \StrUtil::getRequestDecodeParam($this->request, "active");
            $activeMsg = \StrUtil::getRequestDecodeParam($this->request, "activeMsg");
            //校验 ID 是否为空
            if (\StrUtil::isEmpOrNull($id)) {
                $response->msg = "ID不能为空!";
                return json($response);
            }
            //校验 是否为账号有效 或者 账号拉黑/冻结
            if (!$active) {
                $response->msg = "请选择账号状态!";
                return json($response);
            }
            if (!in_array($active, [1, 2])) {
                $response->msg = "账号状态有误!";
                return json($response);
            }
            //如果审核状态为拉黑/冻结  需要填写冻结原因
            if ($active == 2) {
                if (\StrUtil::isEmpOrNull($activeMsg)) {
                    $response->msg = "请填写冻结/拉黑原因!!";
                    return json($response);
                }
                if (mb_strlen($activeMsg) > 100) {
                    $response->msg = "冻结/拉黑原因最多100个字符!";
                    return json($response);
                }
            }
            $old = PsModel::find($id);
            if (!$old) {
                $response->msg = "用户信息不存在!";
                return json($response);
            }
            try {
                $data["id"] = $id;
                $data["active"] = $active;
                $data["activeMsg"] = $activeMsg;
                $data["updateUser"] = $this->user["uid"];
                $data["updateTime"] = date("Y-m-d H:i:s");
                PsModel::update($data);
                $response->msg = "操作成功!";
                $response->code = 200;
                return json($response);
            } catch (\Exception $e) {
                $response->msg = $e->getMessage();
                return json($response);
            }
        }
        $id = \StrUtil::getRequestDecodeParam($this->request, "id");
        $person = PsModel::find($id);
        return view("", ["person" => $person]);
    }

    /**
     * @auth {{/person/resetPassword}}
     * @return type
     */
    public function resetPassword() {
        $response = new \stdClass();
        $response->code = 500;
        $defaultPassword = "JJrc@123";
        $id = \StrUtil::getRequestDecodeParam($this->request, "id");
        if (\StrUtil::isEmpOrNull($id)) {
            $response->msg = "ID不能为空!";
            return json($response);
        }
        $person = PsModel::find($id);
        if (!$person) {
            $response->msg = "数据不存在!";
            return json($response);
        }
        try {
            $data["id"] = $id;
            $data["updateUser"] = $this->user["uid"];
            $data["updateTime"] = date("Y-m-d H:i:s");
            $data["password"] = md5($defaultPassword);
            PsModel::update($data);
            $response->msg = "操作成功!";
            $response->code = 200;
            return json($response);
        } catch (\Exception $e) {
            $response->msg = $e->getMessage();
            return json($response);
        }
    }

}
