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

class AskCategory extends Common{


    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'产品分类','url'=>'javascript:;']]);
    }

    /**
     * 列表
     */
    public function index(){
        $view['lists'] = BestbaoAskCategory::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20); 
        return view()->assign($view);
    }
    
    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data = [
                'title'             => $this->request->param('title/s'),
                'sort'              => $this->request->param('sort/d'),
                'picture'           => $this->request->param('picture/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'Category.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoAskCategory::edit($data);
            if($result){
                return enjson(200,'操作成功',['url'=>url('askCategory/index')]);
            }else{
                return enjson(0);
            }
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'      => $this->request->param('id/s'),
                'title'   => $this->request->param('title/s'),
                'sort'    => $this->request->param('sort/d'),
                'picture' => $this->request->param('picture/s'),
            ];
            $validate = $this->validate($data,'Category.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  BestbaoAskCategory::edit($data);
            if($result){
                return enjson(200,'操作成功',['url'=>url('askCategory/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['info'] = BestbaoAskCategory::where(['id' => $this->request->param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
            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,'Category.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = BestbaoAskCategory::update(['sort'=>$data['sort']],['id' => $data['id']]);
            if($result){
                return enjson(200);
            }else{
                return enjson(0);
            }
        }
    }

    //删除
    public function delete(int $id){
        $goods = BestbaoAsk::where(['category_id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
        if($goods){
            return enjson(403,'删除失败,栏目中还包含资源');
        }
        $result = BestbaoAskCategory::destroy($id);
        if($result){
            return enjson(200);
        }else{
            return enjson(403,'删除失败');
        } 
    }
}