<?php

namespace app\controller;

use app\model\EnterpriseUser as EnterpriseUserModel;
use think\facade\Filesystem;
use think\Request;

class EnterpriseUser
{
    public function index(Request $request)
    {
        $params = $request->param();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        
        $keyword = $params['keyword'] ?? '';

        $where = [];
        $where['enterprise_id'] = ['=',$params['enterprise_id']];
        $query = EnterpriseUserModel::where($where);

        $list = $query->paginate([
            'list_rows' => $pageSize,
            'page' => $page,
            'query' =>$params
        ]);
        
        return json($list);
    }

    
    public function read($id)
    {
        $result = EnterpriseUserModel::find($id);
        if(!$result){
            return json(['code' => 1, 'msg' => '数据不存在']);
        }
        
         return json(['msg'=>'','code'=>0,'data'=>$result]);
    }

    public function update(Request $request)
    {
        $params = $request->param();
        
        $info = EnterpriseUserModel::find($params['id']);
        if(!$info){
            return json(['code' => 1, 'msg' => '数据不存在']);
        }
        unset($params['create_time']);
        $result = $info->save($params);
        
        if($result){
            return json(['msg'=>'更新成功','code'=>0,'data'=>[]]);
        }else{
             return json(['msg'=>'更新失败,请检查~','code'=>1,'data'=>[]]);
        }
    }
    
     public function save(Request $request)
    {
        $params = $request->param();
        
        $result = EnterpriseUserModel::create([
            'enterprise_id'=>$params['enterprise_id'],
            'phone'=>$params['phone'],
            'group_id'=>$params['group_id']
            ]);
        
        if($result){
            return json(['msg'=>'新增成功','code'=>0,'data'=>[]]);
        }else{
             return json(['msg'=>'新增失败,请检查~','code'=>1,'data'=>[]]);
        }
        
    }
    public function delete(Request $request)
    {
        $data = $request->param();
        
        
        if(!EnterpriseUserModel::find($data['id'])){
            return json(['code' => 1, 'msg' => '数据不存在']);
        }
        $result = EnterpriseUserModel::destroy($data['id']);
        if ($result) {
            return json(['code' => 0, 'msg' => '删除成功']);
        } else {
            return json(['code' => 1, 'msg' => '删除失败']);
        }
       
    }
}