<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;

use app\admin\common\AdminController;
use think\facade\Db;
use app\common\api\Response;
use app\common\api\DictApi;
use app\common\model\AmountStandard as AsModel;

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

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

    public function list() {
        $params = $this->request->param();
        $order = $params["order"] ?: "asc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        $where[] = ["type", "=", $this->user["type"]];
        if ($params["allowanceType"]) {
            $where[] = ["allowanceType", "=", $params["allowanceType"]];
        }
        $count = AsModel::where($where)->count();
        $list = AsModel::where($where)->limit($offset, $limit)->order("createTime " . $order)->select()->toArray();
        $levelMap = DictApi::selectByParentCode("talent_arrange");
        foreach ($list as &$item) {
            $item["talentArrangeName"] = $levelMap[$item["talentArrange"]];
        }unset($item);
        return json(["total" => $count, "rows" => $list]);
    }

    public function add() {
        if ($this->request->isPost()) {
            return $this->save();
        }
        return view("edit", ["type" => $this->user["type"]]);
    }

    public function edit() {
        $info = AsModel::where("id", $this->request["id"])->find();
        if ($this->request->isPost()) {
            return $this->save();
        }
        return view("", ["info" => $info, "type" => $this->user["type"]]);
    }

    public function detail() {
        $info = AsModel::where("id", $this->request["id"])->find();
        return view("", ["info" => $info, "type" => $this->user["type"]]);
    }

    public function delete() {
        if (!$this->request["id"])
            return new Response(Response::ERROR, "删除失败");
        AsModel::where("id", $this->request["id"])->delete();
        return new Response(Response::SUCCESS, "删除成功");
    }

    private function save() {
        $param = $this->request->param();
        if (!$param) {
            return new Response(Response::ERROR, "请填写相关信息");
        }
        if (!$param["money"] || !is_numeric($param["money"]) || $param["money"] < 0) {
            return new Response(Response::ERROR, "金额填写错误");
        }
        $data["type"] = $this->user["type"];
        $data["allowanceType"] = $param["allowanceType"];
        $data["talentArrange"] = $param["talentArrange"];
        $data["money"] = $param["money"];
        $data["description"] = $param["description"];
        try {
            if ($param["id"]) {
                $data["id"] = $param["id"];
                $data["updateTime"] = date("Y-m-d H:i:s");
                $data["updateUser"] = $this->user["uid"];
                AsModel::update($data);
                $msg = "修改成功";
            } else {
                $data["id"] = getStringId();
                $data["createTime"] = date("Y-m-d H:i:s");
                $data["createUser"] = $this->user["uid"];
                AsModel::insert($data);
                $msg = "添加成功";
            }
            return new Response(Response::SUCCESS, $msg);
        } catch (\think\db\exception\DbException $ex) {
            return new Response(Response::ERROR, "数据异常，操作失败：" . $ex->getCode());
        }
    }

}
