package com.jeesite.modules.admin.wenjuan.web;

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

import com.jeesite.modules.admin.wenjuan.dao.AdminQuestionItemDao;
import com.jeesite.modules.admin.wenjuan.entity.AdminQuestionItem;
import com.jeesite.modules.admin.wenjuan.entity.AdminQuestionOption;
import com.jeesite.modules.admin.wenjuan.service.AdminQuestionOptionService;
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.*;

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;

/**
 * 问卷调查选项表Controller
 *
 * @author wlc
 * @version 2022-06-18
 */
@Controller
@RequestMapping(value = "${adminPath}/admin/adminQuestionOption")
public class AdminQuestionOptionController extends BaseController {

    @Autowired
    private AdminQuestionOptionService adminQuestionOptionService;

    @Autowired
    private AdminQuestionItemDao adminQuestionItemDao;


    /**
     * 获取数据
     */
    @ModelAttribute
    public AdminQuestionOption get(String questionoptionId, boolean isNewRecord) {
        return adminQuestionOptionService.get(questionoptionId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("admin:adminQuestionOption:view")
    @RequestMapping(value = {"list", ""})
    public String list(AdminQuestionOption adminQuestionOption, Model model) {
        model.addAttribute("adminQuestionOption", adminQuestionOption);
        return "modules/admin/wenjuan/adminQuestionOptionList";
    }

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

    /**
     * 查看编辑表单
     */
    /*@RequiresPermissions("admin:adminQuestionOption:view")*/
    @RequestMapping(value = "form")
    public String form(@RequestParam(value = "id") String questionitemId, Model model) {

        System.out.println(questionitemId);

        AdminQuestionOption adminQuestionOption = new AdminQuestionOption();

        AdminQuestionItem adminQuestionItem = new AdminQuestionItem();
        adminQuestionItem.setQuestionitemId(questionitemId);
        AdminQuestionItem adminQuestionItem1 = adminQuestionItemDao.get(adminQuestionItem);

        adminQuestionOption.setItemTitle(adminQuestionItem1.getTitle());
        adminQuestionOption.setQuestionitemId(adminQuestionItem1);
        model.addAttribute("adminQuestionOption", adminQuestionOption);

        return "modules/admin/wenjuan/adminQuestionOptionForm";
    }

    /**
     * 保存数据
     */
    /*@RequiresPermissions("admin:adminQuestionOption:edit")*/
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated AdminQuestionOption adminQuestionOption) {

        AdminQuestionOption adminQuestionOption1 = new AdminQuestionOption();
        AdminQuestionItem adminQuestionItem = new AdminQuestionItem();

        adminQuestionItem.setQuestionitemId(adminQuestionOption.getQuestionitemId().getQuestionitemId());
        adminQuestionOption1.setQuestionitemId(adminQuestionItem);

        adminQuestionOptionService.save(adminQuestionOption);
        return renderResult(Global.TRUE, text("保存问卷调查选项表成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("admin:adminQuestionOption:edit")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(AdminQuestionOption adminQuestionOption) {
        adminQuestionOptionService.delete(adminQuestionOption);
        return renderResult(Global.TRUE, text("删除问卷调查选项表成功！"));
    }

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

}