<?php
	require_once "pageBase.php";
	require_once "../dal/userDAL.php";

	class _changePassword extends pageBase{
		public function load(){
			if(!parent::chkUserLimit('11')){
				parent::alert2('您沒有使用此頁面的權限!如有疑問請洽主管理者!','index.php');
				exit;
			}

			if(parent::isPostBack()){
				$this->save();
			}else{
				parent::loadTemplateFile(dirname(__FILE__)."/templates/changePassword.html");
				parent::loadData();
			}
			parent::load();
		}
		private function save(){
			$user = parent::getCurrentUser();

			$dal = new userDAL();
			$dal->UserID = $_SESSION[sessionKeys::$adminUserID];
			$dal->loadRecord();

			$password = $_POST["password"];
			$newpassword = $_POST["newpassword"];

			if($password == $dal->Password){
				$dal->Password = $newpassword;
				if($dal->updateRecord()){
					parent::alert("密碼修改成功!");
				}else{
					parent::alert("修改密碼時發生錯誤!");
				}
			}else{
				parent::alert($password);
				parent::alert($dal->Password);
				parent::alert("請輸入正確的舊密碼!");
			}
		}
	}

	$page = new _changePassword();
	$page->load();
	$page->render();
?>