package com.softwarebr.nunu.web.admin;

import com.brframework.commoncms.core.Icons;
import com.softwarebr.nunu.entity.go.*;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.service.go.*;
import com.softwarebr.nunu.json.admin.go.*;
import com.brframework.commoncms.annatotion.AdminMenu;
import com.brframework.commoncms.annatotion.layout.EditAlertLayout;
import com.brframework.commoncms.annatotion.layout.EditLayout;
import com.brframework.commoncms.annatotion.layout.PageLayout;
import com.brframework.commoncms.annatotion.option.CmsOption;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.commonweb.json.Page;
import com.brframework.commonweb.json.PageParam;
import com.brframework.commonwebadmin.aop.annotation.AOLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import static com.brframework.commondb.core.ControllerPageHandle.convertTo;
import static com.brframework.commondb.core.ControllerPageHandle.jpaPageConvertToPage;

/**
 * 标签管理
 * @date 2020-02-19 14:03:47
 * @author lilin
 */

@RestController
@Api(tags = "标签管理")
@AdminMenu(menuName = "标签管理", icon= Icons.TABLE)
@RequestMapping("/admin/access")
public class AdminLabelController {

	@Autowired
	private LabelService labelService;

	@ApiOperation(value = "一级标签管理", notes = "一级标签管理", produces = "text/plain")
	@GetMapping("/v1/label/parent/page")
	@AdminMenu(menuName = "一级标签管理", order = 1)
	@PreAuthorize("hasRole('go-label-parent-query')")
	@PageLayout(
		options = {
			 @CmsOption(name = "新增一级标签", uriMappingMethod = "parentLabelCreateV1")
		},
		columnOptions = {
			 @CmsOption(name = "编辑", uriMappingMethod = "parentLabelEditV1"),
			 @CmsOption(name = "删除", uriMappingMethod = "parentLabelDelV1", alert = "确定删除该记录吗?")
		}
	)
	public JSONResult<Page<LabelParentResult>> parentLabelListV1(PageParam pageParam, LabelParentQueryParam param) {
		param.setStatus(SystemConst.MARKER_STATUS_NO);
		param.setSort(0);
		Page<LabelParentResult> page = jpaPageConvertToPage(labelService.page(pageParam, param), pageParam, LabelParentResult.class);
		return JSONResult.ok(page);
	}

	@ApiOperation(value = "修改一级标签信息", notes = "修改一级标签信息", produces = "text/plain")
	@PostMapping("/v1/label/parent/{id}")
	@PreAuthorize("hasRole('go-label-parent-edit-v1')")
	@AOLog
	@EditAlertLayout(
		paddingMappingMethod = "getLabel"
	)
	public JSONResult parentLabelEditV1(@PathVariable Long id, @RequestBody LabelParentParam param) {
		labelService.updateLabel(id, 0L,param.getLabelName(),0);
		return JSONResult.ok();
	}

	@ApiOperation(value = "添加一级标签信息", notes = "添加一级标签信息", produces = "text/plain")
	@PutMapping("/v1/label/parent/add")
	@PreAuthorize("hasRole('go-label-parent-create-v1')")
	@AOLog
	@EditLayout
	public JSONResult parentLabelCreateV1(@Valid @RequestBody LabelParentParam param) {
		labelService.saveLabel(0L,param.getLabelName(),0);
		return JSONResult.ok();
	}

	@ApiOperation(value = "删除标签信息", notes = "删除标签信息", produces = "text/plain")
	@DeleteMapping("/v1/label/{id}")
	@PreAuthorize("hasRole('go-label-del-v1')")
	@AOLog
	public JSONResult parentLabelDelV1(@PathVariable Long id) {
		labelService.removeLabel(id);
		return JSONResult.ok();
	}

	@ApiOperation(value = "获取一级标签信息", notes = "获取一级标签信息", produces = "text/plain")
	@PreAuthorize("hasRole('go-label-query')")
	@GetMapping("/v1/label/{id}")
	public JSONResult<Label> getLabel(@PathVariable Long id) {
		Optional<Label> optional = labelService.findById(id);
		return JSONResult.ok(optional.get());
	}

	@ApiOperation(value = "二级标签管理", notes = "二级标签管理", produces = "text/plain")
	@GetMapping("/v1/label/page")
	@AdminMenu(menuName = "二级标签管理", order = 2)
	@PreAuthorize("hasRole('go-label-query')")
	@PageLayout(
			options = {
					@CmsOption(name = "新增二级标签", uriMappingMethod = "labelCreateV1")
			},
			columnOptions = {
					@CmsOption(name = "编辑", uriMappingMethod = "labelEditV1"),
					@CmsOption(name = "删除", uriMappingMethod = "parentLabelDelV1", alert = "确定删除该记录吗?")
			}
	)
	public JSONResult<Page<LabelResult>> labelListV1(PageParam pageParam, LabelQueryParam param) {
		param.setStatus(SystemConst.MARKER_STATUS_NO);
		param.setSort(0);
		Page<LabelResult> page = jpaPageConvertToPage(labelService.page(pageParam, param), pageParam, LabelResult.class);
		return JSONResult.ok(page);
	}

	@ApiOperation(value = "修改二级标签信息", notes = "修改二级标签信息", produces = "text/plain")
	@PostMapping("/v1/label/{id}")
	@PreAuthorize("hasRole('go-label-edit-v1')")
	@AOLog
	@EditAlertLayout(
			paddingMappingMethod = "getLabel"
	)
	public JSONResult labelEditV1(@PathVariable Long id, @Valid @RequestBody LabelParam param) {
		labelService.updateLabel(id, param.getParentId(), param.getLabelName(), param.getSort());
		return JSONResult.ok();
	}

	@ApiOperation(value = "添加二级标签信息", notes = "添加二级标签信息", produces = "text/plain")
	@PutMapping("/v1/label/add")
	@PreAuthorize("hasRole('go-label-create-v1')")
	@AOLog
	@EditLayout
	public JSONResult labelCreateV1(@Valid @RequestBody LabelParam param) {
		labelService.saveLabel(param.getParentId(), param.getLabelName(), param.getSort());
		return JSONResult.ok();
	}

	@ApiOperation(value = "一级标签选项", notes = "一级标签选项", produces = "text/plain")
	@PreAuthorize("hasRole('go-label-parent-query')")
	@GetMapping("/v1/label/parent/all")
	public JSONResult<List<Label>> getLabelParentAll() {
		List<Label> label = labelService.getLabel(0L);
		return JSONResult.ok(label);
	}

	@ApiOperation(value = "二级标签选项", notes = "二级标签选项", produces = "text/plain")
	@PreAuthorize("hasRole('go-label-query')")
	@GetMapping("/v1/label/child/all/{parentId}")
	public JSONResult<List<Label>> getLabelChildAll(@PathVariable Long parentId) {
		List<Label> label = labelService.getLabel(parentId);
		return JSONResult.ok(label);
	}

}