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

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

import com.jeesite.modules.admin.wenjuan.dao.AdminQuestionOptionDao;
import com.jeesite.modules.admin.wenjuan.entity.AdminQuestionItem;
import com.jeesite.modules.admin.wenjuan.entity.AdminQuestionOption;
import com.jeesite.modules.admin.wenjuan.service.AdminQuestionItemService;
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.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 java.util.List;


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

    @Autowired
    private AdminQuestionItemService adminQuestionItemService;

    @Autowired
    private AdminQuestionOptionService adminQuestionOptionService;

    @Autowired
    private AdminQuestionOptionDao adminQuestionOptionDao;

    /**
     * 获取数据
     */
    @ModelAttribute
    public AdminQuestionItem get(Long questionitemId, boolean isNewRecord) {
        return adminQuestionItemService.get(questionitemId + "", isNewRecord);
    }

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

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

    /**
     * 查询子表数据
     */
    /*@RequiresPermissions("admin:adminQuestionItem:view")*/
    @RequestMapping(value = "adminQuestionOptionListData")
    @ResponseBody
    public Page<AdminQuestionOption> subListData(AdminQuestionOption adminQuestionOption, HttpServletRequest request, HttpServletResponse response) {
        adminQuestionOption.setPage(new Page<>(request, response));
        Page<AdminQuestionOption> page = adminQuestionItemService.findSubPage(adminQuestionOption);
        return page;
    }

    /**
     * 查看编辑表单
     */
    /*@RequiresPermissions("admin:adminQuestionItem:view")*/
    @RequestMapping(value = "form")
    public String form(AdminQuestionItem adminQuestionItem, Model model) {
        AdminQuestionOption adminQuestionOption = new AdminQuestionOption();
        adminQuestionOption.setQuestionitemId(adminQuestionItem);
        //查询问卷调查项表
        AdminQuestionItem adminQuestionItem1 = adminQuestionItemService.get(adminQuestionItem);
        //查询问卷调查选项表
        List<AdminQuestionOption> list = adminQuestionOptionService.findList(adminQuestionOption);
        int size = list.size();

        adminQuestionItem.setAdminQuestionOptionList(list);
        adminQuestionItem.setTitle(adminQuestionItem1.getTitle());
        model.addAttribute("adminQuestionItem", adminQuestionItem);
        return "modules/admin/wenjuan/adminQuestionItemForm";
    }

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

        AdminQuestionOption adminQuestionOption = new AdminQuestionOption();
        adminQuestionOption.setQuestionitemId(adminQuestionItem);
        List<AdminQuestionOption> list = adminQuestionOptionService.findList(adminQuestionOption);

        if (list.size() > 0) {
            List<AdminQuestionOption> adminQuestionOptionList = adminQuestionItem.getAdminQuestionOptionList();
            for (AdminQuestionOption questionOption : adminQuestionOptionList) {
                /*if (){

                }*/
            }
        }

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

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

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

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

    /**
     * 保存数据
     */
    /*@RequiresPermissions("admin:adminQuestionItem:edit")*/
    @PostMapping(value = "update")
    @ResponseBody
    public String update(@Validated AdminQuestionItem adminQuestionItem) {
        adminQuestionItemService.update(adminQuestionItem);
        return renderResult(Global.TRUE, text("修改问卷调查项表成功！"));
    }

}