package com.hxhq.business.utils.pdf.template.gsp;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hxhq.business.domain.StudyFormApply;
import com.hxhq.business.domain.StudyFormApplyJcgj;
import com.hxhq.business.domain.StudyFormApplyQmxx;
import com.hxhq.business.utils.pdf.PdfBaseUtil;
import com.hxhq.business.utils.pdf.PdfExportUtil;
import com.hxhq.business.utils.pdf.template.TemplateBaseUtil;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.api.model.LoginUser;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 供试品-麻醉/精神药品配制/领取申请单
 *
 * @author tanfei
 */
public class MBMJYLQSQD {
    private static final Logger logger = LoggerFactory.getLogger(MBMJYLQSQD.class.getName());
    public String language = "zh";

    /**
     * 导出
     *
     * @param studyFormApply
     * @return
     */
    public String exportDetail(StudyFormApply studyFormApply, List<StudyFormApplyQmxx> studyFormApplyQmxxList, List<StudyFormApplyJcgj> studyFormApplyJcgjList, String lang, Integer jcgjlx, String localFilePath) {
        language = lang;
        Document document = null;
        FileOutputStream fos = null;
        String filePath = PdfBaseUtil.getFilePath(localFilePath, "Template");
        try {
            String sign = "hxhq";
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser != null) {
                sign = loginUser.getSysUser().getNickName();
            }
            document = PdfBaseUtil.init(document, fos, filePath, sign + PdfExportUtil.parseDateToStr(new Date()), studyFormApply.getBdmc(), false);
            JSONObject bdnr = JSONObject.parseObject(studyFormApply.getBdnr());
            JSONObject zdgxjl = JSONObject.parseObject(StringUtils.isNoneBlank(studyFormApply.getZdgxjl()) ? studyFormApply.getZdgxjl() : "{}");
            JSONArray fhyjjl = JSONArray.parseArray(StringUtils.isNoneBlank(studyFormApply.getFhyjjl()) ? studyFormApply.getFhyjjl() : "[]");

            //region 表单信息

            //基本信息
            PdfBaseUtil.addUnderlinedTitle(document, getName("基本信息"), 10, true);
            Map<String, String> formData2 = new LinkedHashMap<>();
            formData2.put(getName("试验编号或部门名称"), (StringUtils.isNoneBlank(bdnr.getString("syNo")) ? bdnr.getString("syNo") : "") + TemplateBaseUtil.getCheck("_syNo", zdgxjl) + TemplateBaseUtil.getFh("_syNo", fhyjjl));
            formData2.put(getName("SD/部长"), (StringUtils.isNoneBlank(bdnr.getString("SD")) ? bdnr.getString("SD") : "") + TemplateBaseUtil.getCheck("_SD", zdgxjl) + TemplateBaseUtil.getFh("_SD", fhyjjl));
            formData2.put(getName("试验物质名称"), (StringUtils.isNoneBlank(bdnr.getString("sywzmc")) ? bdnr.getString("sywzmc") : "") + TemplateBaseUtil.getCheck("_sywzmc", zdgxjl) + TemplateBaseUtil.getFh("_sywzmc", fhyjjl));
            formData2.put(getName("给药剂量"), (StringUtils.isNoneBlank(bdnr.getString("gyjl")) ? bdnr.getString("gyjl") : "") + TemplateBaseUtil.getCheck("_gyjl", zdgxjl) + TemplateBaseUtil.getFh("_gyjl", fhyjjl));
            formData2.put(getName("需要浓度"), (StringUtils.isNoneBlank(bdnr.getString("xynd")) ? bdnr.getString("xynd") : "") + TemplateBaseUtil.getCheck("_xynd", zdgxjl) + TemplateBaseUtil.getFh("_xynd", fhyjjl));
            formData2.put(getName("需要量"), (StringUtils.isNoneBlank(bdnr.getString("xyl")) ? bdnr.getString("xyl") : "") + TemplateBaseUtil.getCheck("_xyl", zdgxjl) + TemplateBaseUtil.getFh("_xyl", fhyjjl));
            formData2.put(getName("需要时间"), (StringUtils.isNoneBlank(bdnr.getString("xysj")) ? bdnr.getString("xysj") : "") + TemplateBaseUtil.getCheck("_xysj", zdgxjl) + TemplateBaseUtil.getFh("_xysj", fhyjjl));
            formData2.put(getName("预计归还时间"), (StringUtils.isNoneBlank(bdnr.getString("yjghsj")) ? bdnr.getString("yjghsj") : "") + TemplateBaseUtil.getCheck("_yjghsj", zdgxjl) + TemplateBaseUtil.getFh("_yjghsj", fhyjjl));
            if(StringUtils.isNoneBlank(bdnr.getString("dwzsOther"))){
                formData2.put(getName("动物种属"),
                        getName("其他")+ TemplateBaseUtil.getCheck("_dwzs", zdgxjl) + TemplateBaseUtil.getFh("_dwzs", fhyjjl) +
                        (StringUtils.isNoneBlank(bdnr.getString("dwzsOther")) ? bdnr.getString("dwzsOther") : "") + TemplateBaseUtil.getCheck("_dwzsOther", zdgxjl) + TemplateBaseUtil.getFh("_dwzsOther", fhyjjl));
            }else{
                formData2.put(getName("动物种属"), (StringUtils.isNoneBlank(bdnr.getString("dwzs")) ? bdnr.getString("dwzs") : "") + TemplateBaseUtil.getCheck("_dwzs", zdgxjl) + TemplateBaseUtil.getFh("_dwzs", fhyjjl));
            }
            formData2.put(getName("动物平均体重"), (StringUtils.isNoneBlank(bdnr.getString("dwpjtz")) ? bdnr.getString("dwpjtz") : "") + TemplateBaseUtil.getCheck("_dwpjtz", zdgxjl) + TemplateBaseUtil.getFh("_dwpjtz", fhyjjl));
            if(StringUtils.isNoneBlank(bdnr.getString("dwslOther"))) {
                formData2.put(getName("动物数量"),
                        (StringUtils.isNoneBlank(bdnr.getString("dwsl")) ? bdnr.getString("dwsl") : "") + TemplateBaseUtil.getCheck("_dwsl", zdgxjl) + TemplateBaseUtil.getFh("_dwsl", fhyjjl)+
                                getName("其他")+ TemplateBaseUtil.getCheck("_dwslUnit", zdgxjl) + TemplateBaseUtil.getFh("_dwslUnit", fhyjjl) +
                        (StringUtils.isNoneBlank(bdnr.getString("dwslOther")) ? bdnr.getString("dwslOther") : "") + TemplateBaseUtil.getCheck("_dwslOther", zdgxjl) + TemplateBaseUtil.getFh("_dwslOther", fhyjjl)
                );
            }else{
                formData2.put(getName("动物数量"),
                        (StringUtils.isNoneBlank(bdnr.getString("dwsl")) ? bdnr.getString("dwsl") : "") + TemplateBaseUtil.getCheck("_dwsl", zdgxjl) + TemplateBaseUtil.getFh("_dwsl", fhyjjl)+
                         (StringUtils.isNoneBlank(bdnr.getString("dwslUnit")) ? bdnr.getString("dwslUnit") : "") + TemplateBaseUtil.getCheck("_dwslUnit", zdgxjl) + TemplateBaseUtil.getFh("_dwslUnit", fhyjjl)
                );
            }
            PdfBaseUtil.addFormTableColumns(document, formData2, 2);

            PdfBaseUtil.addUnderlinedTitle(document, getName("备注") +  "：" +(StringUtils.isNoneBlank(bdnr.getString("remark")) ? bdnr.getString("remark") : "")+(TemplateBaseUtil.getCheck("_remark",zdgxjl)+TemplateBaseUtil.getFh("_remark",fhyjjl)), 10, false);
             //签名信息
            TemplateBaseUtil.addQmxxApply(document, studyFormApplyQmxxList, language);
            //稽查轨迹
            TemplateBaseUtil.addJcgjApply(document,studyFormApplyJcgjList, jcgjlx, language);
            logger.info("生成成功：{}", filePath);
        } catch (Exception e) {
            logger.error("生成失败", e);
            throw new RuntimeException("生成失败: " + e.getMessage());
        } finally {
            if (document != null) {
                document.close();
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    logger.error("关闭文件流失败", e);
                }
            }
        }
        return filePath;
    }


    /**
     * 获取名称
     *
     * @param name
     * @return
     */
    public String getName(String name) {
        return TemplateBaseUtil.getNameByLanguage(name, language);
    }

}
