<?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\smartbc\controller;
use app\smartbc\model\SmartbcStoreCate;
use app\smartbc\model\SmartbcStore;

class StoreCate extends Common{

    public function initialize() {
        parent::initialize();
        $this->cate  = new SmartbcStoreCate();
        $this->store = new SmartbcStore();
    }

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

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'        => $this->request->param('id/s'),
                'title'     => $this->request->param('title/s'),
                'name'      => $this->request->param('name/s'),
                'sort'      => $this->request->param('sort/d'),
                'parent_id' => $this->request->param('parent_id/d'),
                'picture'   => $this->request->param('picture/s'),
            ];
            $validate = $this->validate($data,'StoreCate.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  $this->cate->edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('storeCate/index',['parent_id'=>$data['parent_id']]),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['info'] = $this->cate->where(['id' => $this->request->param('id/d'),'member_miniapp_id' => $this->member_miniapp_id])->find();
            if(empty($view['info'])){
                $this->error("404 NOT FOUND");
            }
            $view['pathMaps'] = $this->cate->selectPath($view['info']->parent_id);  
            return $this->fetch()->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,'StoreCate.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = $this->cate->save(['sort'=>$data['sort']],['id' => $data['id']]);
            if($result){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }
    }

    //删除
    public function delete(int $id){
        $info =  $this->cate->where(['parent_id' => $id])->find();
        if($info){
            return json(['code'=>403,'msg'=>'删除失败,请查看是否包含子栏目']);
        }
        $store = $this->store->where(['cate_id' => $id])->find();
        if($store){
            return json(['code'=>403,'msg'=>'删除失败,栏目中还包含好店']);
        }
        $result = $this->cate->where(['id'=>$id,'member_miniapp_id' => $this->member_miniapp_id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败,请查看是否包含子栏目']);
        } 
    }
}