package com.wt.dms.web.backlog;

import java.io.IOException;
import java.util.Properties;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.ContextImage;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.hirisun.components.web.wicket.lhgdialog.Dialog;
import com.hirisun.components.web.wicket.markup.html.HWebPage;
import com.wt.dms.model.Sms;
import com.wt.dms.service.IndexManagerService;
import com.wt.dms.util.PropertiesLoader;
import com.wt.dms.util.StatisticVariable;

/**
 * <pre>
 * 业务名: 指标
 * 功能说明: 流程
 * 编写日期: 2014-01-26
 * 作者:	HEA IDE
 * 
 * 历史记录
 * 1、修改日期：2014-01-26
 *    修改人：HEA IDE
 *    修改内容：新增
 * </pre>
 */
public class ProcessPicPage extends HWebPage
{

	@SpringBean
	private IndexManagerService indexManagerService;
	private TextArea<String> smsTextArea;
	private TextField<String> recipientTextArea;
	private FeedbackPanel feedback;
	private static final long serialVersionUID = 1L;
	private Dialog myAlert;

	@Override
	public void renderHead(IHeaderResponse response)
	{
		super.renderHead(response);
		response.renderCSSReference("css/css_sd.css");
	}

	/**
	 * 提示框
	 */
	private void renderAlterModel()
	{
		myAlert = new Dialog("alert") {

		};

		myAlert.dialogId = "editID";
		myAlert.setContent(new Label("content", ""));
		myAlert.setTitle("提示信息");
		myAlert.setInitialHeight(100);
		myAlert.setInitialWidth(200);
		add(myAlert);
	}

	/**
	 * 发送短信
	 * 
	 * @param picPath
	 *            图片路径
	 * @param idxStatus
	 *            指标
	 */
	public ProcessPicPage(String picPath, String idxStatus)
	{
		renderAlterModel();
		ContextImage processImage = new ContextImage("procces_picture_id", picPath);
		add(processImage);
		String sms = new String();
		Form<String> form = new Form<String>("formid");
		try {
			Properties props = PropertiesLoader.getProps();
			sms = props.getProperty("smsContent");// 默认短信内容
		} catch (IOException e) {
			e.printStackTrace();
		}
		form.add(smsTextArea = new TextArea<String>("smsID", new Model<String>(sms))); // 短信内容
		form.add(recipientTextArea = new TextField<String>("recipient", new Model<String>())); // 接收人
		recipientTextArea.setLabel(Model.of("收件人")).setRequired(true);
		feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		form.add(feedback);
		form.setMarkupId("formid");
		form.setOutputMarkupId(true);
		add(form);
		AjaxButton sendSmsBtnSubmit = new AjaxButton("sendSmsBtnSubmit") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form)
			{
				target.add(feedback);
				String reseivers = recipientTextArea.getModelObject();
				boolean vilidate = indexManagerService.isMobileNO(reseivers);
				if (!vilidate) {// 校验不通过
					myAlert.setContent(new Label("content", "收件人号码格式错误！"));
				} else {// 校验通过
					Sms sms = new Sms();
					sms.setPhoneNumbers(recipientTextArea.getModelObject().split(","));
					sms.setSmsContent(smsTextArea.getModelObject());
					boolean isSendSucess = indexManagerService.sendSms(sms);
					if (isSendSucess) {// 发送成功
						myAlert.setContent(new Label("content", "短信发送成功！"));
						myAlert.alert(target);
						ModalWindow.closeCurrent(target);
					} else {// 发送失败
						myAlert.setContent(new Label("content", "短信发送失败！"));
						myAlert.alert(target);
					}
				}
				myAlert.alert(target);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form)
			{
				target.add(feedback);
			}
		};
		form.add(sendSmsBtnSubmit);
		if (StatisticVariable.INDEX_STATUS_HAS_DONE.equalsIgnoreCase(idxStatus)) {
			form.setVisible(false);
		}

	}
}
