<?php

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

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

    /**
     * 我们的团队
     */
    public function index(){
        $model_ourteam = Model('OurTeam');
        $ourteam_keywords = input('post.product_keywords');
        $ourteam_type = input('post.product_type');
        $condition = array();
        if($ourteam_keywords){
            $condition['product_keywords'] = $ourteam_keywords;
        }
        if($ourteam_type){
            $condition['product_type'] = $ourteam_type;
        }
        $ourteam_list = $model_ourteam->getOurTeam($condition,'*',10);
        for($i=0;$i<count($ourteam_list);$i++){
            $ourteam_list[$i]['add_time'] = date("Y-m-d h:i:s",$ourteam_list[$i]['add_time']);
            $ourteam_list[$i]['update_time'] = date("Y-m-d h:i:s",$ourteam_list[$i]['update_time']);
        }

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

    }

    /**
     * 添加团队
     * @return mixed
     */
    public function add()
    {
        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        if (request()->isPost()) {
            $data = array(
                'name' => input('post.name'),
                'info' => input('post.info'),
                'add_time' => time(),
                'update_time' => time(),
            );
            $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('OurTeam')->add($data);
            if ($result){
                $this->success(lang('add_succ'), url('OurTeam/index'));
            }
            $this->error(lang('add_fail'));
        } else {
            $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'),
                'info' => input('post.info'),
                'update_time' => time(),
            );
            $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('OurTeam')->edit(['id' => $id], $data);
            if ($result >= 0) {
                $this->success(lang('edit_succ'), 'OurTeam/index');
            } else {
                $this->error(lang('edit_fail'));
            }
        } else {

            $ourteam = model('OurTeam')->getOneOurteam(['id' => $id]);
            $this->assign('ourteam', $ourteam);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }


    /**
     * 删除团队
     * @return mixed
     */
    function del()
    {
        $id = intval(input('param.id'));
        if ($id) {
            $condition['id'] = $id;
            $result = model('OurTeam')->deleteOurTeam($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'));
        }
    }
}