package com.oqpo.api.web.controller.oper;

import com.oqpo.api.enums.DataMessageCode;
import com.oqpo.api.enums.SystemMessageCode;
import com.oqpo.api.service.oper.AfflService;
import com.oqpo.api.service.oper.SaleMgrMgnService;
import com.oqpo.api.web.dto.request.oper.affl.AfflGridRequest;
import com.oqpo.api.web.dto.request.oper.affl.AfflShopBaseInfoGridRequest;
import com.oqpo.api.web.dto.request.oper.affl.SaveAfflRequest;
import com.oqpo.api.web.dto.request.oper.salemgr.SaleMgrGridRequest;
import com.oqpo.api.web.dto.request.oper.salemgr.SaleMgrPopGridRequest;
import com.oqpo.api.web.dto.request.oper.salemgr.SaveSaleMgrMgnlRequest;
import com.oqpo.api.web.dto.response.DataResponse;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.SaveResponse;
import com.oqpo.api.web.dto.response.oper.affl.AfflShopInfoResponse;
import com.oqpo.api.web.dto.response.oper.salemgr.SaleMgrMgnInfoResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/api/srep")
public class SaleMgrMgnController {
    @Autowired
    private SaleMgrMgnService saleMgrMgnService;

    /**
     * 설명 : 영업담당자 팝업 그리드 리스트
     *
     * @param SaleMgrPopGridRequest
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 영업담당자 팝업 그리드 리스트")
    @PostMapping("/pop-grid-list")
    public ResponseEntity<GridResponse> saleMgrPopGridList(@RequestBody @Valid SaleMgrPopGridRequest saleMgrPopGridRequest) throws Exception {
        return ResponseEntity.ok(saleMgrMgnService.selectPopSaleMgrMgnGridList(saleMgrPopGridRequest.getSSaleMgrNm(), saleMgrPopGridRequest.toDTO(saleMgrPopGridRequest)) );
    }

    /**
     * 설명 : 영업담당자 그리드 리스트
     *
     * @param saleMgrGridRequest
     * @return
     * @throws Exception
     */

    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = " 영업담당자 그리드 리스트")
    @PostMapping("/detail-grid-list")
    public ResponseEntity<GridResponse> detailGridList(@RequestBody @Valid SaleMgrGridRequest saleMgrGridRequest) throws Exception {
        return ResponseEntity.ok(saleMgrMgnService.selectSaleMgrMgnGridList( saleMgrGridRequest.getFromDt(), saleMgrGridRequest.getToDt(), saleMgrGridRequest.getSSaleMgrNm(), saleMgrGridRequest.getSStCd(),  saleMgrGridRequest.toDTO(saleMgrGridRequest)));
    }


    /**
     * 설명 : 영업담당자 정보
     *
     * @param saleMgrId
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "saleMgrId", value = "영업담당자아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "영업담당자 정보")
    @GetMapping("/info-salemgr")
    public ResponseEntity<SaleMgrMgnInfoResponse> infoSaleMgr(@RequestParam(value = "saleMgrId") String saleMgrId) throws Exception {
        return ResponseEntity.ok(saleMgrMgnService.selectSaleMgrMgn(saleMgrId));
    }


    /**
     * 설명 : 영업담당자 정보 저장
     *
     * @param userId
     * @param SaveSaleMgrMgnlRequest
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "영업담당자 저장")
    @PostMapping("/save-salemgr")
    public ResponseEntity<SaveResponse> saveCode(@ApiIgnore String userId, @RequestBody @Valid SaveSaleMgrMgnlRequest saveSaleMgrMgnlRequest) throws Exception {
        saleMgrMgnService.saveSaleMgrMgn(userId, saveSaleMgrMgnlRequest);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.SAVE_OK));
    }


    /**
     * 설명 : 영업담당자 key 중복체크
     *
     * @param afflAbbr
     * @return
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "saleMgrId", value = "영업담당자아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "영업담당자아이디 중복 체크")
    @GetMapping("/check-duplicate")
    public ResponseEntity<DataResponse> checkDuplicate(@RequestParam(value = "saleMgrId") String saleMgrId) throws Exception {
        String dupYn = saleMgrMgnService.selectSaleMgrIdDupYn(saleMgrId);
        DataMessageCode message = null;
        if ("Y".equals(dupYn)) {
            message = DataMessageCode.DUPLICATE_YES;
        } else {
            message = DataMessageCode.DUPLICATE_NO;
        }
        return ResponseEntity.ok(DataResponse.toDTO(message));
    }

    /**
     * 설명 : 가맹점 패스워드 초기화
     *
     * @param 영업담당자 패스워드 초기화
     * @param
     * @return SaveResponse
     * @throws Exception
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-AUTH-TOKEN", value = "CONN-KEY", required = true, dataType = "String", paramType = "header"),
            @ApiImplicitParam(name = "saleMgrId", value = "영업담당자아이디", required = true, dataType = "String", paramType = "query")
    })
    @ApiOperation(value = "영업담당자 패스워드 초기화")
    @GetMapping("/inti-salemgrpw")
    public ResponseEntity<SaveResponse> initSaleMgrPw(@ApiIgnore String userId, @RequestParam(value = "saleMgrId") String saleMgrId) throws Exception {
        saleMgrMgnService.selectSaleMgrInitPw(userId, saleMgrId);
        return ResponseEntity.ok(SaveResponse.toDTO(SystemMessageCode.INIT_PW_OK));
    }

}
