<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\BaseController;
use think\Request;
use think\App;
use think\facade\Db;

//日志
class Log extends Base
{


    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
        $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
        $condition = [['type',"=",1]];
        $count = Db::name("action_log")->where($condition)->count();
        $total = ceil($count/$size);
        $page = $page >= $total ? intval($total) : $page;
        $list =Db::name("action_log")->where($condition)->page($page,$size)->order("addtime desc")->select();
        return app_show(0,"获取成功",['list'=>$list,"count"=>$count]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read()
    {
        $id = isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']) :"";
        if($id===""){
            return error_show(1004,"参数 id 不能为空");
        }
        $info = Db::name("action_log")->where(["id"=>$id])->find();
        if(empty($info)){
            return error_show(1004,"未找到数据");
        }
        return app_show(0,"获取成功",$info);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function msglist()
    {
        $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
        $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
        $condition=[];
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        $apply_id = GetUserInfo($token);
        if (empty($apply_id) || $apply_id['code'] != 0) {
            return error_show(1002, "用户数据不存在");
        }
        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
        //$condition[]=["uid","=",$rm];
        $orderCode = isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):'';
        if($orderCode!=""){
            $condition[]=["orderCode","like","%$orderCode%"];
        }
        $order_type = isset($this->post['order_type'])&&$this->post['order_type']!=''?trim($this->post['order_type']):'';
        if($order_type!=""){
            $condition[]=["order_type","=",$order_type];
        }
        $is_read = isset($this->post['is_read'])&&$this->post['is_read']!==''?intval($this->post['is_read']):'';
        if($is_read!==""){
            $condition[]=["is_read","=",$is_read];
        }
        $count = Db::name("order_msg")->where($condition)->count();
        $total = ceil($count/$size);
        $page = $page >= $total ? $total : $page;
        $list =Db::name("order_msg")->where($condition)->page(intval($page),$size)->order("addtime desc")->select();
        $data=[];
        $type = \think\facade\Config::get("order");
        foreach ($list as $value){
            $value['order_type_cn']=$type["order_type"][$value['order_type']];
            $data[]=$value;
        }
        return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        $id = isset($this->post['id'])&&$this->post['id']!=="" ? intval($this->post['id']) :"";
        if($id===""){
            return error_show(1004,"参数 id 不能为空");
        }
        $info = Db::name("order_msg")->where(["id"=>$id])->find();
        if(empty($info)){
            return error_show(1004,"未找到数据");
        }
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        $apply_id = GetUserInfo($token);
        if (empty($apply_id) || $apply_id['code'] != 0) {
            return error_show(1002, "用户数据不存在");
        }
        $type = \think\facade\Config::get("order");
        $info['order_type_cn']=$type["order_type"][$info['order_type']];
        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
        if($rm==$info['uid'] && $info['is_read']==0){
            Db::name("order_msg")->where(["id"=>$id])->save(["is_read"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
        }
        return app_show(0,"获取成功",$info);
    }

    public function limit()
    {
        $id = isset($this->post['id'])&&$this->post['id']!=="" ? $this->post['id'] :[];
        if(empty($id)){
            return error_show(1004,"参数 id 不能为空");
        }
        $info = Db::name("order_msg")->where(["id"=>$id])->select()->toArray();
        if(empty($info)){
            return error_show(1004,"未找到数据");
        }
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        $apply_id = GetUserInfo($token);
        if (empty($apply_id) || $apply_id['code'] != 0) {
            return error_show(1002, "用户数据不存在");
        }
        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
        $read= Db::name("order_msg")->where(["id"=>$id,"uid"=>$rm])->save(["is_read"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
        return $read?app_show(0,"已读成功"):app_show(1003,"已读失败");
    }
    public function allread()
    {
        $token = isset($this->post['token']) ? trim($this->post['token']) : "";
        $apply_id = GetUserInfo($token);
        if (empty($apply_id) || $apply_id['code'] != 0) {
            return error_show(1002, "用户数据不存在");
        }
        $rm = isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
        $info = Db::name("order_msg")->where(["uid"=>$rm,"is_read"=>0])->select()->toArray();
        if(empty($info)){
            return error_show(1004,"暂无未读数据");
        }
        $read=Db::name("order_msg")->where(["uid"=>$rm,"is_read"=>0])->save(["is_read"=>1,"updatetime"=>date("Y-m-d 
        H:i:s")]);
        return $read?app_show(0,"全部已读成功"):app_show(1003,"全部已读失败");
    }
}