<?php

namespace app\controller\merchant\business;
use crmeb\basic\BaseController;
use FormBuilder\Factory\Elm;
use think\facade\Db;
use think\facade\Route;

class BusinessSchool  extends BaseController
{
    public function classify_list(){
        // $list = Db::name("business_school_classify")->where('mer_id',$this->request->merId())->where('is_del',0)->order("sort desc,id desc")-> select() -> toArray();
        $list = Db::name("business_school_classify")->where('is_del',0)->order("sort desc,id desc")-> select() -> toArray();
        return app("json") -> success($list);
    }

    public function classify_del(){
        $id=$this->request->params(['id']);
        // $count=Db::name('business_school')->where('mer_id',$this->request->merId())->where('classify',$id['id'])->where('is_del',0)->count();
        $count=Db::name('business_school')->where('classify',$id['id'])->where('is_del',0)->count();
        if($count){
            return app('json')->fail('该分类下有数据，不能直接删除~');
        }

        // Db::name('business_school_classify')->where('mer_id',$this->request->merId())->where('id',$id['id'])->update(['is_del'=>1]);
        Db::name('business_school_classify')->where('id',$id['id'])->update(['is_del'=>1]);
        return app("json") -> success('删除成功');
    }
    public function classify_add_form(){
        $data['state']=1;
        $data['sort']=0;
        $form= Elm::createForm(Route::buildUrl('business_school_classify_save')->build(), [
            Elm::input('name', '分类名称')->required(),
            Elm::number('sort', '排序')->required(),
            Elm::switches('state', '状态')->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启'),
        ]);
        $form->setTitle('添加分类')->formData($data);

        $list=formToData($form);
        $list['data']=$data;
        return app('json')->success($list);
    }
    public function classify_save_form()
    {
        $id=$this->request->param('id');
        $data=Db::name('business_school_classify')->find($id);
        if(!$data){
            return app('json')->fail('数据不存在');
        }
        $data['state']=(int)$data['state'];
        $form= Elm::createForm(Route::buildUrl('business_school_classify_save', compact('id'))->build(), [
            Elm::input('name', '分类名称')->required(),
            Elm::number('sort', '排序')->required(),
            Elm::switches('state', '状态')->inactiveValue(0)->activeValue(1)->inactiveText('关闭')->activeText('开启'),
        ]);
        $form->setTitle('修改分类')->formData($data);

        $list=formToData($form);
        $list['data']=$data;
        return app('json')->success($list);
    }

    public function classify_save(){

        $data = $this -> request -> params([
            'name',
            'state',
            'sort',
            'id'
        ]);
        if(!$data['name']){
            return app("json") -> fail('请输入名称');
        }
        if (mb_strlen($data['name'],'UTF-8') > 10) {
            return app('json')->fail('名称最多支持输入 10 个汉字');
        }
        $id = $data['id'] ?? 0;
        $data['mer_id']=$this->request->merId();
        unset($data['id']);
        if ($id == 0){
            //新增
            $res = Db::name("business_school_classify") -> insert($data);
            if ($res)return app("json") -> success("成功");
            return app("json") -> fail('出错');
        }else{
            //
            $res = Db::name("business_school_classify") -> where("id",$id) -> update($data);
            return app("json") -> success("成功");
        }

    }


