<?php

namespace app\admin\controller;

use app\admin\common\AdminController;
use app\common\api\IntegralProjectApi;
use app\common\api\IntegralItemApi;

/**
 * Description of IntegralMgr
 * 积分管理
 * @author sgq
 */
class IntegralMgr extends AdminController {

    public function index() {
        return view();
    }

    public function list() {
        return json(IntegralProjectApi::getList($this->request->param()));
    }

    /**
     * 添加积分项目
     * @return type
     */
    public function add() {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            return $this->doSave($params);
        }
        return view("save");
    }

    /**
     * 编辑积分项目
     * @return type
     */
    public function edit() {
        $params = $this->request->param();
        if ($this->request->isPost()) {
            return $this->doSave($params);
        }
        $row = IntegralProjectApi::getOne($params["id"]);
        return view("save", ["row" => $row]);
    }

    private function doSave($params) {
//        if (!$params["type"]) {
//            return json(["msg" => "请选择人才类别"]);
//        }
        $params["type"] = session('user')['type'];

        if (!$params["name"]) {
            return json(["msg" => "请输入项目名称"]);
        }
        if (!$params["projectType"]) {
            return json(["msg" => "请选择项目类别"]);
        }
        if (!$params["active"]) {
            return json(["msg" => "请设置启用状态"]);
        }
        if (!$params["limit"]) {
            return json(["msg" => "请选择是否上限"]);
        }
        if ($params["limit"] == 1) {
            if (!$params["yearly"]) {
                return json(["msg" => "请选择上限方案"]);
            }
            if (!$params["max"]) {
                return json(["msg" => "请填写积分上限"]);
            }
            if (!is_numeric($params["max"]) || $params["max"] < 1 || $params["max"] != floor($params["max"])) {
                return json(["msg" => "积分上限必需是大于0的整数"]);
            }
        }
        if (IntegralProjectApi::chkExist($params["name"], $params["type"], $params["projectType"], $params["id"]))
            return json(["msg" => "项目名称已经被使用"]);
        $resultStr = $params["id"] ? "编辑" : "添加";
        if (IntegralProjectApi::edit($params))
            return json(["code" => 200, "msg" => "{$resultStr}积分项目成功"]);
        return json(["msg" => "{$resultStr}积分项目失败"]);
    }

    /**
     * 删除积分项目
     * @return type
     */
    public function delete() {
        if ($this->request->isPost()) {
            $id = $this->request->param("id");
            if (IntegralProjectApi::delete($id))
                return json(["code" => 200, "msg" => "删除积分项目成功"]);
            return json(["msg" => "删除积分项目失败"]);
        }
    }

    public function items() {
        return view("items");
    }

    public function itemList() {
        return json(IntegralItemApi::getList($this->request->param()));
    }

    public function addItem() {
        if ($this->request->isPost()) {
            $params = $this->request->param();
            return $this->doItemSave($params);
        }
        return view("save_item");
    }

    public function editItem() {
        $params = $this->request->param();
        if ($this->request->isPost()) {
            return $this->doItemSave($params);
        }
        $row = IntegralItemApi::getOne($params["id"]);
        return view("save_item", ["row" => $row]);
    }

    private function doItemSave($params) {
        if (!$params["projectId"]) {
            return json(["msg" => "请选择积分项目"]);
        }
        if (!$params["name"]) {
            return json(["msg" => "请输入标准名称"]);
        }
        if (!$params["plan"]) {
            return json(["msg" => "请选择积分方案"]);
        }
        if (!$params["unit"]) {
            return json(["msg" => "请输入计量单位"]);
        }
        if (!$params["fstNeedAmount"]) {
            return json(["msg" => "请输入首次达成量"]);
        }
        if (!$params["fstGainPoints"]) {
            //return json(["msg" => "请输入首次获得积分"]);
        }
        if ($params["plan"] == 2) {
            if (!$params["stepNeedAmount"]) {
                return json(["msg" => "请输入每新增量"]);
            }
            if (!$params["stepGainPoints"]) {
                return json(["msg" => "请输入每新增获得积分"]);
            }
        }
        if (!$params["limit"]) {
            return json(["msg" => "请选择是否上限"]);
        }
        if ($params["limit"] == 1) {
            if (!$params["yearly"]) {
                return json(["msg" => "请选择上限方案"]);
            }
            if (!$params["max"]) {
                return json(["msg" => "请填写积分上限"]);
            }
            if (!is_numeric($params["max"]) || $params["max"] < 1 || $params["max"] != floor($params["max"])) {
                return json(["msg" => "积分上限必需是大于0的整数"]);
            }
        }
        if (IntegralItemApi::chkExist($params["name"], $params["projectId"], $params["id"]))
            return json(["msg" => "名称已经被使用"]);
        $resultStr = $params["id"] ? "编辑" : "添加";
        if (IntegralItemApi::edit($params))
            return json(["code" => 200, "msg" => "{$resultStr}积分标准成功"]);
        return json(["msg" => "{$resultStr}积分标准失败"]);
    }

    public function deleteItem() {
        if ($this->request->isPost()) {
            $id = $this->request->param("id");
            if (IntegralItemApi::delete($id))
                return json(["code" => 200, "msg" => "删除积分标准成功"]);
            return json(["msg" => "删除积分标准失败"]);
        }
    }

    public function getProjectsByType() {
        $type = $this->request->param("type") ?: 0;
        $projectType = $this->request->param("projectType") ?: 0;
        $where[] = ["type", "=", $type];
        $where[] = ["active", "=", 1];
        $list = IntegralProjectApi::getAll($where);
        return json($list);
    }

    public function getFilesByType() {
        $type = $this->request->param("type") ?: 0;
        $where[] = ["type", "=", $type];
        $where[] = ["project", "=", 20];
        $where[] = ["active", "=", 1];
        $list = \app\common\api\FileTypeApi::getAll($where);
        return json($list);
    }

    public function getItemsByProject() {
        $projectId = $this->request->param("projectId") ?: 0;
        $where[] = ["projectId", "=", $projectId];
        $where[] = ["active", "=", 1];
        $list = IntegralItemApi::getAll($where);
        return json($list);
    }

}
