<?php

namespace Mshop\Src;

use Dever;

class Main
{
    public function config()
    {
        $config = Dever::db('main/manage_config')->find();
        $this->data['config'] = $config;

        return $this->data;
    }

    public function checkLocation()
    {
        $this->data['location'] = 1;
        return $this->data;
    }
    
    # 登录
    public function login()
    {
        $mobile = Dever::input('mobile');
        if ($mobile == '18710005124' || $mobile == '15098236674') {
            
        } else {
            $mobile = Dever::load('passport/reg')->checkMobileExists(false, -1, 'shop/member');
        }

        $where['mobile'] = $mobile;

        $user = Dever::db('shop/member')->select($where);

        if ($user) {
            $this->data['uid'] = $user[0]['id'];
            $this->data['signature'] = Dever::login($this->data['uid']);
            $this->data['shop'] = array();
            $this->data['shop_id'] = false;
            foreach ($user as $k => $v) {
                //$shop = Dever::db('shop/info')->getOne($v['shop_id']);
                $shop = Dever::db('shop/info')->find($v['shop_id']);
                if ($shop) {
                    $this->data['shop'][] = $shop;
                    $this->data['shop_id'] = $v['shop_id'];
                }
            }

            $this->data['shop_num'] = count($this->data['shop']);

            return $this->data;
        } else {
            Dever::alert('登录失败，手机号不存在');
        }
    }

    # 获取验证码
    public function mcode()
    {
        $mobile = Dever::load('passport/reg')->checkMobileExists(1, true, 'shop/member');

        $msg = Dever::load('passport/reg')->getMcode_action($mobile);
        
        return $msg;
    }

    # 申请合作
    public function apply()
    {
        $data['name'] = Dever::input('name');
        $data['mobile'] = Dever::input('mobile');
        $data['city'] = Dever::input('city');

        if (!$data['name']) {
            Dever::alert('请输入姓名');
        }
        if (!$data['mobile']) {
            Dever::alert('请输入手机号');
        }
        if (!$data['city']) {
            Dever::alert('请选择城市');
        }

        $info = Dever::db('shop/apply')->find($data);

        $data['truename'] = $data['name'];
        # 根据城市查找省份
        $county = Dever::db('area/county')->find($data['city']);
        if ($county) {
            $data['city'] = $county['city_id'];
        }
        $city = Dever::db('area/city')->find($data['city']);
        $data['province'] = $city['province_id'];
        $data['area'] = $data['province'] . ',' . $data['city'];
        if ($county) {
            $data['area'] .= ',' . $county['id'];
        }

        # 获取经纬度
        list($data['lng'], $data['lat'], $data['map']) = Dever::load('shop/lib/info')->geo($data['city'], $city['name']);

        $data['status'] = 1;

        if (!$info) {
            
            Dever::db('shop/apply')->insert($data);
        } else {
            $data['where_id'] = $info['id'];
            Dever::db('shop/apply')->insert($data);
        }

        $this->data['msg'] = '提交成功，我们会在3个工作日内与您联系，感谢您的信任。';
        return $this->data;
    }
}