<?php
/**
 * 预约与调查模块定义
 *
 * @author WeEngine Team
 * @url http://bbs.we7.cc
 */
defined('IN_IA') or exit('Access Denied');

class ResearchModule extends WeModule {
	public function fieldsFormDisplay($rid = 0) {
		global $_W;
		if($rid) {
			$reply = pdo_fetch("SELECT * FROM " . tablename('research_reply') . " WHERE rid = :rid", array(':rid' => $rid));
			$sql = 'SELECT * FROM ' . tablename('research') . ' WHERE `weid`=:weid AND `reid`=:reid';
			$activity = pdo_fetch($sql, array(':weid' => $_W['weid'], ':reid' => $reply['reid']));
		}
		include $this->template('form');
	}

	public function fieldsFormValidate($rid = 0) {
		global $_GPC;
		$reid = intval($_GPC['activity']);
		if($reid) {
			$sql = 'SELECT * FROM ' . tablename('research') . " WHERE `reid`=:reid";
			$params = array();
			$params[':reid'] = $reid;
			$activity = pdo_fetch($sql, $params);
			if(!empty($activity)) {
				return '';
			}
		}
		return '没有选择合适的预约活动';
	}

	public function fieldsFormSubmit($rid) {
		global $_GPC;
		$reid = intval($_GPC['activity']);
		$record = array();
		$record['reid'] = $reid;
		$record['rid'] = $rid;
		$reply = pdo_fetch("SELECT * FROM " . tablename('research_reply') . " WHERE rid = :rid", array(':rid' => $rid));
		if($reply) {
			pdo_update('research_reply', $record, array('id' => $reply['id']));
		} else {
			pdo_insert('research_reply', $record);
		}
	}

	public function ruleDeleted($rid) {
		pdo_delete('research_reply', array('rid' => $rid));
	}

	public function settingsDisplay($settings) {
		global $_GPC, $_W;
		if(checksubmit()) {
			$cfg = array(
				'noticeemail' => $_GPC['noticeemail'],
			);
			if($this->saveSettings($cfg)) {
				message('保存成功', 'refresh');
			}
		}
		include $this->template('setting');
	}
}
