<?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\CitysType;
use app\citys\model\Citys;

class Type extends Common{


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

    /**
     * 列表
     */
    public function index(){
        $view['lists'] = CitysType::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 = [
                'member_miniapp_id' => $this->member_miniapp_id,
                'title'             => $this->request->param('title/s'),
                'endtips'           => $this->request->param('endtips/s'),
                'phone'             => $this->request->param('phone/f'),
                'price'             => $this->request->param('price/f'),
                'sort'              => $this->request->param('sort/d'),
            ];
            $validate = $this->validate($data,'Cate.type');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  CitysType::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('citys/cate/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'        => $this->request->param('id/s'),
                'title'     => $this->request->param('title/s'),
                'endtips'   => $this->request->param('endtips/s'),
                'phone'     => $this->request->param('phone/f'),
                'price'     => $this->request->param('price/f'),
                'sort'      => $this->request->param('sort/d'),
            ];
            $validate = $this->validate($data,'Cate.type');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  CitysType::edit($data);
            if($result){
                return enjson(200,['url'=>url('citys/type/index')]);
            }else{
                return enjson(0);
            }
        }else{
            $view['id']  = $this->request->param('id/d',0);
            $view['info']= CitysType::where($this->mini_program)->where(['id' => $view['id']])->find();
            return view('edit',$view);
        }
    }

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

    //删除
    public function delete(){
        $id = $this->request->param('id/d');
        $info = citys::where(['type_id' => $id])->count();
        if($info){
            return enjson(0,'删除失败,栏目中还包含内容');
        }
        $result = CitysType::where(['id'=>$id,'member_miniapp_id' => $this->member_miniapp_id])->delete();
        if($result){
            return enjson(200);
        }else{
            return enjson(0);
        } 
    }
}