<template>
  <div>
    <div class="edit-container" v-if="open">
      <div class="edit-top">
        <div class="left-top">
          <img src="@/assets/images/back.png" @click="cancel()" />
          <div class="left-title"></div>
        </div>
        <div class="center-top">
        </div>
        <div class="right-top">
          <el-button @click="cancel()">{{ $t('form.close') }}</el-button>
          <el-button type="primary" v-if="form.bdzt === 5 && form.tbzt === 1" @click="openApprove = true">{{
            $t('page.business.study.studyFormFill.tb') }}</el-button>
        </div>
      </div>
      <!-- 1:流程；3：编辑；5：人员；7：修改；9：补充说明 -->
      <el-button type="primary" @click="exportExcel(-1)">{{ $t('page.business.study.studyFormFill.dcqbjcgj') }}
      </el-button>
      <el-button type="primary" @click="exportExcel(1)">{{ $t('page.business.study.studyFormFill.dclcjcgj') }}
      </el-button>
      <el-button type="primary" @click="exportExcel(3)">{{ $t('page.business.study.studyFormFill.dcbjjcgj') }}
      </el-button>
      <el-button type="primary" @click="exportExcel(7)">{{ $t('page.business.study.studyFormFill.dcxgjcgj') }}
      </el-button>
      <el-button type="primary" @click="exportExcel(999)">{{ $t('page.business.study.studyFormFill.dcbhsjgj') }}
      </el-button>
      <div class="edit-content">
        <div class="detail-content" style="width: 100%; height: 100%; padding: 0px 10px;">
          <vue-html2pdf :show-layout="true" pdf-content-width="100%" :pdf-format="form.templatePdfSize" :pdf-quality="2"
            :float-layout="false" pdf-orientation="landscape" :paginate-elements-by-height="0" :enable-download="false"
            :preview-modal="false" :filename="form.bdmc" :html-to-pdf-options="{
              margin: [10, 5, 10, 5], // 格式: [上, 右, 下, 左] (单位: mm)
              // 或者使用对象格式
              // margin: { top: 20, right: 15, bottom: 20, left: 15 },
              filename: 'document.pdf',
              image: {
                type: 'jpeg',
                quality: 2
              },
              enableLinks: false,
              html2canvas: {
                scale: 2,
                useCORS: true
              },
              jsPDF: {
                unit: 'mm',           // 单位: mm
                format: form.templatePdfSize,          // 纸张大小
                orientation: 'landscape' // 方向
              }
            }" @beforeDownload="handleBeforeDownload" ref="html2Pdf" @progress="onProgress">
            <section slot="pdf-content">
              <div class="pdf-content">
                <TemplateTable ref="templateTable" :sn="form.templateSn" :templateData="form" fillType="detail" />
                <div v-if="showExport" style="width: 100%; padding: 0px 30px ;">
                  <div class="content-title" style="margin-bottom: 10px;">
                    <div class="line"></div>
                    <div class="subtitle"> {{ $t('page.business.study.studyFormFill.qmxx') }}</div>
                  </div>
                  <table class="datatable">
                    <thead>
                      <tr>
                        <th style="width: 20%;">{{ $t('page.business.study.studyFormFill.qmr') }}</th>
                        <th style="width: 20%;">{{ $t('page.business.study.studyFormFill.qmyy') }}</th>
                        <th style="width: 20%;">{{ $t('page.business.study.studyFormFill.qmsj') }}</th>
                        <th style="width: 40%;">{{ $t('page.business.study.studyFormFill.bzyy') }}</th>
                      </tr>
                    </thead>
                    <tbody>
                      <tr v-for="(item, index) in qmxxExportList" :key="index">
                        <td>{{ item.qmrMc }}</td>
                        <td>{{ $i18n.locale === 'zh_CN' ? item.qmyy : item.qmyyEn }}</td>
                        <td>{{ item.createTime }}</td>
                        <td>{{ item.remark }}</td>
                      </tr>
                    </tbody>
                  </table>
                  <div class="content-title" style="margin-top: 10px;" v-show="jcgjlxExport != 999">
                    <div class="line"></div>
                    <div class="subtitle"> {{ $t('page.business.study.studyFormFill.jcgj') }}</div>
                  </div>
                  <JcgjExportList ref="jcgjExportList" :readonly="true" v-show="jcgjlxExport != 999" />
                </div>
              </div>
              <div v-if="showExport" id="watermark-overlay" ref="watermarkContainer" :style="{
                '--watermark-text': `'${watermarkText}'`,
                '--watermark-opacity': opacity,
                '--watermark-size': '14px',
                '--watermark-color': 'red'
              }"></div>
            </section>
          </vue-html2pdf>
          <div style="margin-left: 20px;">
            <div class="content-title">
              <div class="line"></div>
              <div class="subtitle"> {{ $t('page.business.study.studyFormFill.qmxx') }}</div>
            </div>
            <div class="pal">
              <el-table :data="qmxxList" v-loading="loadingQmxx" style="width: 100%;">
                <el-table-column :label="$t('page.business.study.studyFormFill.qmr')" align="center" prop="qmrMc" />
                <el-table-column :label="$t('page.business.study.studyFormFill.qmyy')" align="center"
                  :prop="$i18n.locale === 'zh_CN' ? 'qmyy' : 'qmyyEn'" />
                <el-table-column :label="$t('page.business.study.studyFormFill.qmsj')" align="center"
                  prop="createTime" />
                <el-table-column :label="$t('page.business.study.studyFormFill.bzyy')" align="center" prop="remark" />
              </el-table>
            </div>
            <pagination v small layout="prev, pager, next" :total="totalQmxx" :page.sync="queryParamsQmxx.pageNum"
              :limit.sync="queryParamsQmxx.pageSize" @pagination="getQmxxList" />
            <div class="content-title" style="margin-top: 10px;">
              <div class="line"></div>
              <div class="subtitle"> {{ $t('page.business.study.studyFormFill.jcgj') }}</div>
            </div>
            <JcgjList ref="jcgjList" @handleQuery="getJjcgjList" :showXg="true" />
            <pagination v small layout="prev, pager, next" :page.sync="queryParamsJcgj.pageNum"
              :limit.sync="queryParamsJcgj.pageSize" :total="jcgjTotal" @pagination="getJjcgjList" />
          </div>
        </div>
      </div>
    </div>

    <!-- 填报 -->
    <el-dialog :title="$t('page.business.study.studyFormFill.cjjl')" :visible.sync="openApprove" width="500px"
      append-to-body :close-on-click-modal="false">
      <el-form ref="formApprove" :model="formApprove" :rules="rulesApprove" label-width="120px" v-if="openApprove">
        <el-alert :title="$t('page.business.study.studyFormFill.ts')" :closable="false" type="success">
        </el-alert>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.qmyy')" prop="qmyy">
              <el-input type="text" :value="formApprove.qmyy" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.remark')" prop="remark">
              <el-input type="textarea" v-model="formApprove.remark" :rows="5" maxlength="500"
                :placeholder="$t('form.placeholderInput')">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.signer')">
              <el-input type="text" v-model="nickName" maxlength="50" disabled
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('form.password')" prop="qmrmm">
              <el-input type="password" show-password v-model="formApprove.qmrmm" maxlength="20"
                :placeholder="$t('form.placeholderInput')" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="approve">{{ $t('form.confirm') }}</el-button>
        <el-button @click="openApprove = false">{{ $t('form.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { studyFormFill_jcgjqmxxList, studyFormFill_uploadFile, studyFormFill_exportByFileUrl, studyFormFill_tb, studyFormFill_info, studyFormFill_jcgj, studyFormFill_qmxx, studyFormFill_exportDetail } from "@/api/business/study/studyFormFill"
import { getToken } from "@/utils/auth"
import { mapGetters } from 'vuex'
import JcgjList from "@/views/business/comps/common/JcgjList";
import JcgjExportList from "@/views/business/comps/common/JcgjExportList";
import TemplateTable from '@/views/business/comps/template/TemplateTable';
import moment from "moment";
import VueHtml2pdf from 'vue-html2pdf'
export default {
  name: "Xq",
  components: { JcgjExportList, JcgjList, TemplateTable, VueHtml2pdf },
  data() {
    return {
      watermarkText: '',
      appTitle: process.env.VUE_APP_TITLE,
      baseUrl: process.env.VUE_APP_FILE_DOMAIN,
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/file/upload', // 上传文件服务器地址
      opacity: 0.8,
      watermarkOpacity: 0.8,
      watermarkSize: 40,
      openApprove: false,
      formApprove: {
        id: null,
        qmyy: this.$t('page.business.study.studyFormFill.cjjl'),
        remark: '',
        qmrmm: '',
      },
      rulesApprove: {
        qmrmm: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]
      },
      qmxxList: [],
      totalQmxx: 0,
      loadingQmxx: true,
      queryParamsQmxx: {
        formId: null,
        pageNum: 1,
        pageSize: 5
      },
      open: false,
      showIndex: 1,
      form: {},
      rules: {
        bdmc: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }],
        templateId: [{
          required: true,
          message: ' ',
          trigger: 'blur'
        }]

      },
      jcgjTotal: 0,
      jcgjList: [],
      queryParamsJcgj: {
        pageNum: 1,
        formId: null,
        pageSize: 5,
      },
      showExport: false,
      qmxxExportList: [],
      jcgjExportList: [],
      jcgjlxExport: 0
    }
  },
  computed: {
    ...mapGetters([
      'nickName', 'name'
    ]),
  },
  created() {
  },
  methods: {
    //创建水印-弃用
    updateWatermark() {
      // 创建水印背景
      const text = this.nickName + ' ' + moment().format("YYYY-MM-DD HH:mm:ss");
      const canvas = document.createElement('canvas');
      const ctx = canvas.getContext('2d');

      // 设置canvas尺寸
      canvas.width = 300;
      canvas.height = 300;

      // 绘制水印
      ctx.fillStyle = `rgba(255, 0, 0,  ${this.opacity})`;
      ctx.font = '12px Arial';
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';

      // 旋转45度
      ctx.translate(canvas.width / 2, canvas.height / 2);
      ctx.rotate(-45 * Math.PI / 180);
      ctx.fillText(text, 0, 0);

      // 设置为背景
      const container = this.$refs.watermarkContainer;
      container.style.backgroundImage = `url(${canvas.toDataURL()})`;
      container.style.backgroundRepeat = 'repeat';
    },
    //添加水印-弃用
    async addDynamicWatermark({ pdfContent }) {
      return new Promise((resolve) => {
        const canvas = document.createElement('canvas')
        const ctx = canvas.getContext('2d')

        // 设置canvas尺寸
        canvas.width = 300;
        canvas.height = 300;


        // 绘制动态水印
        ctx.fillStyle = `rgba(100, 100, 100, ${this.watermarkOpacity})`
        ctx.font = `${this.watermarkSize}px Arial`
        ctx.textAlign = 'center'
        ctx.textBaseline = 'middle'

        // 计算水印密度
        const stepX = 300
        const stepY = 200

        // 绘制倾斜水印
        ctx.save()
        ctx.translate(canvas.width / 2, canvas.height / 2)
        ctx.rotate(-Math.PI / 4) // 45度倾斜
        let time = moment().format("YYYY-MM-DD HH:mm:ss")
        for (let x = -canvas.width; x < canvas.width * 2; x += stepX) {
          for (let y = -canvas.height; y < canvas.height * 2; y += stepY) {
            // 动态水印内容
            const dynamicText = `${this.watermarkText} - ${time}`
            ctx.fillText(dynamicText, x, y)
          }
        }
        ctx.restore()

        // 创建水印层
        const watermarkLayer = document.createElement('div')
        watermarkLayer.className = 'watermark-layer'
        watermarkLayer.style.cssText = `
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          pointer-events: none;
          background-image: url(${canvas.toDataURL('image/png')});
          background-repeat: repeat;
          z-index: 9999;
        `


        // 等待渲染
        setTimeout(() => resolve(), 100)
      })
    },
    //生成进度
    onProgress(progress) {
      console.log(`生成进度: ${progress}%`)
      this.removePageBreak()
      if (progress == 100) {
        this.showExport = false
        // this.$modal.closeLoading()
      } else {
        this.$modal.loading()
      }
    },
    //导出
    exportExcel(jcgjlx) {
      this.$modal.loading()
      debugger
      this.jcgjlxExport = jcgjlx
      // this.showExport = true
      // studyFormFill_jcgjqmxxList({ jcgjlx: jcgjlx, id: this.form.id }).then(response => {
      //   this.jcgjExportList = response.data.jcgj
      //   this.qmxxExportList = response.data.qmxx
      //   // this.updateWatermark()
      //   this.$refs.jcgjExportList.init(this.jcgjExportList)
      setTimeout(() => {
        this.$refs.html2Pdf.generatePdf()
        this.saveSimpleLog({ jcmc: '填报表单详情导出', jcmcEn: 'Record Detail Export', name: this.form.bdmc + '（' + this.form.bdbh + '）', nameEn: this.form.bdmc + '（' + this.form.bdbh + '）' })
      }, 200);
      // })
    },
    //获取文件blog
    async handleBeforeDownload({ html2pdf, options, pdfContent }) {
      this.$modal.loading()
      // 1. 使用 html2pdf 手动构建 PDF，并获取底层的 jsPDF 实例
      const pdf = await html2pdf()
        .set(options)      // 应用你的配置选项
        .from(pdfContent)  // 指定 PDF 内容
        .toPdf()           // 转换为 PDF
        .get('pdf');       // 获取 jsPDF 实例

      // 2. 从 jsPDF 实例中输出 Blob 对象
      const blob = pdf.output('blob');

      // 3. 将 Blob 上传到你的服务器
      await this.uploadPdfToServer(blob);

      // 注意：如果你还想让文件自动下载，可以在这里调用 .save()
      // pdf.save('my-document.pdf');
    },
    //上传到服务器
    async uploadPdfToServer(blob) {
      const formData = new FormData();
      formData.append('file', blob, 'hxhq-export-form.pdf');
      try {
        const response = await fetch(this.uploadFileUrl, {
          method: 'POST',
          headers: {
            Authorization: "Bearer " + getToken(),
          },
          body: formData
        });
        // 检查响应状态
        if (!response.ok) {
          // 如果响应不成功，可以尝试获取错误信息
          const errorData = await response.json().catch(() => ({}));
          throw new Error(errorData.message || `HTTP error! status: ${response.status}`);
        }
        // 获取 JSON 数据
        const data = await response.json();
        // console.log('返回的JSON数据:', data);
        if (data.code == 200) {
          studyFormFill_exportByFileUrl(
            {
              url: data.data.url,
              studyFormFillId: this.form.id,
              jcgjlx: this.jcgjlxExport,
              version:this.appTitle,
              lang: this.$store.getters.language.split("_")[0]
            }
          ).then(response => {
            window.open(this.baseUrl + response.msg)
          }).finally(() => {
            this.$modal.closeLoading()
          })
        } else {
          this.$modal.msgError(data.msg)
          this.$modal.closeLoading()
        }
      } catch (error) {
        this.$modal.msgError("导出失败，稍后再试")
        this.$modal.closeLoading()
      }
    },
    //移除分页空白
    removePageBreak() {
      document.querySelectorAll('.html2pdf__page-break').forEach(el => {
        el.remove()
      })
    },
    //导出服务器-弃用
    exportExcel_bak(jcgjlx) {
      this.$modal.loading()
      studyFormFill_exportDetail(_.merge({}, this.queryParamsJcgj, { jcgjlx: jcgjlx, lang: this.$store.getters.language.split("_")[0] })).then(response => {
        window.open(process.env.VUE_APP_FILE_DOMAIN + response.msg)
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    getJjcgjList(val) {
      this.$modal.loading()
      if (val) {
        this.queryParamsJcgj = _.merge({}, this.queryParamsJcgj, val)
      }
      studyFormFill_jcgj(this.queryParamsJcgj).then(response => {
        this.jcgjList = response.rows
        this.jcgjTotal = response.total
        this.$refs.jcgjList.init(this.jcgjList)
      }).finally(() => {
        this.$modal.closeLoading()
      })
    },
    getQmxxList() {
      this.loadingQmxx = true
      studyFormFill_qmxx(this.queryParamsQmxx).then(response => {
        this.qmxxList = response.rows
        this.totalQmxx = response.total
        this.loadingQmxx = false
      })
    },
    cancel() {
      this.$emit('close')
      this.open = false
    },
    reset() {
      this.form = {
        id: null,
        studyId: null,
        bdbh: null,
        bdmc: null,
        bdsm: null,
        templateId: null,
        templateMc: null,
        bdnr: null
      }
      this.resetForm("form")
    },
    show(row) {
      this.reset()
      this.$modal.loading()
      this.formApprove.id = row.id
      this.queryParamsJcgj.formId = row.id
      this.queryParamsQmxx.formId = row.id
      studyFormFill_info({ id: row.id }).then(response => {
        this.form = response.data
        this.open = true
        this.getQmxxList()
        this.getJjcgjList()
        this.saveSimpleLog({ jcmc: '填报详情', jcmcEn: 'Record Detail', name: this.form.bdmc + '（' + this.form.bdbh + '）', nameEn: this.form.bdmc + '（' + this.form.bdbh + '）' })
      })
    },
    approve() {
      this.$refs["formApprove"].validate(valid => {
        if (valid) {
          this.$modal.loading()
          studyFormFill_tb(this.formApprove).then(response => {
            this.openApprove = false
            this.$emit('close')
            this.open = false
          }).finally(() => {
            this.$modal.closeLoading()
          })
        }
      })
    }
  }
}
</script>
<style scoped>
.content-title {
  width: 100%;
  background: #f9f9ff;
  font-size: 0.96rem;
  font-weight: bold;
  padding-left: 10px;
  height: 40px;
  page-break-inside: avoid;
  line-height: 40px;
  display: flex;
  justify-content: flex-start;
  text-align: left;
}

.line {
  width: 2px;
  float: left;
  height: 16px;
  margin-top: 12px;
  margin-right: 8px;
  border-left: #3178ff 3px solid;
}

.subtitle {
  height: 40px;
  line-height: 40px;
  color: #464647 !important;
}


.pdf-content {
  padding: 0px;
  font-family: Arial, sans-serif;


}

#watermark-overlay {
  position: absolute;
  top: -80px;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.6;
  font-size: 12px;
  pointer-events: none;
  z-index: 10;
  color: red;
}

.pdf-content h1 {
  color: #333;
  border-bottom: 2px solid #4CAF50;
  padding-bottom: 10px;
}

.upload-file-list .el-upload-list__item {
  margin-bottom: 0px !important;
}

.html2pdf__page-break {
  display: none !important;
}


.content {
  position: relative;
  z-index: 1;
}

.controls {
  margin-bottom: 20px;
  padding: 10px;
  background: #f5f5f5;
}

input {
  margin: 0 10px;
  padding: 8px;
}
</style>