<?php

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

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

    /**
     * 开发案例
     */
    public function index(){
        $model_Case = Model('DevelopmentCase');
        $case_keywords = input('post.product_keywords');
        $case_type = input('post.product_type');
        $condition = array();
        if($case_keywords){
            $condition['product_keywords'] = $case_keywords;
        }
        if($case_type){
            $condition['product_type'] = $case_type;
        }
        $case_list = $model_Case->getDevelopmentCase($condition,'*',10);
        for($i=0;$i<count($case_list);$i++){
            $case_list[$i]['add_time'] = date("Y-m-d h:i:s",$case_list[$i]['add_time']);
            $case_list[$i]['update_time'] = date("Y-m-d h:i:s",$case_list[$i]['update_time']);
        }

        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('case_list', $case_list);
        $this->assign('show_page', $model_Case->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'),
                    'type' => input('post.type'),
                    'add_time' => time(),
                    'update_time' => time(),
                    'status' => '0',
                );
            }else{
                $data = array(
                    'name' => input('post.name'),
                    'sort' => input('post.sort'),
                    'type' => input('post.type'),
                    '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('DevelopmentCase')->addCase($data);
            if ($result){
                $this->success(lang('add_succ'), url('DevelopmentCase/index'));
            }
            $this->error(lang('add_fail'));
        } else {
            $case = array('status'=>1);
            $this->assign('case', $case);
            $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'),
                '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('DevelopmentCase')->edit(['id' => $id], $data);
            if ($result >= 0) {
                $this->success(lang('edit_succ'), 'DevelopmentCase/index');
            } else {
                $this->error(lang('edit_fail'));
            }
        } else {

            $case = model('DevelopmentCase')->getOneCase(['id' => $id]);
            $this->assign('case', $case);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }


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