import Vue from 'vue'
import { sealFile, convert, downloadSignedFile, dataURLtoBlob } from "@/utils/index.js";
import { converPdfToImage } from "@/utils/htmlToPDF";
import { initSign } from '@/utils/index';
import config from '@/config';

export default {
    data () {
        return {
            noticeType: 1, //1-立改 2-限改
        }
    },
    methods: {
        addAosEvent() {
            if (!this.$signAos) return
            this.$signAos.event.once('onSubmitSuccess', this.onSignSubmitSuccess)
            this.$signAos.event.once('onSignComplete', this.onSignComplete)
            this.$signAos.event.once('onCloseFrame', this.onCloseFrame)
        },
        async onSignSubmitSuccess(options) {
            const { signId, thirdBizId, files, sourceFileList, fileMd5List } = options;
            this.signId = signId
            this.$signAos.startView(signId)
            try {
                this.showOverLay = true;
                // 获取签字后的 pdf 文件流
                const signFile = await downloadSignedFile(files[0]);
                // 将 pdf流 转为 base64 的文件
                const signFileBase64 = await convert(signFile.data);
                // // 上传 base64 图片盖章
                const el = document.getElementById('pdfContainer');
                const y = Math.max(50, el.clientHeight - 120);
                const sealFileData = await sealFile(signFileBase64, {
                    "x": "250.00",
                    "y": y,
                });
                // const sealFileBase64 = await convert(sealFileData.data)
                // 将 pdf 的文件转换为 image
                let imgBase64 = null;
                //await converPdfToImage(new Blob([sealFileData.data], {type: 'application/pdf'}));
                // // 将盖章后的 pdf 文件流 转为 base64 的文件
                // this.tipMsg = "下发中...";
                await this.uploadPngAndPdf(
                    imgBase64,
                    new Blob([sealFileData.data], {type: 'application/pdf'})
                )
            } catch(err) {
                console.log(err);
                this.showOverLay = false;
                this.showSubmitBtn = true;
            }
        },
        async uploadPngAndPdf(imgblob, pdfblob) {
            this.showOverLay = true;
            // 上一步中的函数
            const name = this.noticeType === 2 ? '责令限期改正通知书' : '责令立即改正通知书';
            // var imgFd = new FormData();
            // imgFd.append("fileBinary", imgblob, "pdf.png");
            // imgFd.append("fileName", `${name}.png`);
            // // 上传图片
            // let imgUploadRes = await this.$http.post("/v2/api/file", imgFd);
            // if (imgUploadRes.data && imgUploadRes.data.id) {
            //     if (this.noticeType === 2) {
            //         this.formData.deadline_png_file_id = imgUploadRes.data.id;
            //     }
            //     else {
            //         this.formData.immediately_png_file_id = imgUploadRes.data.id;
            //     }
            // }
            // 上传 pdf
            const pdfFd = new FormData();
            pdfFd.append("fileBinary", pdfblob);
            pdfFd.append("fileName",`${name}.pdf`);
            // 上传 pdf
            const pdfUploadRes = await this.$http.post("/v2/api/file", pdfFd);
            if (pdfUploadRes.data && pdfUploadRes.data.id) {
                if (this.noticeType === 2) {
                    this.formData.deadline_file_id = pdfUploadRes.data.id;
                }
                else {
                    this.formData.immediately_file_id = pdfUploadRes.data.id;
                }
            }
            // 更新任务字段
            this.updateTaskNoticeFile();
        },
        onSignComplete() {
        },
        onCloseFrame() {
            // this.showOverLay = false;
            // this.showSubmitBtn = true;
        },
        startToSign(subData, x = 140, y = 34, tips) {
            try {
                this.addAosEvent()
                // 发起签署
                if (this.$signAos) {
                    this.$signAos.startSign({
                        signId: '',
                        // socialCreditCode: config.socialCreditCode,
                        files: [
                          {
                            file: subData,
                            sign: [{
                                x: x,
                                y: y,
                                width: 100,
                                height: 30,
                                page: 1,
                                required: true,
                                name: '签名',
                                tipText: tips || '被检查单位(场所)签名',
                            }]
                          }
                        ],
                        signType: 'TOUCH',
                        isFree: false,
                        thirdBizId: 'xxx',
                    });
                }
                else {
                    this.$toast.fail('签署还未就绪，请过会再试');
                    this.showOverLay = false;
                    this.showSubmitBtn = true;
                    initSign().then(res => {
                        Vue.prototype.$signAos = res
                    }).catch(_ => {})
                }
                
              }catch(err) {
                console.log(err);
              }
        }
    }
}