<?php

namespace app\admin\controller;
use think\Lang;

class DevelopmentServer extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/product.lang.php');
    }

    /**
     * 开发服务
     */
    public function index(){
        $model_Server = Model('DevelopmentServer');
        $server_keywords = input('post.product_keywords');
        $server_type = input('post.product_type');
        $condition = array();
        if($server_keywords){
            $condition['product_keywords'] = $server_keywords;
        }
        if($server_type){
            $condition['product_type'] = $server_type;
        }
        $server_list = $model_Server->getDevelopmentServer($condition,'*',10);
        for($i=0;$i<count($server_list);$i++){
            $server_list[$i]['add_time'] = date("Y-m-d h:i:s",$server_list[$i]['add_time']);
            $server_list[$i]['update_time'] = date("Y-m-d h:i:s",$server_list[$i]['update_time']);
        }

        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('server_list', $server_list);
        //$this->assign('show_page', $Case_list->page_info->render());
        $this->setAdminCurItem('index');
        return $this->fetch();

    }

    /**
     * 添加开发服务
     * @return mixed
     */
    public function add()
    {
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        if (request()->isPost()) {
            if(input('post.status') == ""){
                $data = array(
                    'name' => input('post.name'),
                    'sort' => input('post.sort'),
                    'detail' => input('post.detail'),
                    'add_time' => time(),
                    'update_time' => time(),
                    'status' => '0',
                );
            }else{
                $data = array(
                    'name' => input('post.name'),
                    'sort' => input('post.sort'),
                    'detail' => input('post.detail'),
                    'add_time' => time(),
                    'update_time' => time(),
                    'status' => input('post.status'),
                );
            }

            $imgurl = DS_THEME_STYLE_URL . 'images/team';
            $numurl = '/static/home/images/team';
            $file = request()->file('image');
            if($file){
                $imgname = time().'.jpg';
                $file = $file->setSaveName($imgname);//设置保存文件名
                $imgo = $file->move($imgurl, $savename = $imgname, $replace = true);
                if($imgo){
                    $data['img_url'] =$numurl.'/'.$imgname;
                }
            }

            $result = model('DevelopmentServer')->add($data);
            if ($result){
                $this->success(lang('add_succ'), url('DevelopmentServer/index'));
            }
            $this->error(lang('add_fail'));
        } else {
            $server = array('status'=>1);
            $this->assign('server', $server);
            $this->setAdminCurItem('add');
            return $this->fetch('form');
        }
    }

    /**
     * 修改开发服务
     * @return mixed
     */
    public function edit()
    {
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $id = input('param.id');
        if ($id <= 0) {
            $this->error('系统错误');
        }
        $condition['id'] = $id;
        if (request()->isPost()) {
            $data = array(
                'name' => input('post.name'),
                'sort' => input('post.sort'),
                'detail' => input('post.detail'),
                'update_time' => time(),
                'status' => input('post.status'),
            );
            $imgurl = DS_THEME_STYLE_URL . 'images/team';
            $numurl = '/static/home/images/team';
            $file = request()->file('image');
            if($file){
                $imgname = time().'.jpg';
                $file = $file->setSaveName($imgname);//设置保存文件名
                $imgo = $file->move($imgurl, $savename = $imgname, $replace = true);
                if($imgo){
                    $data['img_url'] =$numurl.'/'.$imgname;
                }
            }

            $result = model('DevelopmentServer')->edit(['id' => $id], $data);
            if ($result >= 0) {
                $this->success(lang('edit_succ'), 'DevelopmentServer/index');
            } else {
                $this->error(lang('edit_fail'));
            }
        } else {

            $server = model('DevelopmentServer')->getOneServer(['id' => $id]);
            $this->assign('server', $server);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }


    /**
     * 删除开发服务
     * @return mixed
     */
    function del()
    {
        $id = intval(input('param.id'));
        if ($id) {
            $condition['id'] = $id;
            $result = model('DevelopmentServer')->deleteServer($condition);
            if ($result) {
                ds_json_encode(10000, lang('del_succ'));
            } else {
                ds_json_encode(10001, lang('del_fail'));
            }
        } else {
            ds_json_encode(10001, lang('param_error'));
        }
    }
}