<?php
namespace MpSmi\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class BasicController extends BaseController{
    /**
     * 商家详情
     */
    public function storeDetail(){
        if($_POST){
            $store = M("stores");
            // dump($_FILES);die;
            if($_FILES['logo1']['error'] == 0 || $_FILES['logo2']['error'] == 0){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   = 2097152 ;// 设置附件上传大小2m
                $upload->exts      = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  = './Uploads/'; // 设置附件上传根目录
                $upload->savePath  = 'goods_img/'; // 设置附件上传（子）目录
                // 上传文件 
                $info   =   $upload->upload();
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功
                    $stores  = $store->where("id=".I("id"))->find(); //查询oldlogo,old store_photo
                    $oldlogo = "./Uploads/".$stores['logo']; //oldlogo 路径
                    $oldphoto= "./Uploads/".$stores['store_photo']; // old store_photo路径
                    $newlogo = $info['logo1']['savepath'].$info['logo1']['savename'];
                    $newphoto= $info['logo2']['savepath'].$info['logo2']['savename'];
                    $logo    = $newlogo ?: I("logoold1");
                    $photo   = $newphoto ?: I("logoold2");
                }
            }else{
                $logo = I("logoold1"); //无上传新logo
                $photo = I("logoold2");
            }
            $store->create();
            $store->logo = $logo;
            $store->store_photo = $photo;
            $result = $store->save();
            if($result){
                if($newlogo){
                    unlink($oldlogo); //上传新logo成功，删除oldlogo
                }
                if($newphoto){
                    unlink($oldphoto); //
                }
                alertMsg("修改成功", U('Basic/storeDetail'));
            }       
        }

        $id = session("store_id"); 
        $info = M("stores")
            ->join("s LEFT JOIN `am_users_oa` uo ON s.`openid`=uo.`openid`")
            ->join("LEFT JOIN `am_stores_enter` se ON s.`openid`=se.`openid`")
            ->field("s.*,uo.`nickname`,uo.`sex`,uo.`headimgurl`,se.`business_licence`,se.`fcc`")
            ->where("s.`id`={$id}")
            ->find();
        // 省 市 区
        $region = M("regionCounty")
            ->join("county LEFT JOIN `am_region_city` city ON county.`city_id`=city.`id`")
            ->join("LEFT JOIN `am_region_province` p ON city.`province_id`=p.`id`")
            ->where("county.`id`={$info['county_id']}")
            ->field("p.`name` AS `province`,city.`name` AS `city`,county.`name` AS `county`")
            ->find();
        $info['province'] = $region['province'];
        $info['city']     = $region['city'];
        $info['county']   = $region['county'];
        $this->assign("info", $info);
        $this->display("storeDetail");die;
    }
    //商家公告
    public function storeNotice(){
        if($_POST){
            $data   = array("notice" => I("notice"));
            $result = M("stores")->where(array("id" => I("id")))->save($data);
            if($result){
                alertMsg("修改成功", U('Basic/storeNotice'));
            }
        }
        $info = M("stores")->where(array("id" => session("store_id")))->find();
        $this->assign("info",$info);
        $this->display();
    }
    //起送价配送费
    public function storeSend(){
        if($_POST){
            $data   = array("send_price" => I("send_price"),"send_cost" => I("send_cost"));
            $result = M("stores")->where(array("id" => I("id")))->save($data);
            if($result){
                alertMsg("修改成功", U('Basic/storeSend'));
            }
        }
        $info = M("stores")->where(array("id" => session("store_id")))->find();
        $this->assign("info",$info);
        $this->display();
    }
    //开店有效期
    public function storeInvalid(){
        $info = M("stores")->where(array("id" => session("store_id")))->find();
        $this->assign("info",$info);
        $this->display();
    } 
    //修改密码
    public function storePwd(){
        if($_POST){
            $passwordold   = I("passwordold");
            $passwordnew   = I("passwordnew");
            $passwordnews  = I("passwordnews");
            if(!$passwordold){
                alertMsg("请输入原密码");
            }elseif(!$passwordnew || !$passwordnews){
                alertMsg("请输入新密码或确认密码");
            }elseif ($passwordnew != $passwordnews) {
                alertMsg("新密码与确认密码不一致");
            }else{
                $passwordold = md5($passwordold);
                $checkpwd = M("stores")->where(array("id"=>I("id"),"password"=>$passwordold))->find();
                if($checkpwd == NULL){
                    alertMsg("原密码错误");
                }else{
                    $data['password'] = md5($passwordnew);
                    $result = M("stores")->where(array("id" => I("id")))->save($data);
                    if($result){
                        alertMsg("修改成功", U('storePwd'));
                    }else{
                        alertMsg("修改失败", U('storePwd'));
                    }
                }
            }      
        }
        $info = M("stores")->where(array("id" => session("store_id")))->find();
        $this->assign("info",$info);
        $this->display();
    }
}