<?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\bestbao\controller;
use app\bestbao\model\BestbaoAsk;
use app\bestbao\model\BestbaoAskCategory;

class Ask extends Common{

    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'知识库','url'=>'javascript:;']]);
    }


    /**
     * 列表
     */
    public function index(){
        $view['lists'] = BestbaoAsk::where(['member_miniapp_id' => $this->member_miniapp_id])->order('is_top desc,sort desc,update_time desc')->paginate(15);
        return view()->assign($view);
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $param = [
                'is_top'            => $this->request->param('is_top/s'),
                'is_ad'             => $this->request->param('is_ad/s'),
                'ad_img'            => $this->request->param('ad_img/s'),
                'category_id'       => $this->request->param('category_id/d'),
                'title'             => $this->request->param('title/s'),
                'note'              => $this->request->param('note/s'),
                'price'             => $this->request->param('price/s'),
                'content'           => $this->request->param('content/s'),
                'answer'            => $this->request->param('answer/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'update_time'       => time(),
                'create_time'       => time(),
            ];
            $validate = $this->validate($param,'Ask.add');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoAsk::create($param);
            if($result){
                return enjson(200,'操作成功',['url'=>url('ask/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['category'] = BestbaoAskCategory::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->select();
            return view()->assign($view);
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $param = [
                'id'                => $this->request->param('id/s'),
                'is_top'            => $this->request->param('is_top/s'),
                'is_ad'             => $this->request->param('is_ad/s'),
                'ad_img'            => $this->request->param('ad_img/s'),
                'category_id'       => $this->request->param('category_id/d'),
                'title'             => $this->request->param('title/s'),
                'note'              => $this->request->param('note/s'),
                'price'             => $this->request->param('price/s'),
                'content'           => $this->request->param('content/s'),
                'answer'            => $this->request->param('answer/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'update_time'       => time(),
            ];
            $validate = $this->validate($param,'Ask.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoAsk::update($param);
            if($result){
                return enjson(200,'操作成功',['url'=>url('ask/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['info']     = BestbaoAsk::where(['id' => $this->request->param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
            $view['category'] = BestbaoAskCategory::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->select();
            return view()->assign($view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'ask.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = BestbaoAsk::update(['sort'=>$data['sort']],['id' => $data['id']]);
            if($result){
                return enjson(200);
            }else{
                return enjson(0);
            }
        }
    }

    //删除
    public function delete(int $id){
        $result = BestbaoAsk::destroy($id);
        if($result){
            return enjson(200);
        }else{
            return enjson(403,'删除失败');
        } 
    }


    /**
     * 推荐
     * @param integer $id 用户ID
     */
    public function isAd(int $id){
        $result = BestbaoAsk::ad($id);
        if(!$result){
            return enjson(0,'操作失败');
        }else{
            return enjson(200,'操作成功');
        }
    }


    /**
     * 置顶
     * @param integer $id 用户ID
     */
    public function isTop(int $id){
        $result = BestbaoAsk::top($id);
        if(!$result){
            return enjson(0,'操作失败');
        }else{
            return enjson(200,'操作成功');
        }
    }
}