package com.ibiz.web.external.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import com.ibiz.web.external.service.CPXHService;
import com.ibiz.web.external.domain.CPXH;
import com.ibiz.web.external.filter.*;

@RestController
@RequestMapping(value = "")
public class CPXHController {
	@Autowired
    CPXHService cpxhservice;


	@RequestMapping(method = RequestMethod.POST, value = "/cpxhs")
    public ResponseEntity<CPXH> create(@RequestBody CPXH cpxh) {
        CPXH cpxh2 = cpxhservice.create(cpxh);
        return ResponseEntity.status(HttpStatus.OK).body(cpxh2);
    }

	@RequestMapping(method = RequestMethod.PUT, value = "/cpxhs/{cpxh_id}")
    public ResponseEntity<CPXH> update(@PathVariable("cpxh_id") String cpxh_id, @RequestBody CPXH cpxh) {
        CPXH cpxh2 = cpxhservice.update(cpxh_id, cpxh);
        return ResponseEntity.status(HttpStatus.OK).body(cpxh2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/cpxhs/getdraft")
    public ResponseEntity<CPXH> getDraft() {
        //CPXH cpxh = cpxhservice.getDraft( cpxh_id);
        return ResponseEntity.status(HttpStatus.OK).body(new CPXH());
    }

	@RequestMapping(method = RequestMethod.DELETE, value = "/cpxhs/{cpxh_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("cpxh_id") String cpxh_id) {
        boolean b = cpxhservice.remove( cpxh_id);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/cpxhs/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody CPXH cpxh) {
        boolean b = cpxhservice.checkKey(cpxh);
        return ResponseEntity.status(HttpStatus.OK).body(b);
    }

	@RequestMapping(method = RequestMethod.POST, value = "/cpxhs/{cpxh_id}/save")
    public ResponseEntity<CPXH> save(@PathVariable("cpxh_id") String cpxh_id, @RequestBody CPXH cpxh) {
        CPXH cpxh2 = cpxhservice.save(cpxh_id, cpxh);
        return ResponseEntity.status(HttpStatus.OK).body(cpxh2);
    }

	@RequestMapping(method = RequestMethod.GET, value = "/cpxhs/{cpxh_id}")
    public ResponseEntity<CPXH> get(@PathVariable("cpxh_id") String cpxh_id) {
        CPXH cpxh = cpxhservice.get( cpxh_id);
        return ResponseEntity.status(HttpStatus.OK).body(cpxh);
    }

    @RequestMapping(method= RequestMethod.GET , value="/cpxhs/fetchdefault")
	public ResponseEntity<List<CPXH>> fetchDefault(CPXHSearchContext searchContext,Pageable pageable) {
        
        searchContext.setPageable(pageable);
        Page<CPXH> page = cpxhservice.fetchDefault(searchContext);
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(pageable.getPageNumber()))
        		.header("x-per-page", String.valueOf(pageable.getPageSize()))
        		.header("x-total", String.valueOf(page.getTotalElements()))
        		.body(page.getContent());
    }


}
