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.form.AddProjectInstallWay;
import com.sifang.modules.sunshine.SystemSetting.form.ProjectTypeDetailVo;
import com.sifang.modules.sunshine.SystemSetting.service.FormSetService;
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.web.BaseController;
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.ResponseBody;

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

/**
 * 表单设置
 */
@Controller
@RequestMapping("${adminPath}/form/set")
public class FormSetController extends BaseController {

    @Autowired
    private FormSetService service;

    @Autowired
    ProjectInstallService projectInstallService;

    /**
     * 表单设置页面
     */
    @RequestMapping(value = "/index")
    public String index(String type,Model model) {
        model.addAttribute("type", "1"); // 类型：1.表单类型 2.附件类型
        model.addAttribute("partnerStep", FormSetUtils.findAllPartnerStep());
        return "modules/yangguang/systemSetting/formSet/formSetList";
    }

    /**
     * 获取设置列表信息
     */
    @ResponseBody
    @RequestMapping(value = "getList")
    public Object getList(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        String step = params.get("step");
        String partner = params.get("partner");
        String type = params.get("type");
        // selectUsed 为1时表示只查询已启用的参数  非1表示查询所有参数
        boolean selectUsed = ("1".equals(params.get("selectUsed")));
        return FormSetUtils.getFormPartSetByStep(null, type, step, partner, selectUsed);
    }

    /**
     * 修改配置信息
     */
    @ResponseBody
    @RequestMapping(value = "changePartnerSet")
    public Object changePartnerSet(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        String id = params.get("id");
        String attr = params.get("attr");
        String partner = params.get("partner");
        String type = params.get("type");
        if (Objects.equals("1", type)) {
            return service.changeUseFlag(id, partner, attr);
        } else if (Objects.equals("2", type)) {
            return service.changeMustFillFlag(id, partner, attr);
        } else {
            return Result.fail("参数错误，未识别的操作类型");
        }
    }

    //设计示例图  stageId=02 表示勘测设计的实例图
    @RequestMapping(value = "/toSetExampleImg")
    public String toAdd(String stageId, Model model) {
        if (StrKit.notBlank(stageId)) {
            List<ProjectTypeDetailVo> reflist=projectInstallService.getProjectReference2(stageId);
            model.addAttribute("reflist", reflist);
        }
        //获取自定义字段
        List<Record> setList = FormSetUtils.findEntryImgs(stageId);
        model.addAttribute("setList", setList);
        return "modules/yangguang/systemSetting/formSet/toSetExampleImg";
    }

    //保存设置的示例图
    @ResponseBody
    @RequestMapping(value = "/saveSetExampleImg")
    public Result<Void> saveSetExampleImg(@RequestBody AddProjectInstallWay projectSetting) {
        projectInstallService.saveSetExampleImg(projectSetting);
        return Result.ok();
    }

}