    public function get_sort(){
        // $data=Db::name('business_school')->field('sort')->where('mer_id',$this->request->merId())->where('is_del',0)->order('sort desc,id desc')->find();
        $data=Db::name('business_school')->field('sort')->where('is_del',0)->order('sort desc,id desc')->find();
        $sort=1;
        if($data){
            $sort=$data['sort']+1;
        }
        return app("json") -> success("成功",['sort'=>$sort]);
    }
    public function lst(){
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['classify','type','state','name','date']);
        $query=Db::name('business_school')
            -> alias("sp")
            -> join("business_school_classify spc","sp.classify = spc.id","left")
            // ->where('spc.mer_id',$this->request->merId())
            ->where('sp.is_del',0)
            -> when(isset($where['type']) && $where['type']!='',function ($query) use ($where){
                $query -> where("sp.type",$where['type']);
            })
            -> when(isset($where['state']) && $where['state']!='',function ($query) use ($where){
                $query -> where("sp.state",$where['state']);
            })
            -> when(isset($where['name']) && $where['name']!='',function ($query) use ($where){
                $query -> whereLike("sp.name","%".$where['name']."%");
            })
            -> when(isset($where['classify']) && $where['classify']!='',function ($query) use ($where){
                $query -> where("sp.classify",$where['classify']);
            })
            -> when(isset($where['date']) && $where['date']!='',function ($query) use ($where){
                getModelTime($query, $where['date'], 'sp.create_time');
            })
            ->field("sp.*,spc.name as classify_name");
        $count = $query -> count();
        $list = $query ->order("sp.sort desc,sp.id desc")->page($page,$limit)->select()->toArray();
        return app('json')->success(compact("list","count"));
    }


    public function del(){
        $id_str=$this->request->param('id');
        if($id_str=='' || is_array($id_str)){
            return app("json") -> fail('请选择数据~');
        }
        // Db::name('business_school')->where('mer_id',$this->request->merId())->whereIn('id',explode(',',$id_str))->update(['is_del'=>1]);
        Db::name('business_school')->whereIn('id',explode(',',$id_str))->update(['is_del'=>1]);
        return app("json") -> success('删除成功');
    }
    public function get_one(){
        $id=$this->request->params(['id']);
        if(empty($id['id'])){
            return app('json')->fail('id不存在');
        }
        // $data=Db::name('business_school') ->where('mer_id',$this->request->merId())->where('id',$id['id'])->find();
        $data=Db::name('business_school')->where('id',$id['id'])->find();
        return app("json") -> success($data);
    }


    public function save(){
        $data = $this -> request -> params([
            'name',
            'image',
            'classify',
            'type',
            'state',
            'sort',
            'mp4',
            'cover',
            'content',
            'id'
        ]);
        if(!$data['name']){
            return app("json") -> fail('请输入名称');
        }
        $id = $data['id'] ?? 0;
        $data['mer_id']=$this->request->merId();
        unset($data['id']);
        if ($id == 0){
            //新增
            $res = Db::name("business_school") -> insert($data);
            if ($res)return app("json") -> success("成功");
            return app("json") -> fail('出错');
        }else{
            //
            $res = Db::name("business_school") -> where("id",$id) -> update($data);
            return app("json") -> success("成功");
        }

    }
    public function save_state(){
        $id_str=$this->request->param('id');
        $state=$this->request->param('state');
        if($id_str=='' || is_array($id_str)){
            return app("json") -> fail('请选择数据~');
        }
        $ids=explode(',',$id_str);

        Db::name('business_school')
            // ->where('mer_id',$this->request->merId())
            ->whereIn('id',$ids)
            ->update(['state'=>$state==1?0:1]);
        return app("json") -> success('操作成功');
    }

    public function save_share_classify(){
        $data = $this -> request -> params([
            'name',
            'sort',
            "id"
        ]);
        if(!$data['name']){
            return app("json") -> fail('请输入分类名称');
        }
        $id = $data['id'] ?? 0;
        unset($data['id']);
        $data['mer_id']=$this->request->merId();
        if ($id == 0 ){
            $res = Db::name("business_school_classify") -> insert($data);
        }else{
            // $res = Db::name("business_school_classify") ->where('mer_id',$this->request->merId()) -> where("id",$id) -> update($data);
            $res = Db::name("business_school_classify")->where("id",$id)->update($data);
            return app("json") -> success("成功");
        }
        if ($res)return app("json") -> success("成功");
        return app("json") -> fail('出错');
    }
}
