package com.jeesite.modules.sso.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.sso.entity.SsoApp;
import com.jeesite.modules.sso.service.SsoAppService;

import java.util.List;
import java.util.UUID;

/**
 * 应用接入列表Controller
 * @author yinet
 * @version 2022-06-14
 */
@Controller
@RequestMapping(value = "${adminPath}/sso/ssoApp")
public class SsoAppController extends BaseController {

	@Autowired
	private SsoAppService ssoAppService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public SsoApp get(Long id, boolean isNewRecord) {
		return ssoAppService.get(id+"", isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("sso:ssoApp:view")
	@RequestMapping(value = {"list", ""})
	public String list(SsoApp ssoApp, Model model) {
		// model.addAttribute("ssoApp", ssoApp);

		if (ssoApp.getAccessType().equals("0")) {
			return "modules/sso/ssoAppList";
		} else if (ssoApp.getAccessType().equals("1")) {
			return "modules/sso/messageList";
		} else {
			return "modules/sso/serviceList";
		}


	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("sso:ssoApp:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<SsoApp> listData(SsoApp ssoApp, HttpServletRequest request, HttpServletResponse response) {
		ssoApp.setPage(new Page<>(request, response));
		Page<SsoApp> page = ssoAppService.findPage(ssoApp);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sso:ssoApp:view")
	@RequestMapping(value = "form")
	public String form(SsoApp ssoApp, Model model) {
		model.addAttribute("ssoApp", ssoApp);
		return "modules/sso/ssoAppForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("sso:ssoApp:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated SsoApp ssoApp) {
		ssoAppService.save(ssoApp);
		return renderResult(Global.TRUE, text("保存应用接入列表成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("sso:ssoApp:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(SsoApp ssoApp) {
		ssoAppService.delete(ssoApp);
		return renderResult(Global.TRUE, text("删除应用接入列表成功！"));
	}

	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("sso:ssoApp:view")
	@RequestMapping(value = "ssoAppSelect")
	public String ssoAppSelect(SsoApp ssoApp, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("ssoApp", ssoApp);
		return "modules/sso/ssoAppSelect";
	}

	//修改状态
	@RequestMapping("/changeStatus")
	public String changeStatus(String flag,String id){
		System.out.println(flag+"===="+id);

		int i=ssoAppService.changeStatus(flag,id);
		if(i!=0){
			return renderResult(Global.TRUE, text("修改成功！"));
		}else {
			return renderResult(Global.TRUE, text("修改失败！"));
		}

	}
	//修改状态
	@RequestMapping("/recommendStatus")
	public String recommendStatus(String flag,String id){
		System.out.println(flag+"===="+id);

		int i=ssoAppService.recommendStatus(flag,id);
		if(i!=0){
			return renderResult(Global.TRUE, text("修改成功！"));
		}else {
			return renderResult(Global.TRUE, text("修改失败！"));
		}

	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("sso:ssoApp:view")
	@RequestMapping(value = "serviceform")
	public String serviceform(SsoApp ssoApp, Model model) {
		if(ssoApp.getId()==null || ssoApp.getId()=="") {
			ssoApp.setAppKey(UUID.randomUUID().toString());
			ssoApp.setAppSecretkey(UUID.randomUUID().toString());
		}
		model.addAttribute("ssoApp", ssoApp);
		return "modules/sso/serviceForm";
	}


	/**
	 * 应用
	 */
	@RequestMapping(value = "ssoApp")
	public List<SsoApp> ssoApp(SsoApp ssoApp) {
		ssoAppService.findList(ssoApp);
		return ssoAppService.findList(ssoApp);
	}


}
