package com.yihu.wlyy.web.doctor.feedback;

import com.yihu.wlyy.service.app.feedback.AppealService;
import com.yihu.wlyy.service.app.feedback.FeedbackService;
import com.yihu.wlyy.util.CommonUtil;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.regex.Pattern;

/**
 * Created by Reece on 2017/5/6.
 */
@Controller
@RequestMapping(value = "/doctor/feedback")
@Api(description = "医生端意见反馈与账号申诉")
public class DoctorFeedbackController extends BaseController {
    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private AppealService appealService;

    /**
     * 医生端保存意见反馈
     *
     * @param description 问题描述
     * @param type        选择类型
     * @param images      图片，多图逗号分隔
     * @param contact     联系方式
     * @return
     */
    @RequestMapping(value = "/saveFeedback", method = RequestMethod.POST)
    @ApiOperation(value = "医生端保存反馈")
    @ResponseBody
    public String saveFeedback(
            @RequestParam String description,
            @RequestParam int type,
            @RequestParam(required = false) String images,
            @RequestParam(required = false) String contact) {
        try {
            if (StringUtils.isNotEmpty(contact)) {
                //        邮箱正则|QQ号|手机号正则
                String regexEmail = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
                String regexQQ = "^[1-9][0-9]{4,}$";
                String regexPhone = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
                Boolean emailFlag = Pattern.matches(regexEmail, contact);
                Boolean qqFlag = Pattern.matches(regexQQ, contact);
                Boolean phoneFlag = Pattern.matches(regexPhone, contact);
                if (emailFlag || qqFlag || phoneFlag) {
                    return write(-1, "QQ/邮箱格式错误！");
                }
            }
//        图片上传
            if (StringUtils.isNotEmpty(images)) {
                images = CommonUtil.copyTempImage(images);
            }
//        保存到数据库
            feedbackService.saveFeedback(getUID(),description,type,images,contact,2);
            return write(200, "保存成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "保存失败!");
        }
    }

    /**
     * 医生端保存账号申诉
     *
     * @param description 问题描述
     * @param type        选择类型
     * @param images      图片，多图逗号分隔
     * @param phone       手机号码
     * @return
     */
    @RequestMapping(value = "/saveAppeal", method = RequestMethod.POST)
    @ApiOperation(value = "医生端保存申诉")
    @ResponseBody
    public String saveAppeal(
            @RequestParam String description,
            @RequestParam int type,
            @RequestParam(required = false) String images,
            @RequestParam String phone) {
        try {
//        手机号正则
            String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$";
            if (Pattern.matches(regex, phone)) {
//        图片上传
                if (StringUtils.isNotEmpty(images)) {
                    images = CommonUtil.copyTempImage(images);

                }
//             保存到数据库
                appealService.saveAppeal(getUID(),description,type,images,phone,2);
                return write(200, "保存成功!");
            } else {
                return write(-1, "手机号码有误!");
            }
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "保存失败!");
        }
    }


}
