package com.community.huiju.controller;

import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.model.TaUser;
import com.community.huiju.model.TpTicket;
import com.community.huiju.model.TpTransaction;
import com.community.huiju.model.TpTicketRecordComment;
import com.community.huiju.service.AgreementServicel;
import com.community.huiju.service.ITicketService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Example;
import io.swagger.annotations.ExampleProperty;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "工单相关的API",description = "工单相关的API")
public class TicketController extends BaseController {

    @Autowired
    private ITicketService iTicketService;

    @Autowired
    private AgreementServicel agreementServicel;

    @RequestMapping(value = "/tickets/{communityId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取 报修/投诉/联系单 各3条数据", notes = "根据 小区编号,第几页,一页多少行")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "Integer",name = "communityId",value = "小区编号"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageNum",value = "第几页"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageSize",value = "一页多少数据"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),
    })
    @Deprecated
    public ResponseBean getService(@PathVariable(value = "communityId") Integer communityId,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize,
                                   HttpSession session) {
        ResponseBean response = new ResponseBean();

        if (!check(communityId)) {
            response.addError("小区不存在");
            return response;
        }

        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);

        TpTicket tpTicket = new TpTicket();
        tpTicket.setCommunityId(Integer.valueOf(communityId));
        tpTicket.setTaUserId(userElement.getId());

        response = iTicketService.getList(tpTicket, pageNum, pageSize);
        return response;
    }

    @ApiOperation(value = "获取报修/投诉/联系单进度详情数据", notes = "获取报修/投诉/联系单进度详情数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "communityId", value = "小区编号"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "ticketId", value = "报修id")
    })
    @RequestMapping(value = "/ticket/schedule/{communityId}", method = RequestMethod.GET)
    public ResponseBean getTicketSchedule(@PathVariable(value = "communityId") Integer communityId, @RequestParam Integer ticketId) {
        ResponseBean responseBean = new ResponseBean();

        if (!check(communityId)) {
            responseBean.addError("小区不存在");
            return responseBean;
        }

        TpTicket ticket = iTicketService.getTicketSchedule(String.valueOf(communityId), ticketId);
        if (null != ticket) {
            responseBean.addSuccess(ticket);
        }else {
            responseBean.addError("没有该工单!");
        }

        return responseBean;
    }

    @RequestMapping(value = "/tickets/list/{communityId}", method = RequestMethod.GET)
    @ApiOperation(value = "获取 报修/投诉/联系单 数据列表", notes = "根据 小区编号,工单类型,第几页,一页多少行")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "Integer",name = "communityId", value = "小区编号"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageNum", value = "第几页"),
            @ApiImplicitParam(paramType = "query",dataType = "Integer",name = "pageSize", value = "一页多少数据"),
            @ApiImplicitParam(paramType = "query",dataType = "String",name = "type", value = "工单类型 (1：投诉   2：报修 3：联系物业)"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token"),

    })
    public ResponseBean getServiceList(@PathVariable(value = "communityId") Integer communityId,
                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                       @RequestParam(value = "type") String type,
                                       HttpSession session) {
        ResponseBean responseBean = new ResponseBean();

        if (!check(communityId)) {
            responseBean.addError("小区不存在");
            return responseBean;
        }

        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);

        TpTicket tpTicket = new TpTicket();
        tpTicket.setCommunityId(Integer.valueOf(communityId));
        tpTicket.setTaUserId(userElement.getUserVerifyId());
        tpTicket.setType(type);

        responseBean = iTicketService.getByTypeList(tpTicket, pageNum, pageSize);

        return responseBean;

    }

    @ApiOperation(value = "回复 报修/投诉/联系单 追问接口", notes = "回复 报修/投诉/联系单 追问接口")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "communityId", value = "小区Id"),
            @ApiImplicitParam(name = "tpTicketRecordComment", value = "回复记录参数(ticketId:工单id,content:回复时间,ticketRecordCommentId:需要回复的id)", paramType = "body"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
    @RequestMapping(value = "/ticket/reply/{communityId}", method = RequestMethod.POST)
    public ResponseBean updateTicketsReply(@PathVariable(value = "communityId") Integer communityId, @RequestBody TpTicketRecordComment tpTicketRecordComment,HttpSession session){
        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
        Integer userId = userElement.getId();
        ResponseBean responseBean = new ResponseBean();

        if (!check(communityId)) {
            responseBean.addError("小区不存在");
            return responseBean;
        }

        tpTicketRecordComment.setCommunityId(communityId);
        Integer size = iTicketService.updateTicketsReply(tpTicketRecordComment, userId);
        if (size > 0){
            responseBean.addSuccess(size);
        } else {
            responseBean.addError("更新失败");
        }
        return responseBean;
    }

    @ApiOperation(value = "添加 报修/投诉/联系单", notes = "添加 报修/投诉/联系单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "paramets", value =
                    "ticketTitle:工单标题,ticketContent:工单内容，type: 1：投诉   2：报修 3：联系物业 , repairType：0:公共区域  1: 房屋质量  2:户内设施, imageUrl:图片地址 "),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")
    })
    @RequestMapping(value = "/addtpTicket", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean addTransaction(@RequestBody String paramets, HttpSession session) throws IOException {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
        ResponseBean response = iTicketService.addAiTicketService(userElement, paramets);
        return response;
    }

    @ApiOperation(value = "物业，门卫，领导电话", notes = "物业，门卫，领导电话")
    @RequestMapping(value = "/allTpAnnouncementTel", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")
    })
    public ResponseBean getTpAnnouncementTel(HttpSession session) {
        UserElement userElement = getUserElement(session);
        ResponseBean agreement = agreementServicel.getTpAnnouncementTel(userElement);
        return agreement;
    }

    @ApiOperation(value = "修改 报修/投诉/联系单", notes = "修改 报修/投诉/联系单")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "ticketId", value = "工单Id"),
            @ApiImplicitParam(paramType = "body", dataType = "String", name = "parameter", value =
                    "ticketTitle:工单标题,ticketContent:工单内容，type: 1：投诉   2：报修 3：联系物业 , imageUrl:图片地址 "),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")
    })
    @RequestMapping(value = "/updateTicket/{ticketId}", method = RequestMethod.POST)
    public ResponseBean updateTicketContent(@PathVariable("ticketId") Integer ticketId,
                                            @RequestBody String parameter,
                                            HttpSession session) {
        ResponseBean responseBean = new ResponseBean();
        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
        responseBean = iTicketService.updateTicketContent(ticketId, parameter,userElement.getId());
        return responseBean;
    }

    @ApiOperation(value = "报修进度终止", notes = "报修进度终止")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "integer", name = "communityId", value = "小区Id"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "ticketId", value = "帖子Id"),
            @ApiImplicitParam(paramType = "header",dataType = "String",name = "X-Auth-Token",value = "Token")})
    @RequestMapping(value = "/stopTicket/{communityId}", method = RequestMethod.GET)
    public ResponseBean stopTicket(@PathVariable("communityId") Integer communityId, @RequestParam("ticketId") Integer ticketId, HttpSession session) {
        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
        Integer userId = userElement.getId();

        ResponseBean responseBean = new ResponseBean();
        if (!check(communityId)) {
            responseBean.addError("小区不存在");
            return responseBean;
        }

        responseBean = iTicketService.stopTpTicketRecord(communityId, ticketId, userId);
        return responseBean;
    }

    @ApiOperation(value = "添加工单对话", notes = "添加工单对话")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "content:对话内容，imageUrl:[]图片,ticketId:工单id，" )
    })
    @ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "token")
    @RequestMapping(value = "/addRecordComment", method = RequestMethod.POST)
    public ResponseBean addRecordComment(@RequestBody String parameter, HttpSession session){
        UserElement userElement = (UserElement) session.getAttribute(Constant.APP_USER_SESSION);
        ResponseBean  responseBean = iTicketService.addRecordComment(parameter,userElement.getUserVerifyId(),userElement.getCommunityId(),userElement.getUserName());
        return responseBean;
    }
}


