<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 信息管理
 */
namespace app\citys\controller;
use app\citys\controller\Common;
use app\citys\model\Citys;
use app\citys\model\CitysReply;
use think\facade\Request;
use think\helper\Time;
use util\Util;

class Info extends Common{

    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'信息管理','url'=>url("citys/info/index")]]);
    }

    /**
     * 列表
     */
    public function index(int $types = 0){
        $condition  = [];
        $time      = Request::param('time/d',0);
        $starttime = Request::param('starttime/s');
        $endtime   = Request::param('endtime/s');
        if($time){
            switch ($time) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['create_time','>=',$start];
            $condition[] = ['create_time','<=',$end];
        }else{
            if($starttime){
                $condition[] = ['create_time','>=',strtotime($starttime)];
            }
            if($endtime){
                $condition[] = ['create_time','<=',strtotime($endtime)];
            }
        }
        $view['top_num']   = citys::where($this->mini_program)->where($condition)->sum("is_top");
        $view['views_num'] = citys::where($this->mini_program)->where($condition)->sum("views");
        $view['lock_num']  = citys::where($this->mini_program)->where($condition)->where(['is_lock' => 1])->sum("is_lock");
        $condition[] = ['is_lock','=',$types];
        $lists = citys::where($this->mini_program)->where($condition)->order('is_top desc,sort desc,id desc')->paginate(20,false,['query' => ['types' => $types,'starttime' => $starttime,'endtime' => $endtime,'time'=>$time]]); 
        $view['types']     = $types;
        foreach ($lists as $key => $value) {
            $lists[$key]['howday'] = Util::ftime($value['create_time']);
        }
        $view['lists']      = $lists;
        $view['time']       = $time;
        $view['starttime']  = $starttime;
        $view['endtime']    = $endtime;
        return view()->assign($view);
    }

   /**
     * 查看回复
     * @param integer $id 用户ID
     */
    public function reply(int $id){
        $info = citys::where($this->mini_program)->where(['id'=> $id])->find();
        if(empty($info)){
            return $this->error('没有内容');
        }
        $info['create_time'] = Util::ftime($info['create_time']);
        $view['info']        = $info;
        $view['reply']       = $info->comments()->order('id desc')->paginate(10,false,['query'=>['id' => $id]]);
        $view['fields']      = $info->fields;
        if($info->is_get){
            $view['tabs'] = [
                ['name' =>'信息内容','url' =>url('citys/index/reply',['id' => $id]),'action' => 1],
                ['name' =>'订单列表','url' =>url('citys/order/index',['infoid' => $id])],
            ];
        }
        return view()->assign($view);
    }
  
    //删除
    public function delete(int $id){
        $result = citys::where($this->mini_program)->where(['id' => $id])->delete();
        if($result){
            CitysReply::where($this->mini_program)->where(['info_id' => $id])->delete();
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败,请查看是否包含子链接']);
        } 
    }

    /**
     * 锁定
     * @param integer $id 用户ID
     */
    public function islock(int $id){
        $result = citys::lock($id,$this->member_miniapp_id);
        if(!$result){
            return json(['code'=>0,'message'=>'操作失败']);
        }else{
            return json(['code'=>200,'message'=>'操作成功']);
        }
    }


    /**
     * 指定
     * @param integer $id 用户ID
     */
    public function isTop(int $id){
        $result = citys::Top($id);
        if(!$result){
            return json(['code'=>0,'message'=>'操作失败']);
        }else{
            return json(['code'=>200,'message'=>'操作成功']);
        }
    }
 
    //删除
    public function delReply(){
        $id = input('get.id/d');
        $result = CitysReply::where($this->mini_program)->where(['id' => $id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败']);
        } 
    }

    /**
     * 锁定
     * @param integer $id 用户ID
     */
    public function islockReply(int $id){
        $result = CitysReply::lock($id);
        if(!$result){
            return json(['code'=>0,'message'=>'操作失败']);
        }else{
            return json(['code'=>200,'message'=>'操作成功']);
        }
    }    
}