<?php

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

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

    /**
     * 招聘岗位列表
     */
    public function index(){
        $model_recruit = Model('Recruit');
        $recruit_keywords = input('post.product_keywords');
        $recruit_type = input('post.product_type');
        $condition = array();
        if($recruit_keywords){
            $condition['product_keywords'] = $recruit_keywords;
        }
        if($recruit_type){
            $condition['product_type'] = $recruit_type;
        }
        $recruit_list = $model_recruit->getRecruit($condition,'*',10);
        for($i=0;$i<count($recruit_list);$i++){
            $recruit_list[$i]['add_time'] = date("Y-m-d h:i:s",$recruit_list[$i]['add_time']);
            $recruit_list[$i]['update_time'] = date("Y-m-d h:i:s",$recruit_list[$i]['update_time']);
        }

        $allpower = $this->qxhans();
        $this->assign('allpower',$allpower);
        $this->assign('recruit_list', $recruit_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'),
                    'address' => input('post.address'),
                    'add_time' => time(),
                    'update_time' => time(),
                    'status' => '0',
                );
            }else{
                $data = array(
                    'name' => input('post.name'),
                    'sort' => input('post.sort'),
                    'detail' => input('post.detail'),
                    'address' => input('post.address'),
                    'add_time' => time(),
                    'update_time' => time(),
                    'status' => input('post.status'),
                );
            }

            $result = model('Recruit')->addRecruit($data);
            if ($result){
                $this->success(lang('add_succ'), url('Recruit/index'));
            }
            $this->error(lang('add_fail'));
        } else {
            $recruit = array('status'=>1);
            $this->assign('recruit', $recruit);
            $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'),
                'address' => input('post.address'),
                'update_time' => time(),
                'status' => input('post.status'),
            );

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

            $recruit = model('Recruit')->getOneRecruit(['id' => $id]);
            $this->assign('recruit', $recruit);
            $this->setAdminCurItem('edit');
            return $this->fetch('form');
        }
    }


    /**
     * 删除招聘职位
     * @return mixed
     */
    function del()
    {
        $id = intval(input('param.id'));
        if ($id) {
            $condition['id'] = $id;
            $result = model('Recruit')->deleteRecruit($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'));
        }
    }
}