<?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\ais\controller\store;
use app\ais\controller\Common;
use app\ais\model\AisStoreCate;
use app\ais\model\AisStore;
use app\ais\model\AisStoreChain;

class Chain extends Common{

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

    /**
     * 连锁门店
     */
    public function index(int $store_id){
        $view['info']     = AisStore::where(['id' => $store_id])->find();
        $view['lists']    = AisStoreChain::where(['store_id' => $store_id])->order('sort desc,id desc')->paginate(20);
        $view['store_id'] = $store_id;
        return view()->assign($view);
    }


    //编辑
    public function edit(int $store_id){
        if(request()->isAjax()){
            $data = [
                'store_id'          => $store_id,
                'id'                => $this->request->param('id/d',0),
                'title'             => $this->request->param('title/s'),
                'address'           => $this->request->param('address/s'),
                'longitude'         => $this->request->param('longitude/s'),
                'latitude'          => $this->request->param('latitude/s'),
                'telphone'          => $this->request->param('telphone/s'),
                'update_time'       => time(),
                'member_miniapp_id' => $this->member_miniapp_id
            ];
            $validate = $this->validate($data,'Store.chain');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            if($data['id']){
                $rel = AisStoreChain::update($data);
            }else{
                $rel = AisStoreChain::create($data);
            }
            if($rel){
                return enjson(200);
            }
            return enjson(0);
        }else{
            $view['id']       = $this->request->param('id/d',0);
            $view['info']     = AisStoreChain::where(['store_id' => $store_id,'id' => $view['id']])->find();
            $view['store_id'] = $store_id;
            return view()->assign($view);
        }
    }

    //删除
    public function delete(int $id){
        AisStoreChain::where($this->mini_program)->where(['id' =>$id])->delete();   //删除好店
        return json(['code'=>200,'msg'=>'操作成功']);
    } 

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

}