package com.sifang.modules.sunshine.SystemSetting.controller;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectInstallWay;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectSetting;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmProjectTypeReference;
import com.sifang.modules.sunshine.SystemSetting.form.AddProjectInstallWay;
import com.sifang.modules.sunshine.SystemSetting.form.ProjectTypeDetailVo;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectInfoSettingService;
import com.sifang.modules.sunshine.SystemSetting.service.ProjectInstallService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.util.FormSetUtils;
import com.sifang.modules.sys.util.GuardUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author hongzq
 * 系统设置-项目安装方式
 */
@Controller
@RequestMapping("${adminPath}/sysSettings/projectInstall")
public class ProjecInstallController extends BaseExceptionController {

    @Autowired
    ProjectInstallService projectInstallService;

    @RequestMapping(value = "/index")
    public String toProjectSetting() {
        return "modules/yangguang/systemSetting/projectInstall/projectInstall";
    }

    @RequestMapping(value = "/toAdd")
    public String toAdd(String id, Model model) {
        if (StrKit.notBlank(id)) {
            CrmProjectInstallWay setting = projectInstallService.getById(id);
            model.addAttribute("setting", setting);
            List<ProjectTypeDetailVo> reflist=projectInstallService.getProjectReference2(setting.getId());
            model.addAttribute("reflist", reflist);
        }
        //获取自定义字段
            List<Record> setList = FormSetUtils.findEntryAzImgs();
        model.addAttribute("setList", setList);
        //获取所有方案类型
        List<Record> typeList = projectInstallService.findProjectInstalls(id);
        model.addAttribute("typeList", typeList);
        return "modules/yangguang/systemSetting/projectInstall/addProjectInstall";
    }

    @ResponseBody
    @RequestMapping(value = "/findTypeReferenceList")
    public Result findTypeReferenceList(String typeId) {
        List<CrmProjectTypeReference> reflist=projectInstallService.getProjectReference(typeId);
        return Result.success(reflist);
    }

    @ResponseBody
    @RequestMapping(value = "/add")
    public Result<Void> add(@RequestBody AddProjectInstallWay projectSetting) {
        checkParams(projectSetting);
        projectInstallService.add(projectSetting);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/edit")
    public Result<Void> edit(@RequestBody AddProjectInstallWay projectSetting) {
        GuardUtils.notBlank(projectSetting.getId(), "id不能为空");
        checkParams(projectSetting);
        projectInstallService.edit(projectSetting);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public Result<Void> delete(@RequestParam(value = "ids") String ids) {
        GuardUtils.notBlank(ids, "ids不能为空");
        projectInstallService.delete(ids);
        return Result.ok();
    }

    @ResponseBody
    @RequestMapping(value = "/ajaxForList")
    public Object ajaxForProjectSettings(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        projectInstallService.listProjectInstallWays(page);
        return page.renderJson();
    }

    private void checkParams(AddProjectInstallWay projectSetting) {
        GuardUtils.notBlank(projectSetting.getName(), "安装方式名称不能为空");
    }
}
