package com.jwipc.sys.controller.pc;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.contract.base.DigitalSealConfigClientDTO;
import com.jwipc.contract.client.ContractSignClient;
import com.jwipc.order.client.PurchaseOrderClient;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.entity.DigitalSealBusinessConfig;
import com.jwipc.sys.entity.User;
import com.jwipc.sys.service.IDigitalSealBusinessConfigService;
import com.jwipc.sys.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
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 java.net.URLEncoder;

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

/**
 * @author shenyang.ou
 * @date 2024/5/9 11:14
 * @desc e签宝前端
 */
@Controller
@RequestMapping(MODULE_ROOT + "/esign")
@RequiredArgsConstructor
public class ESignPageController {
    private final IDigitalSealBusinessConfigService digitalSealBusinessConfigService;
    private final ContractSignClient contractSignClient;
    private final PurchaseOrderClient purchaseOrderClient;
    private final IUserService userService;
    @GetMapping("/dialog")
    public String dialog(Model model,String businessTypeCode,Long businessId,String firstParty,Long firstPartyUserId,
                         String firstPartyName,String secondParty,String secondPartyName,String createFileUrl,String title,Integer sumSignFlag,String summaryFileId,String attachmentType) {
        model.addAttribute("businessTypeCode",businessTypeCode);
        model.addAttribute("businessId",businessId);
        model.addAttribute("firstParty",firstParty);
        model.addAttribute("firstPartyUserId",firstPartyUserId);
        model.addAttribute("firstPartyName",firstPartyName);
        model.addAttribute("secondParty",secondParty);
        model.addAttribute("secondPartyName",secondPartyName);
        model.addAttribute("createFileUrl",createFileUrl);
        model.addAttribute("title",title);
        model.addAttribute("sumSignFlag",sumSignFlag);
        model.addAttribute("summaryFileId",summaryFileId);
        model.addAttribute("attachmentType",attachmentType);
        //如果是汇总签，签署文件已经生成，需要根据文件获取页数，邓京术 2025-07-16
        if (StringUtils.isNotEmpty(summaryFileId)) {
            Integer numberOfPages = purchaseOrderClient.getEsignTotalPage(Long.parseLong(summaryFileId));
            model.addAttribute("numberOfPages",numberOfPages);
        }
        DigitalSealBusinessConfig data = digitalSealBusinessConfigService.getOne(Wrappers.lambdaQuery(DigitalSealBusinessConfig.class).eq(DigitalSealBusinessConfig::getBusinessTypeCode,businessTypeCode).last(" limit 1"));
        model.addAttribute("data",data);

        //获取公司对应的 E签宝信息
        DigitalSealConfigClientDTO sealConfig=  contractSignClient.selectESignDetailClient(firstParty,data.getSignatureType());
        if (ObjectUtil.isNotNull(sealConfig)){
            User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getAccountName,sealConfig.getSignerAccount()).last(" limit 1"));
            if (ObjectUtil.isNotNull(user)){
                sealConfig.setPhone(user.getPhone());
                sealConfig.setName(user.getUsername());
            }
        }
        model.addAttribute("sealConfig",sealConfig);

        if (firstPartyUserId!=null){
            User user = userService.getById(firstPartyUserId);
            model.addAttribute("defaultAccount",user.getAccountName());
        }

//        SupplierContactsDTO supplierContactsDTO = supplierClient.getDefaultContact(secondParty,"10");
//        model.addAttribute("supplierContact",supplierContactsDTO);
        return MODULE_ROOT + "/esign/dialog";
    }

    @GetMapping("/fileLookDialog")
    public String fileLookDialog(Model model,Long businessId,String businessTypeCode) {
        model.addAttribute("businessId",businessId);
        model.addAttribute("businessTypeCode",businessTypeCode);
        return MODULE_ROOT + "/esign/file_look_dialog";
    }

    @GetMapping("/fileLookDialogs")
    public String fileLookDialog(Model model,String businessId,String businessTypeCode) {
        model.addAttribute("ids", URLEncoder.encode(businessId));
        model.addAttribute("businessTypeCode",businessTypeCode);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/esign/file_look_dialog";
    }
}
