package com.jwipc.supplier.controller.pc;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.constant.ModuleConst;
import com.jwipc.supplier.dto.SupplierChangeVersionBaseDTO;
import com.jwipc.supplier.dto.SupplierDetailPageDTO;
import com.jwipc.supplier.logic.SupplierChangeLogic;
import com.jwipc.supplier.logic.SupplierLogic;
import com.jwipc.util.EncryptUtils;
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.Objects;

/**
 * <p> 供应商信息变更相关页面</p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@Controller
@RequestMapping(ModuleConst.MODULE_ROOT + "/supplierChange/page")
@RequiredArgsConstructor
public class SupplierChangePageController {
    private final SupplierLogic supplierLogic;
    private final SupplierChangeLogic supplierChangeLogic;

    /**
     * 供应商信息变更列表页面
     *
     * @param model
     * @return
     */
    @GetMapping("index")
    public String index(Model model,Integer examineState) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("examineState", examineState);
        return ModuleConst.MODULE_ROOT + "/supplierChange/index";
    }

    @GetMapping("indexAll")
    public String indexAll(Model model,Integer examineState) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("examineState", examineState);
        return ModuleConst.MODULE_ROOT + "/supplierChange/indexAll";
    }

    /**
     * 供应商信息变更新建或者编辑页面
     *
     * @param model
     * @return
     */
    @GetMapping("detail")
    public String detail(Model model, @RequestParam(required = false, defaultValue = "0") Long id,
                         @RequestParam(required = false, defaultValue = "0") Long supplierId)
    {
        if (LoginUtil.isSupplier()) {
            supplierId = LoginUtil.getSupplierId();
        }
        SupplierDetailPageDTO supplierDetailPageDTO = null;
        Integer isFreeze = null;
        Integer oaType = null;
        String remark = "";
        Integer eliminateFlag = null;
         String executionTime = "";
         String paymentTerms = "";
        if(Objects.nonNull(id) && id != 0){//变更申请id
             supplierDetailPageDTO = supplierLogic.renderPage(supplierId,id);
            SupplierChangeVersionBaseDTO detail = supplierChangeLogic.getDetail(Long.valueOf(id));
            remark = detail.getRemark();
            oaType = detail.getOaType();
            isFreeze = detail.getIsFreeze();
            eliminateFlag = detail.getEliminateFlag();
            executionTime = detail.getExecutionTime();
            paymentTerms = detail.getPaymentTerms();
        }else{
             supplierDetailPageDTO = supplierLogic.renderPage(supplierId,null);
        }


        model.addAttribute("supplier", supplierDetailPageDTO.getSupplier());
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("templateFollowTable", supplierDetailPageDTO.getTemplateFollowTable());
        model.addAttribute("templateFollowTableSet", supplierDetailPageDTO.getTemplateFollowTableSet());
        model.addAttribute("fieldList", supplierDetailPageDTO.getFieldList());
        model.addAttribute("textList", supplierDetailPageDTO.getTextList());
        model.addAttribute("changeVersionId", id);
        model.addAttribute("supplierId", supplierId);
        model.addAttribute("remark", remark);
        model.addAttribute("oaType", oaType);
        model.addAttribute("isFreeze", isFreeze);
        model.addAttribute("eliminateFlag", eliminateFlag);
        model.addAttribute("executionTime", executionTime);
        model.addAttribute("paymentTerms", paymentTerms);

        return ModuleConst.MODULE_ROOT + "/supplierChange/detail";
    }

    /**
     * 供应商信息变更 详情查看界面
     *
     * @param model
     * @param id    供应商变更信息主键
     * @param type  0:审批;1:查看
     * @return
     */
    @GetMapping("lookup")
    public String lookup(Model model,
                         @RequestParam(required = false, defaultValue = "0") String id,
                         @RequestParam(required = false, defaultValue = "1") Integer type)
    {
        if (!StrUtil.isNumeric(id)) {
            id = EncryptUtils.decrypt(id);
            //需要模拟登录
            if(!StpUtil.isLogin()){
                StpUtil.login(-99999,30);
            }
        }
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("type", type);
        model.addAttribute("changeVersion", supplierChangeLogic.getDetail(Long.valueOf(id)));
        return ModuleConst.MODULE_ROOT + "/supplierChange/lookup";
    }

}
