package com.jwipc.spe.controller;

import cn.hutool.core.lang.Pair;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.security.LoginUtil;
import com.jwipc.spe.dto.AppraiseReportDTO;
import com.jwipc.spe.dto.SupAppraisePlanSupplierBaseDTO;
import com.jwipc.spe.dto.SupAppraiseRectifiedNoticeBaseDTO;
import com.jwipc.spe.logic.RectifiedNoticeLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Set;

import static com.jwipc.spe.constant.ModuleConst.MODULE_ROOT;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(MODULE_ROOT + "/rectifiedNotice/page")
@RequiredArgsConstructor
public class RectifiedNoticePageController {
    private final RectifiedNoticeLogic rectifiedNoticeLogic;

    /**
     * 绩效考核通知单明细新增和编辑 页面
     *
     * @param model          model
     * @param supplierPlanId 绩效考核结果ID
     * @return 页面
     */
    @GetMapping("add")
    public String add(Model model,
                      Long supplierPlanId)
    {
        if (supplierPlanId != null) {
            SupAppraisePlanSupplierBaseDTO result = rectifiedNoticeLogic.getAppraisePlan(supplierPlanId);
            model.addAttribute("data", result);
            //需要把两个备注的数据带出来
            Set<String> data = rectifiedNoticeLogic.getAppraiseItem(supplierPlanId);
            model.addAttribute("dataList", JSON.toJSONString(data));
        }
        model.addAttribute("supplierPlanId", supplierPlanId);
        return MODULE_ROOT + "/rectifiedNotice/notice_add";
    }

    /**
     * 绩效考核通知单明细新增和编辑 页面
     *
     * @param model model
     * @param id    整改通知单主键
     * @return 页面
     */
    @GetMapping("edit")
    public String edit(Model model,
                       Long id)
    {
        model.addAttribute("id", id);
        final Pair<SupAppraiseRectifiedNoticeBaseDTO, AppraiseReportDTO> detail = rectifiedNoticeLogic.getDetail(id);
        model.addAttribute("data", detail.getKey());
        model.addAttribute("plan", detail.getValue());
        model.addAttribute("edit", true);
        return MODULE_ROOT + "/rectifiedNotice/notice_edit";
    }

    /**
     * 供应商绩效考核整改 首页
     *
     * @return 页面
     */
    @GetMapping("index")
    public String index(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/rectifiedNotice/notice_index";
    }

    /**
     * 供应商绩效考核整改 首页
     *
     * @return 页面
     */
    @GetMapping("allList")
    public String allList(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/rectifiedNotice/all_notice";
    }

    /**
     * 绩效考核通知单明细 页面
     *
     * @param model    model
     * @param pageType 页面类型 0:查看页面 1:编辑页面 2:审批页面
     * @param id       通知单ID
     * @return 页面
     */
    @GetMapping("detail")
    public String detail(Model model, Integer pageType, Long id) {
        model.addAttribute("id", id);
        Pair<SupAppraiseRectifiedNoticeBaseDTO, AppraiseReportDTO> notice = rectifiedNoticeLogic.getDetail(id);
        model.addAttribute("data", notice.getKey());
        final String reportJson = notice.getKey().getReportJson();
        final JSONObject report = JSON.parseObject(reportJson);
        model.addAttribute("report", report);
        model.addAttribute("plan", notice.getValue());
        model.addAttribute("pageType", pageType);
        return MODULE_ROOT + "/rectifiedNotice/notice_detail";
    }

    /**
     * 供应商绩效考核整改确认列表页面
     *
     * @return 页面
     */
    @GetMapping("confirm")
    public String confirmIndex() {
        return MODULE_ROOT + "/rectifiedNotice/confirm_index";
    }

    /**
     * 绩效考核确认通知单明细 页面
     *
     * @param model   model
     * @param optType 页面操作类型 1:跟踪 2:审批
     * @param id      通知单ID
     * @return 页面
     */
    @AuditLogInter(value = "详情页数据", module = "绩效考核")
    @GetMapping("confirmDetail")
    public String confirmDetail(Model model, Integer optType, Long id) {
        model.addAttribute("id", id);
        final Pair<SupAppraiseRectifiedNoticeBaseDTO, AppraiseReportDTO> detail = rectifiedNoticeLogic.getDetail(id);
        model.addAttribute("data", detail.getKey());
        model.addAttribute("plan", detail.getValue());
        final String reportJson = detail.getKey().getReportJson();
        final JSONObject report = JSON.parseObject(reportJson);
        model.addAttribute("report", report);
        model.addAttribute("examineUserName", LoginUtil.getName());
        model.addAttribute("optType", optType);
        return MODULE_ROOT + "/rectifiedNotice/confirm_detail";
    }

    /**
     * 供应商绩效考核整改审批
     *
     * @return 页面
     */
    @GetMapping("/examine")
    public String examineIndex(Model model) {
        return MODULE_ROOT + "/rectifiedNotice/examine_index";
    }

    /**
     * 供应商绩效考核整改审批详情
     *
     * @return 页面
     */
    @GetMapping("/examineDetail")
    public String examineDetail(Model model, Integer optType, Long id) {
        model.addAttribute("id", id);
        final Pair<SupAppraiseRectifiedNoticeBaseDTO, AppraiseReportDTO> detail = rectifiedNoticeLogic.getDetail(id);
        model.addAttribute("data", detail.getKey());
        final String reportJson = detail.getKey().getReportJson();
        final JSONObject report = JSON.parseObject(reportJson);
        model.addAttribute("report", report);
        model.addAttribute("plan", detail.getValue());
        model.addAttribute("optType", optType);
        model.addAttribute("examineUserName", LoginUtil.getName());
        return MODULE_ROOT + "/rectifiedNotice/examine_detail";
    }

    /**
     * 供应商绩效考核结案查询 首页(供应商类型为 1:一般供应商)
     *
     * @return 页面
     */
    @GetMapping("closing")
    public String closingIndex(Model model, @RequestParam(defaultValue = "0", required = false) Long planId) {
        //如果传入绩效考核的的计划id就说明是从绩效考核中查看的
        model.addAttribute("planId", planId);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/rectifiedNotice/closing_index";
    }

    @GetMapping("rectified/supplierList")
    public String supplierList(Model model, Long id) {
        model.addAttribute("id", id);
        return MODULE_ROOT + "/rectifiedNotice/rectified_supplier_list";
    }
}
