<?php
namespace app\worker\controller;

use app\worker\BaseController;
use app\common\model\Worker as WorkerModel;

class Worker extends BaseController
{
	
	public function workerForm()
	{
		$workerid = $this->access_worker['id'];
		$worker = WorkerModel::findOrEmpty($workerid);
		return view('worker/workerform',[
			'worker'	=> $worker
		]);
	}
	
	public function editWorker()
	{
		$workerid = $this->access_worker['id'];
		$data = [
			'title'			=> input('title/s', ""),
			'ftitle'		=> input('ftitle/s', ""),
			'tilpic'  		=> input('tilpic/s', ""),
			'realname'  	=> input('realname/s', ""),
			'mobile'    	=> input('mobile/s', ""),
			'position'    	=> input('position/s', ""),
			'weixin'    	=> input('weixin/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')
		];
		$worker = WorkerModel::find($workerid);
		$worker->save($data);
		exit(json_encode(array(
			'code' 	=> 0
		)));
	}
	
	
	public function myPassword() 
	{
		return view('worker/mypassword');
	}
	
	public function editMyPassword()
	{
		$worker = $this->access_worker;
		$oldpassword = input('oldpassword');
		if ( $worker['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'	=> "两次输入的新密码不一致。"
			)));
		}
		WorkerModel::update(['password'=>md5($password)], ['id'=>$worker['id']]);
		session('access_worker', null);
		echo json_encode(array(
			'code' 	=> 0
		));
	}
	
	

}
