<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request as Req;
use App\Http\Controllers\Controller;
use Request;
class TypeNameController extends Controller
{
    //首页
    public function index(){
        $dt=\App\Lib\DataTable\DataTable::init();
        $dt->setLang('type_name');
        $dt->enableCheckBox();
        $dt->addColsFields('id');
        $dt->addColsFields('name');
        $dt->addColsFields('sort');
        $dt->addColsFields('remark');
        $dt->addColsFields('type_id');
        $dt->addColsFields('create_at');
        $dt->setToolBar(array('edit'),array('sort' => false));
        return  view('admin.TypeName/index',$dt->render());
    }

    //数据源
    public function dataSource(Request $req){
        $name = isset($req->name) ? trim($req->name) : null;
        $limit = isset($req->limit) ? trim($req->limit) : 20;
        $page = isset($req->page) ? trim($req->page) : null;
        $where=array();
        if (!empty($name)) {
            $where[] = array('name', 'like', '%' . $name . '%');
        }
        $data = \App\Models\TypeName::getList($where,$limit);
        $tn = trans('type_name');
        foreach ($data['data'] as $k => $v) {
            // $data['data'][$k]['type_id']=$tn['typed'][$v['type_id']];
        }

        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);

    }
   //添加
   function add(Req $req){
        if(!$req->isMethod('post')){
            return view('admin.TypeName/add');
        }else{
            $name=$req->name?trim($req->name):'';
            $sort=$req->sort?intval($req->sort):1;
            $remark=$req->remark?trim($req->remark):'';
            $type=$req->type?$req->type:'';
        
            if(empty($name)){
                return responseToJson(-240006);
            }
            $data=array(
                'name'=>$name,
                'sort'=>$sort,
                'remark'=>$remark,
                'type_id'=>implode($type,','),
                'create_at'=>date('Y-m-d H:i:s'),
            );
            $model=\App\Models\TypeName::addGroup($data);
            return responseToJson($model);
        }        
    }
	//查看
    function view(Request $req){
       return $this->edit($req);
    }

   function delete(Req $req) {
        $loginname = '';
        $id = $req->input('id');
        if (empty($id)) {
            return responseToJson(-2001); //id����Ϊ��
        }
        $ids = explode(',', $id);
        if (!is_array($ids) && intval($ids) < 0) {
            return responseToJson(-2002); //id����
        }
        if (is_array($ids) && count($ids) > 0) {
            foreach ($ids as $k => $v) {
                if (intval($v) < 1) {
                    unset($ids[$k]);
                }
            }
        }
        $rows = \App\Models\TypeName::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003); //id����
        }
        return responseToJson(1); //id����
    }
    //修改
    function edit(Req $req){
        $id=$req->id;
        if(intval($id)<1){
            return -1;
        }
        if(!$req->isMethod('post')){

            $data=\App\Models\TypeName::where('id',$id)->first();
            if(!$data){
                return -2;
            }
            $data=$data->toArray();
            return view('admin.TypeName/add',array('admin'=>$data));
        }else{
            $model=\App\Models\TypeName::where('id',$id)->first();
            $model->name=trim($req->input('name'));
            $model->sort=trim($req->input('sort'));
            $model->remark=trim($req->input('remark'));
            $model->type_id=trim($req->input('type_id'));
            $model->create_at=date('Y-m-d H:i:s');
            $model->save();
            return responseToJson(1);
        }
    }

}


