package com.huiju.estateagents.property.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.property.entity.TpMessageBoard;
import com.huiju.estateagents.property.service.IMessageBoardService;
import com.huiju.estateagents.property.vo.MessageBoardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class MessageBoardController extends BaseController {

    @Autowired
    IMessageBoardService iMessageBoardService;

    @GetMapping("/admin/message-board")
    public ResponseBean getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(required = false) Integer communityId,
                                @RequestParam(required = false) String name,
                                @RequestParam(required = false) String phone,
                                @RequestParam(required = false) String startDate,
                                @RequestParam(required = false) String endDate,
                                HttpServletRequest request) {
        IPage<TpMessageBoard> page = new Page<>(pageNum, pageSize);

        Map<String, Object> params = new HashMap<>();
        params.put("orgId", getOrgId(request));
        params.put("name", name);
        params.put("phone", phone);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("communityId", communityId);

        IPage<TpMessageBoard> result = iMessageBoardService.getPagedBy(page, params);
        return ResponseBean.success(result);
    }

    @GetMapping("/admin/message-board/{id}")
    public ResponseBean getDetail(@PathVariable Integer id,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                  HttpServletRequest request) {
        IPage<TpMessageBoard> page = new Page<>(pageNum, pageSize);
        IPage<TpMessageBoard> result = iMessageBoardService.getByMsgId(page, getOrgId(request), id);

        return ResponseBean.success(result);
    }

    @GetMapping("/wx/message-board")
    public ResponseBean getWxList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                  HttpServletRequest request) {
        IPage<TpMessageBoard> page = new Page<>(pageNum, pageSize);
        IPage<TpMessageBoard> result = iMessageBoardService.getWxList(page, getOrgId(request), getPersonId(request));

        return ResponseBean.success(result);
    }

    @PostMapping("/{client}/message-board")
    public ResponseBean saveMessageOrReply(@PathVariable String client,
                                           @RequestBody MessageBoardVO params,
                                           HttpServletRequest request) {
        String personId = null;
        Integer userId = null;

        if ("wx".equals(client)) {
            personId = getPersonId(request);
        } else {
            userId = getUserId(request);
        }

        try {
            iMessageBoardService.saveMessageOrReply(params, getOrgId(request), userId, personId);
            return ResponseBean.success("success");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("留言失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

}
