<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/20
 * Time: 19:57
 */

namespace app\index\controller;


use app\common\controller\Frontend;
use app\index\model\Store;
use think\Db;

class Shop extends Frontend
{
    protected $noNeedLogin = ['detail'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->view->assign('is_search', 0);
        $this->view->assign('is_active', 0);
        $this->view->assign('title', '店铺详情');
    }

    public function detail(){
        $province_id = $this->request->param('province_id',0,'intval');
        $user_id = $this->auth->id;
        $store_id = $this->request->param('store_id',0,'intval');
        if(empty($store_id)){
            $this->error('缺少必要参数');
        }
        $storeModel = new Store();
        $data = $storeModel->findData(['id'=>$store_id]);
        //收藏
        $is_collect = "2";
        $encrypt = '2';
        if(!empty($user_id)){
            $storecollect = Db::name('storecollect')->where(['user_id'=>$user_id,'stores_id'=>$data['id']])->find();
            if(!empty($storecollect)){
                $is_collect = "1";
            }
            $userModel = new \app\index\model\User();
            $user = $userModel->findData(['id'=>$data['user_id']]);
            if(!empty($user)){
                $province_ids = [];
                /*if($data['province_ids']) {
                    $province_ids = is_array($data['province_ids']) ? $data['province_ids'] : explode(',',$data['province_ids']);
                }*/
                $user_province = Db::name('user_province')->where(['user_id'=>$data['user_id'],'expiration_time'=>['gt',time()]])->select();
                foreach($user_province as $key => $u_p){
                    $province_ids[] = $u_p['province_id'];
                }
                if($data['is_svip'] == '1' || in_array($province_id,$province_ids)){
                    $encrypt = '1';
                }
            }
        }
        $data['is_collect'] = $is_collect;
        if(empty($user_id)){
            $text = "(请您先登录)";
            $data['phone'] = substr_replace($data['phone'],'****',3,4).$text;
            $data['address'] = substr_replace($data['address'],'********',9).$text;
        }
        //当前用户若不是会员加密部分信息
        if($encrypt == '2'){
            $text = '';
            if(empty($user_id)){
                $text = "(请您先登录)";
            }
            $data['phone'] = substr_replace($data['phone'],'****',3,4).$text;
            $data['address'] = substr_replace($data['address'],'********',9).$text;
        }
        $this->assign('data',$data);
//        dump(collection($data)->toArray());
        if($this->request->isAjax()){
            $this->success('SUCCESS','',$data);
        }
        return $this->fetch();
    }

    public function collect(){
        $user_id = $this->auth->id;
        $store_id = $this->request->param('store_id',0,'intval');
        if(empty($store_id)){
            $this->error('缺少必要参数');
        }
        $data = Db::name('storecollect')->where(['user_id'=>$user_id,'stores_id'=>$store_id])->find();
        $arr['user_id'] = $user_id;
        $arr['stores_id'] = $store_id;
        $arr['updatetime'] = time();
        if(empty($data)){
            $arr['createtime'] = time();
            $result = Db::name('storecollect')->insert($arr);
        }else{
            $result = Db::name('storecollect')->where(['id'=>$data['id']])->update($arr);
        }
        if(empty($result)){
            $this->error('sql执行失败');
        }
        $this->success('SUCCESS');
    }
}