<?php
namespace app\api\controller;

use think\App;
use app\api\BaseController;
use app\api\middleware\Auth;
use think\facade\Db;

/**
 * 党员维护
 */
class ArticleComment extends BaseController{
    /**
     * 控制器中间件 [不需要鉴权]
     * @var array
     */
    protected $middleware = [
        Auth::class => ['except' 	=> ['index','OneList','isread'] ]
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);

    }
    public function index($search='',$vote_id=1,$page=1) {
        $select=Db::table('fa_article_comment')
            ->withAttr('user_info',function ($data,$value){
                $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find();
                $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find();
                $data = $find;
                $data['user_type_info']='群众';
                $data['user_name']='暂为填写姓名';
                if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){
                    $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name');
                    $data['user_name']=$msg['name'];
                }
                return $data;
            })
            ->withAttr('reply',function ($data,$value){
                if ($value['reply']==null){
                    $find='无';
                }else{
                    $find=$value['reply'];
                }
                return $find;
            })
            ->where('vote_id', $vote_id)->where('status', 1)->where('type',1)->page($page)->limit(20)->select();
        $select_type2=Db::table('fa_article_comment')
            ->withAttr('user_info',function ($data,$value){
                $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find();
                $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find();
                $data = $find;
                $data['user_type_info']='群众';
                $data['user_name']='暂为填写姓名';
                if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){
                    $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name');
                    $data['user_name']=$msg['name'];
                }
                return $data;
            })
            ->withAttr('reply',function ($data,$value){
                if ($value['reply']==null){
                    $find='无';
                }else{
                    $find=$value['reply'];
                }
                return $find;
            })
            ->where('vote_id', $vote_id)->where('status', 1)->where('type',2)->page($page)->limit(20)->select();
        $select_type3=Db::table('fa_article_comment')
            ->withAttr('user_info',function ($data,$value){
                $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find();
                $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find();
                $data = $find;
                $data['user_type_info']='群众';
                $data['user_name']='暂为填写姓名';
                if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){
                    $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name');
                    $data['user_name']=$msg['name'];
                }
                return $data;
            })
            ->withAttr('reply',function ($data,$value){
                if ($value['reply']==null){
                    $find='无';
                }else{
                    $find=$value['reply'];
                }
                return $find;
            })
            ->where('vote_id', $vote_id)->where('status', 1)->where('type',3)->page($page)->limit(20)->select();
        return $this->apiSuccess('ok',['list1'=>$select,'list2'=>$select_type2,'list3'=>$select_type3]);
    }
    public function OneList($vote_id=1,$page=1) {
        $select=Db::table('fa_article_comment')
            ->withAttr('user_info',function ($data,$value){
                $find=Db::table('fa_user')->where('id',$value['user_id'])->field('nickname,avatar')->find();
                $msg=Db::table('fa_szxc_information_usermsg')->where('user_id',$value['user_id'])->field('political_outlook,name')->find();
                $data = $find;
                $data['user_type_info']='群众';
                if($msg['name']){
                    $data['user_name']=$msg['name'];
                }else{
                    $data['user_name']='暂为填写姓名';
                }
                if ($msg['political_outlook']!='' || $msg['political_outlook']!=0){
                    $data['user_type_info']= Db::table('fa_category')->where('id',$msg['political_outlook'])->value('name');
                }
                return $data;
            })
            ->withAttr('reply',function ($data,$value){
                if ($value['reply']==null){
                    $find='无';
                }else{
                    $find=$value['reply'];
                }
                return $find;
            })->where('status', 1)->where('vote_id',$vote_id)->page($page)->limit(20)->select();

        return $this->apiSuccess('ok',['list'=>$select]);
    }
    public function add(){

    }
    public function post(){
        $input = get_params();
//        $res=$this->validate->check($input);
//        if (!$res){
//            return $this->apiError($this->validate->getError());
//        }
//        判断是否过期
        $vote_end_time = Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->value('end_time');
        if($vote_end_time && strtotime($vote_end_time) < time()){
            $this->apiError('已结束议事不能投票');
        }
        if ($input['type']!=0){
            $find=Db::table('fa_article_comment')->where('vote_id', $input['vote_id'])->where('user_id', JWT_UID)->find();
            if ($find){
                return $this->apiError('只能投票一次');
            }
        }
        if(empty($input['content'])){
            $this->apiError('请输入投票内容');
        }
        $useraddress = Db::table('fa_szxc_information_useraddress')->where('user_id', JWT_UID)->where('status', 1)->find();

        if ($useraddress) {
            $input['county']   = $useraddress['area_id'];
            $input['township'] = $useraddress['street_id'];
            $input['village'] = $useraddress['village_id'];
        }

        $input['user_id']=JWT_UID;
        $input['add_time']=date('Y-m-d H:i:s');
        $res=Db::table('fa_article_comment')->save($input);
        if ($res){
            if($input['type']==1){
                Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->inc('agree')->update();
            }elseif($input['type']==2){
                Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->inc('opposition')->update();
            }elseif($input['type']==3){
                Db::table('fa_article_vote_side_tables')->where('article_id', $input['vote_id'])->inc('other')->update();
            }else{
                $find=Db::table('fa_article')->where('id',$input['vote_id'])->find();
                if ($find){
                    $category=[150,149,148,147,165];
                    if (in_array($find['category_id'],$category)){
                        Db::table('fa_article')->where('id',$input['vote_id'])->update(['is_solve'=>1]);
                    }
                }
            }
            return $this->apiSuccess('评论成功');
        }else{
            return $this->apiError('添加失败');
        }
    }
    public function edit($id){
        $find=Db::table('fa_article_comment')->with('user')->where('id',$id)->find();
        return $this->apiSuccess('ok',$find);

    }
    public function put($id){
        $input = get_params();
//        $res=$this->validate->check($input);
//        if (!$res){
//            return $this->apiError($this->validate->getError());
//        }
        $input['user_id'] = JWT_UID;
        $res=Db::table('fa_article_comment')->where('id',$id)->update($input);
        if ($res){
            return $this->apiSuccess('修改成功');
        }else{
            return $this->apiError('修改失败');
        }
    }
    public function delete($id){
        $res=Db::table('fa_article_comment')->where('id',$id)->update(['status'=>0]);
        if ($res){
            return $this->apiSuccess('删除成功');
        }else{
            return $this->apiError('删除失败');
        }
    }

//    服务大厅清除小红点
    public function isread($vote_id=0) {
        $select=Db::table('fa_article_comment')->where('status', 1)->where('vote_id',$vote_id)->select();

        if($select->toArray()){
            $selects = $select->toArray();
            foreach ($selects as $k=>$v){
                $data['is_read'] = 1;
                Db::table('fa_article_comment')->where('id',$v['id'])->update($data);
            }
        }
         $this->apiSuccess('ok');
    }

}