<?php

namespace App\Http\Controllers\Manage\Manage;

use App\Http\Controllers\Manage\BaseController;
use App\Http\Controllers\Manage\Manage\Transformers\ManageTransformer1;
use App\Http\Controllers\Manage\Manage\Transformers\ManageTransformer;
use App\Modules\Channel\Services\ChannelService;
use App\Modules\Channel\Services\ChannelUserService;
use App\Modules\Manage\Services\ManageService;
use App\Modules\Manage\Models\Manage;
use Illuminate\Http\Request;
use Cache;
use Redis;

/**
 * 账户管理
 * @author zhoulingjie
 */
class AccountController extends BaseController
{

    function getAccounts(Request $request) {
    	//商务，只获取到当前商务下的渠道列表
    	if (!$this->checkEditRole()) {
    		return response()->error('MANAGE_PRIV_ERROR');
    	}
        $params = [];
        $accounts = Manage::getAll(1);

        return response()->pagination(new ManageTransformer(), $accounts);

    }
    
    function addAccount(Request $request) {
    	$account = $request->has('account') ? trim($request->input('account')) : '';
    	$number = $request->has('number') ? trim($request->input('number')) : '';
    	$password = $request->has('password') ? trim($request->input('password')) : '';
    	$nickname = $request->has('nickname') ? trim($request->input('nickname')) : '';
    	$role = $request->has('role') ? trim($request->input('role')) : '';
  
    	//商务，只获取到当前商务下的渠道列表
    	if (!$this->checkEditRole()) {
    		return response()->error('MANAGE_PRIV_ERROR');
    	}
    	
    	// 是否存在
    	$origin_account = Manage::getByAccount($account);
    	if(!empty($origin_account)){
    		return response()->error('MANAGE_USER_EXIST');
    	}
    	$new_password = md5($password."^-^zhuishuyun^_^");
    	// 添加
    	$params = [
    	   'account'=>$account,
    	   'number'=>$number,
    	   'password'=>$new_password,
    	   'nickname'=>$nickname,
    	   'role'=>$role,
    	];
    	
    	Manage::add($params);
    
    	return response()->success();
    }
    
    function updateAccount(Request $request) {
    	$account = $request->has('account') ? trim($request->input('account')) : '';
    	$number = $request->has('number') ? trim($request->input('number')) : '';
    	$password = $request->has('password') ? trim($request->input('password')) : '';
    	$nickname = $request->has('nickname') ? trim($request->input('nickname')) : '';
    	$role = $request->has('role') ? trim($request->input('role')) : '';
    	$is_enabled = $request->has('is_enabled') ? trim($request->input('is_enabled')) : '1';
    	 
    	//商务，只获取到当前商务下的渠道列表
    	if (!$this->checkEditRole()) {
    		return response()->error('MANAGE_PRIV_ERROR');
    	}
    	// 是否存在
    	$origin_account = Manage::getByAccount($account);
    	if(empty($origin_account)){
    		return response()->error('ACCOUNT_NOT_EXIST');
    	}
    	$new_password = md5($password."^-^zhuishuyun^_^");
    	$origin_account->number=$number;
    	$origin_account->password=$new_password;
    	$origin_account->nickname=$nickname;
    	$origin_account->role=$role;
    	$origin_account->is_enabled=$is_enabled;
    	$origin_account->save();
    
    	return response()->success();
    }
    
    function get_role_list(){
    	$data= [
    	  'product_operate'=>'产品运营',
    	  'finance'=>'财务',
    	  'content_operate'=>'内容运营',
    	  'content_leader'=>'内容主管',
    	  'business'=>'商务',
    	  'business_leader'=>'商务主管',
    	  'admin'=>'管理员',
    	  'customer'=>'客服',
    	];
    	return response()->success($data);
    }
    
    function get_edit_user_priv_list(){
    	$data= [
    	  'zsy_sjj',
    	  'zsy_zlj',
    	  'zsy_gdy',
    	];
    	return response()->success($data);
    }

}