<?php
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
$tpl ="shop/";
$settings = $this->module['config'];
$sql = "SELECT * FROM" . tablename("evzg_express_shop") . "WHERE openid='{$_W['openid']}' AND 	uniacid='{$_W['uniacid']}'";
$shop = pdo_fetch($sql);
$op = ! empty($_GPC['op']) ? $_GPC['op'] : 'display';
if($op=='post'){
    $name = $_GPC['name'];
    $mobile = $_GPC['mobile'];
    if($shop['code'] != $_GPC['code']){
        message("验证码错误",referer(),'error');
    }
    if(empty($name)){
        message("店铺名字不能为空",referer(),'error');
    }
    if(empty($mobile)){
        message("联系方式不能为空",referer(),'error');
    }
    $image = $_GPC['thumb'];
    if (strstr($image,",")){
        $image = explode(',',$image);
        $image = $image[1];
    }
    $foldername ="evzg_express";
    if (!file_exists("../attachment/images/".$foldername)) {
        mkdir("../attachment/images/".$foldername, 0777, true);
    }
    $thumb = '../attachment/images/'.$foldername.'/'.date('YmdHis').rand(1000,9999).'.jpg';
    file_put_contents($thumb, base64_decode($image));//返回的是字节数
    $data['uniacid'] = $_W['uniacid'];
    $data['name'] = $name;
    $data['openid'] = $_W['openid'];
    $data['mobile'] = $mobile;
    $data['thumb'] = $thumb;
    $data['createtime'] = time();
    $data['enabled'] = 1;
    if(!empty($shop)){
        $result = pdo_update("evzg_express_shop",$data,array('openid'=>$_W['openid'],'mobile'=>$_GPC['mobile']));
    }else{
        $result = pdo_insert("evzg_express_shop",$data);
    }

    if($result){
        message("入住成功",$this->createMobileUrl('shop'),'success');
    }else{
        message("入住失败",referer(),'error');
    }
}

include $this->template($tpl.'login');