<?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\allwin\controller;
use app\allwin\model\AllwinStoreQuan;
use app\allwin\model\AllwinStore;

class Quan extends Common{

    /**
     * 列表
     */
    public function index(){
        $view['lists']    = AllwinStoreQuan::where(['member_miniapp_id' => $this->member_miniapp_id])->order('id desc')->paginate(20);
        $view['pathMaps'] = [['name'=>'商圏管理','url'=> url("quan/index")]];
        return $this->fetch()->assign($view);
    }

    /**
     * 已绑定商圏列表
     */
    public function storeIndex(int $id){
        $view['quan_id']  = $id;
        $view['lists']    = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id, 'quan_id' => $id])->order('id desc')->paginate(20);
        $view['pathMaps'] = [['name'=>'商圏管理','url'=> url("quan/index")],['name'=>'商圈商户','url'=> url("quan/storeindex",['id' => $id])]];
        return $this->fetch()->assign($view);
    }

    /**
     * 商圏列表
     */
    public function select(int $quan_id){
        $condition   = [];
        $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
        if(request()->isAjax()){
            $ids     = $this->request->param('ids');
            if(empty($ids)){
                return json(['code'=>0,'msg'=>'请选择要入住商圏的好店']);
            }
            $result = AllwinStore::where($condition)->whereIn('id',$ids)->update(['quan_id' => $quan_id]);
            if($result){
                return json(['code'=>200,'msg'=>'好店入住商圏成功','data' =>[]]);
            }else{
                return json(['code'=>0,'msg'=>'好店入住商圏失败']);
            }
        }else{
            $keyword  = $this->request->param('keyword');
            if(!empty($keyword)) {
                $condition[] = ['name', 'like', '%' . $keyword . '%'];
            }
            $view['lists']   = AllwinStore::where($condition)->order('id desc')->paginate(20);
            $view['keyword'] = $keyword;
            $view['quan_id'] = $quan_id;
            return $this->fetch()->assign($view);
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $param = [
                'id'      => $this->request->param('id/d',0),
                'title'   => $this->request->param('title/s'),
                'content' => $this->request->param('content/s'),
                'picture' => $this->request->param('picture/s'),
            ];
            $validate = $this->validate($param,'Quan.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  AllwinStoreQuan::where(['member_miniapp_id' => $this->member_miniapp_id,'id' =>  $param['id']])->find();
            if($result){
                $result->title   = $param['title'];
                $result->content = $param['content'];
                $result->picture = $param['picture'];
                $result->save();
            }else{
                $param['member_miniapp_id'] = $this->member_miniapp_id;
                AllwinStoreQuan::create($param);
            }
            return json(['code'=>200,'url'=>url('quan/index'),'msg'=>'操作成功']);
        }else{
            $view['info']= AllwinStoreQuan::where(['id' => $this->request->param('id/d',0),'member_miniapp_id' => $this->member_miniapp_id])->find();
            return $this->fetch()->assign($view);
        }
    }

    //删除商圈
    public function delete(int $id){
        $store = AllwinStore::where(['member_miniapp_id' => $this->member_miniapp_id,'quan_id' => $id])->find();
        if($store){
            return json(['code'=>403,'msg'=>'删除失败,商圏中还包含店铺']);
        }
        $result = AllwinStoreQuan::destroy($id);
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败']);
        } 
    }

    //从商圏中删除好店
    public function deleteStore(int $id){
        $result = AllwinStore::where(['id' => $id])->update(['quan_id' => 0]);
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败']);
        }
    }
}