<?php

namespace Mshop\Lib;

use Dever;

class Core
{
    public function __construct()
    {
        $this->passport_uid = Dever::load('passport/user')->check(false, 'passport_signature');

        $this->uid = Dever::load('passport/user')->check(false);
        if ($this->uid <= 0) {
            $this->uid = 1;
        }
        if ($this->uid) {
            $this->user = Dever::db('shop/member')->find($this->uid);
            $this->user['uid'] = $this->uid;
        }

        $this->checkLogin();

        $this->shop_id = Dever::input('shop_id');

        if (!$this->shop_id) {
            Dever::alert('请选择门店');
        }

        $shop = Dever::db('shop/member')->find(array('mobile' => $this->user['mobile'], 'shop_id' => $this->shop_id));

        if (!$shop) {
            Dever::alert('您没有权限管理该门店');
        }

        if ($shop['uid'] > 0) {
            $this->passport_uid = $shop['uid'];
        } elseif ($shop['uid'] <= 0 && $this->passport_uid > 0) {
            Dever::db('shop/member')->update(array('where_id' => $shop['id'], 'set_uid' => $this->passport_uid));
        }

        $this->shop = Dever::db('shop/info')->find($this->shop_id);

        if (!$this->shop) {
            Dever::alert('门店不存在');
        }

        //$this->shop['coord_address'] = '';
    }

    public function checkLogin()
    {
        if (!$this->uid || $this->uid <= 0) {
            Dever::alert('请先登录', -2);
        }
    }

    public function alert($state)
    {
        if ($state) {
            return array('msg' => 'ok');
        } else {
            Dever::alert('操作失败');
        }
    }
}