<?php
namespace app\admin\controller;
use think\Controller;

class System extends Base 
{
    public function index()
    {
      if(request()->isPost()){
        
        $_post=input('post.');

        foreach ($_post as $k => $v) {
               $post[]=$k;
            }
            $_ennameres=db('system')->field('enname')->select();
            foreach ($_ennameres as $k => $v) {
                $ennameres[]=$v['enname'];
            }
            foreach ($ennameres as $v) {
                if(!in_array($v, $post)&&$v!='logo'&&$v!='zfb'&&$v!='wxewm'&&$v!='erweima'){
                    $arr[]=$v;
                }
            }

             //处理图片上传
            if($_FILES['erweima']['tmp_name']!=''){

                $data['erweima']=$this->ewmupload($_FILES['erweima']);
                $newlogo="/public/uploads/".$data['erweima'];
                $oldSystemres=db('system')->order('id')->select();
               
              db('system')->where(array('enname'=>'erweima'))->update(array('value'=>$newlogo));
              
                $oldLogo = '';
                foreach ($oldSystemres as $i => $v){
                    if ($v['enname'] == 'erweima'){
                        $oldLogo = $v['value'];
                    }
                }

                if(is_file(ROOT_PATH.$oldLogo)){
                    unlink(ROOT_PATH.$oldLogo);
                }
               
            }

             //处理图片上传
            if($_FILES['wxewm']['tmp_name']!=''){

                $data['wxewm']=$this->wxewmupload($_FILES['wxewm']);
                $newlogo="/public/uploads/".$data['wxewm'];
                $oldSystemres=db('system')->order('id')->select();
               
              db('system')->where(array('enname'=>'wxewm'))->update(array('value'=>$newlogo));
              
                $oldLogo = '';
                foreach ($oldSystemres as $i => $v){
                    if ($v['enname'] == 'wxewm'){
                        $oldLogo = $v['value'];
                    }
                }

                if(is_file(ROOT_PATH.$oldLogo)){
                    unlink(ROOT_PATH.$oldLogo);
                }
               
            }

             //处理图片上传
            if($_FILES['zfb']['tmp_name']!=''){

                $data['zfb']=$this->zfbupload($_FILES['zfb']);
                $newlogo="/public/uploads/".$data['zfb'];
                $oldSystemres=db('system')->order('id')->select();
               
              db('system')->where(array('enname'=>'zfb'))->update(array('value'=>$newlogo));
              
                $oldLogo = '';
                foreach ($oldSystemres as $i => $v){
                    if ($v['enname'] == 'zfb'){
                        $oldLogo = $v['value'];
                    }
                }

                if(is_file(ROOT_PATH.$oldLogo)){
                    unlink(ROOT_PATH.$oldLogo);
                }
               
            }
            
            //处理图片上传
            if($_FILES['logo']['tmp_name']!=''){

                $data['logo']=$this->upload($_FILES['logo']);
                $newlogo="/public/uploads/".$data['logo'];
                $oldSystemres=db('system')->order('id')->select();
               
              db('system')->where(array('enname'=>'logo'))->update(array('value'=>$newlogo));
              
                $oldLogo = '';
                foreach ($oldSystemres as $i => $v){
                    if ($v['enname'] == 'logo'){
                        $oldLogo = $v['value'];
                    }
                }

                if(is_file(ROOT_PATH.$oldLogo)){
                    unlink(ROOT_PATH.$oldLogo);
                }
               
            }

            

          

          if($_post){
                foreach ($_post as $k => $v) {
                   db('system')->where(array('enname'=>$k))->update(array('value'=>$v));
                } 

               
               $this->success('修改配置项成功！',url('index'));
                
            }else{
               $this->error('数据发送有误！');
            }
            return;

      }
      

    
      $systemres=db('system')->order('id')->select();
      $this->assign([
            'systemres'=>$systemres,
            
        ]);
        return view();
    }

 public function ewmupload(){
    $file = request()->file('erweima');
    if($file){
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       if($info){
         return $info->getSaveName();
       }else{
            // 上传失败获取错误信息
            echo $file->getError();
            die;
        }
    }
   }

   public function upload(){
    $file = request()->file('logo');
    if($file){
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       if($info){
         return $info->getSaveName();
       }else{
            // 上传失败获取错误信息
            echo $file->getError();
            die;
        }
    }
   }


   public function zfbupload(){
    $file = request()->file('zfb');
    if($file){
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       if($info){
         return $info->getSaveName();
       }else{
            // 上传失败获取错误信息
            echo $file->getError();
            die;
        }
    }
   }

   public function wxewmupload(){
    $file = request()->file('wxewm');
    if($file){
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       if($info){
         return $info->getSaveName();
       }else{
            // 上传失败获取错误信息
            echo $file->getError();
            die;
        }
    }
   }

   public function uploadwappic(){
    $file = request()->file('wappic');
    if($file){
       $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
       if($info){
         return $info->getSaveName();
       }else{
            // 上传失败获取错误信息
            echo $file->getError();
            die;
        }
    }
   }
   

    
  
   
}
