<?php
namespace app\agent\controller;

use app\agent\BaseController;
use app\common\model\Worker as WorkerModel;
use app\common\model\Agent as AgentModel;

class Agent extends BaseController
{
	
	public function agentForm()
	{
		$agentid = $this->access_agent['id'];
		$agent = AgentModel::with('worker')->findOrEmpty($agentid);
		return view('agent/agentform',[
			'agent'		=> $agent
		]);
	}
	
	public function editAgent()
	{
		$agentid = $this->access_agent['id'];
		$data = [
			'tilpic'		=> input('tilpic/s'),
			'realname'  	=> input('realname/s'),
			'mobile'    	=> input('mobile/s'),
			'priority'		=> input('priority/d'),
			'picall'		=> input('picall/a', array()),
			'telephone'		=> input('telephone/s'),
			'latitude'		=> input('latitude/f'),
			'longitude'		=> input('longitude/f'),
			'province'		=> input('province/s'),
			'city'			=> input('city/s'),
			'district'		=> input('district/s'),
			'address'		=> input('address/s'),
			'details'		=> input('details/s'),
		];
		$agent = AgentModel::find($agentid);
		$agent->save($data);
		
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	
	public function myPassword() 
	{
		return view('agent/mypassword');
	}
	
	public function editMyPassword()
	{
		$agent = $this->access_agent;
		$oldpassword = input('oldpassword');
		if ( $agent['password']!==md5($oldpassword) ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "当前密码不正确。"
			)));
		}
		$password = input('password');
		$repassword = input('repassword');
		if ( $password!==$repassword ){
			exit(json_encode(array(
				'code' 	=> 1,
				'msg'	=> "两次输入的新密码不一致。"
			)));
		}
		AgentModel::update(['password'=>md5($password)], ['id'=>$agent['id']]);
		session('access_agent', null);
		echo json_encode(array(
			'code' 	=> 0
		));
	}
	
	

}
