<?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\allwin\controller\info;
use app\allwin\controller\Common;
use app\allwin\model\AllwinInfo;
use app\allwin\model\AllwinInfoReply;
use think\facade\Request;
use think\helper\Time;
use util\Util;

class index extends Common{

    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'信息管理','url'=>url("info.index/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']   = AllwinInfo::where($this->mini_program)->where($condition)->sum("is_top");
        $view['views_num'] = AllwinInfo::where($this->mini_program)->where($condition)->sum("views");
        $view['lock_num']  = AllwinInfo::where($this->mini_program)->where($condition)->where(['is_lock' => 1])->sum("is_lock");
        $condition[] = ['is_lock','=',$types];
        $lists = AllwinInfo::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']);
            $lists[$key]['images'] = json_decode($value['images'],true);
        }
        $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 = AllwinInfo::where($this->mini_program)->where(['id'=> $id])->find();
        if(empty($info)){
            return $this->error('没有内容');
        }
        $info['images']      = empty($info['images']) ? [] :json_decode($info['images']);
        $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']      = json_decode($info->fields,true);
        if($info->is_get){
            $view['tabs'] = [
                ['name' =>'信息内容','url' =>url('info.index/reply',['id' => $id]),'action' => 1],
                ['name' =>'订单列表','url' =>url('info.order/index',['infoid' => $id])],
            ];
        }
        return view()->assign($view);
    }
  
    //删除
    public function delete(){
        $id = input('get.id/d');
        $result = AllwinInfo::where($this->mini_program)->where(['id' => $id])->delete();
        if($result){
            AllwinInfoReply::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 = AllwinInfo::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 = AllwinInfo::Top($id);
        if(!$result){
            return json(['code'=>0,'message'=>'操作失败']);
        }else{
            return json(['code'=>200,'message'=>'操作成功']);
        }
    }
 
    //删除
    public function delReply(){
        $id = input('get.id/d');
        $result = AllwinInfoReply::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 = AllwinInfoReply::lock($id);
        if(!$result){
            return json(['code'=>0,'message'=>'操作失败']);
        }else{
            return json(['code'=>200,'message'=>'操作成功']);
        }
    }    
}