<?php

namespace app\admin\controller;

use think\Validate;
use think\Lang;

class Homeinfo extends AdminControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/member.lang.php');
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function index()
    {
        /*$a='sadsffdsfds';
        echo substr($a,0,20);die;*/
        $getHomeInformation = model('homeinformation')->getHomeInformation();
        $this->assign('information', $getHomeInformation);
        $this->setAdminCurItem('index');

        return $this->fetch();
    }

    public function edit()
    {
        $homeinformation_id = input('param.homeinformation_id');
        if (!request()->isPost()) {
            $getHomeInformation = model('homeinformation')->findHomeInformation($homeinformation_id);
            $this->assign('information', $getHomeInformation);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        } else {
            $updateData = array(
                'homeinformation_name' => input('post.homeinformation_name'),
                'homeinformation_content' => input('post.homeinformation_content'),
                'homeinformation_en' => input('post.homeinformation_en'),
                'homeinformation_bgcolor' => input('post.homeinformation_bgcolor'),
            );
            $files = request()->file('homeinformation_bgImg');
            if($files){
                $imgurl = DS_THEME_STYLE_URL . 'images';
                $begin = strrpos(input('post.bgImgName'), '/');
                $end = strrpos(input('post.bgImgName'), '.');
                $oldFileName = substr(input('post.bgImgName'), $begin + 1, $end - $begin - 1);
                $oldFile = $imgurl . "\\" . substr(input('post.bgImgName'),$begin + 1);
                if(file_exists($oldFile)){
                    unlink($oldFile);
                };
                $files = $files->setSaveName($oldFileName);//设置保存文件名
                $imgt = $files->move($imgurl, $savename = $oldFileName, $replace = true);
                if($imgt){
                    $update_array['site_logowx'] = $oldFileName.'.png';
                    $img = 1;
                }
            }
            $result = model('homeinformation')->updateHomeInformation($homeinformation_id, $updateData);
            if ($result || $img) {
                dsLayerOpenSuccess(lang('member_edit_succ'));
            } else {
                $this->error(lang('member_edit_fail'));
            }
        }
    }

}
