package com.itjob.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.*;

import com.itjob.dao.model.BindPhone;
import com.itjob.service.BindPhoneManager;

/**
 * 绑定电话设置的Action
 * @author AmigoXie
 */
public class BindPhoneAction extends Action {
	protected BindPhoneManager bindPhoneManager;

	public void setBindPhoneManager(BindPhoneManager bindPhoneManager) {
		this.bindPhoneManager = bindPhoneManager;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		try {
			String method = request.getParameter("method");
			if ("getBindPhoneList".equals(method)) {
				return getBindPhoneList(mapping, form, request, response);
			} else if ("insertBindPhone".equals(method)) {
				return insertBindPhone(mapping, form, request, response);
			} else if ("deleteBindPhone".equals(method)) {
				return deleteBindPhone(mapping, form, request, response);
			} else if ("getBindPhone".equals(method)) {
				return getBindPhone(mapping, form, request, response);
			} else if ("updateBindPhone".equals(method)) {
				return updateBindPhone(mapping, form, request, response);
			} else if ("setAnswerFlag".equals(method)) {
				return setAnswerFlag(mapping, form, request, response);
			} else if ("setCalloutFlag".equals(method)) {
				return setCalloutFlag(mapping, form, request, response);
			} else if ("setPriOrder".equals(method)) {
				return setPriOrder(mapping, form, request, response);
			} else if ("validBindPhone".equals(method)) {
				return validBindPhone(mapping, form, request, response);
			} else {
				request.setAttribute("errorMsg", "错误请求");
				return mapping.findForward("error");
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMsg", e.getMessage());
			return mapping.findForward("error");
		}
	}
	
	/**
	 * 获得绑定电话列表
	 */
	public ActionForward getBindPhoneList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userNumber = (String) request.getSession().getAttribute("userNumber");
		List bindPhoneList = bindPhoneManager.getBindPhoneList(userNumber);
		request.setAttribute("bindPhoneList", bindPhoneList);
        return mapping.findForward("bindPhoneList");
	}
	
	/**
	 * 添加绑定电话信息
	 */
	public ActionForward insertBindPhone(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		BindPhone bindPhone = new BindPhone();
		bindPhone.setBindPhone(request.getParameter("bindPhone"));
		bindPhone.setUserNumber((String) request.getSession().getAttribute("userNumber"));
		bindPhoneManager.insertBindPhone(bindPhone);
		request.setAttribute("returnPage", "/bindPhoneAction.do?method=getBindPhoneList");
        return mapping.findForward("success");
	}
	
	/**
	 * 删除一到多个绑定电话信息
	 */
	public ActionForward deleteBindPhone(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] idArray = request.getParameterValues("id");
		bindPhoneManager.deleteBindPhone(idArray);
		request.setAttribute("returnPage", "/bindPhoneAction.do?method=getBindPhoneList");
        return mapping.findForward("success");
	}
	
	/**
	 * 获得某个绑定电话信息
	 */
	public ActionForward getBindPhone(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		long id = Long.parseLong(request.getParameter("id"));
		BindPhone bindPhone = bindPhoneManager.getBindPhone(id);
		request.setAttribute("bindPhone", bindPhone);
        return mapping.findForward("updateBindPhone");
	}
	
	/**
	 * 修改某个绑定电话的信息
	 */
	public ActionForward updateBindPhone(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		BindPhone bindPhone = new BindPhone();
		bindPhone.setId(Long.parseLong(request.getParameter("id")));
		bindPhone.setUserNumber(request.getParameter("userNumber"));
		bindPhone.setBindPhone(request.getParameter("bindPhone"));
		bindPhone.setAnswerFlag(Integer.parseInt(request.getParameter("answerFlag")));
		bindPhone.setCalloutFlag(Integer.parseInt(request.getParameter("calloutFlag")));
		bindPhone.setPhoneState(Integer.parseInt(request.getParameter("phoneState")));
		bindPhone.setPriOrder(Integer.parseInt(request.getParameter("priOrder")));
		bindPhoneManager.updateBindPhone(bindPhone);
		request.setAttribute("returnPage", "/bindPhoneAction.do?method=getBindPhoneList");
        return mapping.findForward("success");
	}
	
	/**
	 * 切换绑定电话对象是否允许接听标志
	 */
	public ActionForward setAnswerFlag(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		long id = Long.parseLong(request.getParameter("id"));
		bindPhoneManager.setAnswerFlag(id);
        return getBindPhoneList(mapping, form, request, response);
	}
	
	/**
	 * 切换绑定电话对象是否允许外呼标志
	 */
	public ActionForward setCalloutFlag(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		long id = Long.parseLong(request.getParameter("id"));
		bindPhoneManager.setCalloutFlag(id);
        return getBindPhoneList(mapping, form, request, response);
	}
	
	/**
	 * 设置多个绑定电话的接听顺序
	 */
	public ActionForward setPriOrder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String[] idArray = request.getParameterValues("id");
		String[] priOrderArray = request.getParameterValues("priOrder");
		bindPhoneManager.setPriOrder(idArray, priOrderArray);
        return getBindPhoneList(mapping, form, request, response);
	}
	
	/**
	 * 验证绑定号码是否已存在（ajax验证）
	 */
	public ActionForward validBindPhone(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		 // 处理中文问题
		 request.setCharacterEncoding("utf-8");
         response.setContentType("text/html;charset=utf-8");
         String bindPhone = request.getParameter("bindPhone");
         System.out.println("bindPhone=" + bindPhone);
         String userNumber = (String) request.getSession().getAttribute("userNumber");
         int re = bindPhoneManager.validBindPhone(userNumber, bindPhone);
         System.out.println("re=" + re);
         String msg = "";
         if (re == 1) {
        	 msg = "该绑定号码已被设置，不允许添加";
         }
         PrintWriter out = response.getWriter();
         out.write(msg); 
         out.flush();
         out.close();
         return null;
	}
}
