<?php

namespace app\controller\merchant\share;
use crmeb\basic\BaseController;
use think\facade\Db;

class SharePosters  extends BaseController
{
    public function lst(){
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['classify']);
        $query=Db::name('share_posters')
            -> alias("sp")
            -> join("share_posters_classify spc","sp.classify = spc.id","left")
            ->where('spc.mer_id',$this->request->merId())
            -> when(!empty($where['classify']),function ($query) use ($where){
                $query -> where("sp.classify",$where['classify']);
            })->field("sp.sort,sp.name,spc.name as classify_name,sp.time,sp.type,sp.status,sp.id");
        $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 classify_list(){
        $list = Db::name("share_posters_classify") ->where('mer_id',$this->request->merId())->where('is_del',0)->order("sort desc,id asc")-> select() -> toArray();
        return app("json") -> success($list);
    }

    public function classify_del(){
        $id=$this->request->params(['id']);
        Db::name('share_posters_classify')->where('mer_id',$this->request->merId())->where('id',$id['id'])->update(['is_del'=>1]);
        return app("json") -> success('删除成功');
    }
    public function del(){
        $id=$this->request->params(['id']);
        Db::name('share_posters')->where('mer_id',$this->request->merId())->where('id',$id['id'])->delete();
        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('share_posters') ->where('mer_id',$this->request->merId())->where('id',$id['id'])->find();
        return app("json") -> success($data);
    }

    public function save_share(){
        $data = $this -> request -> params([
            'name',
            'image',
            'classify',
            'qr_coordinate',
            'head_coordinate',
            'nickname_coordinate',
            'head_size',
            'qr_size',
            'nickname_size',
            'image_fake',
            'type',
            'status',
            'sort',
            '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("share_posters") -> insert($data);
            if ($res)return app("json") -> success("成功");
            return app("json") -> fail('出错');
        }else{
            //
            $res = Db::name("share_posters") -> where("id",$id) -> update($data);
            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("share_posters_classify") -> insert($data);
        }else{
            $res = Db::name("share_posters_classify") ->where('mer_id',$this->request->merId()) -> where("id",$id) -> update($data);
            return app("json") -> success("成功");
        }
        if ($res)return app("json") -> success("成功");
        return app("json") -> fail('出错');
    }
}
