<?php

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

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

    /**
     * 领导团队列表
     */
    public function index(){
        $model_leaderteam = Model('LeaderTeam');
        $leaderteam_keywords = input('post.product_keywords');
        $leaderteam_type = input('post.product_type');
        $condition = array();
        if($leaderteam_keywords){
            $condition['product_keywords'] = $leaderteam_keywords;
        }
        if($leaderteam_type){
            $condition['product_type'] = $leaderteam_type;
        }
        $leaderteam_list = $model_leaderteam->getLeaderTeam($condition,'*',10);
        for($i=0;$i<count($leaderteam_list);$i++){
            $leaderteam_list[$i]['add_time'] = date("Y-m-d h:i:s",$leaderteam_list[$i]['add_time']);
            $leaderteam_list[$i]['update_time'] = date("Y-m-d h:i:s",$leaderteam_list[$i]['update_time']);
        }

        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('leaderteam_list', $leaderteam_list);
        $this->assign('show_page', $model_leaderteam->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_ch' => input('post.name_ch'),
                'name_en' => input('post.name_en'),
                'gender' => input('post.gender'),
                'age' => input('post.age'),
                'position' => input('post.position'),
                'department' => input('post.department'),
                '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('LeaderTeam')->add($data);
            if ($result){
                $this->success(lang('add_succ'), url('LeaderTeam/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_ch' => input('post.name_ch'),
                'name_en' => input('post.name_en'),
                'gender' => input('post.gender'),
                'age' => input('post.age'),
                'position' => input('post.position'),
                'department' => input('post.department'),
                '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('LeaderTeam')->edit(['id' => $id], $data);
            if ($result >= 0) {
                $this->success(lang('edit_succ'), 'LeaderTeam/index');
            } else {
                $this->error(lang('edit_fail'));
            }
        } else {

            $leaderteam = model('LeaderTeam')->getOneleaderteam(['id' => $id]);
            $this->assign('leaderteam', $leaderteam);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }


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