import { PDFDocument } from 'pdf-lib'
import fontKit from '@pdf-lib/fontkit'

export default {
  print: {
    ignoreEncryption: true,
    methods: {
      async print (postDoc) {
        console.log(postDoc)
        const fontUrl = 'http://163.13.138.226/PMIS/KAIU.TTF' // 字體網址
        const templateUrl = 'http://163.13.138.226/PMIS/test1.pdf' // 樣板網址
        const fontBytes = await fetch(fontUrl).then((res) => res.arrayBuffer())
        let createDocumentDocRaw = await fetch(templateUrl) // pdf樣板檔
        const buffer = await createDocumentDocRaw.arrayBuffer()
        const createDocumentDoc = await PDFDocument.load(buffer)
        const createDocumentForm = createDocumentDoc.getForm()
        createDocumentDoc.registerFontkit(fontKit)
        const font = await createDocumentDoc.embedFont(fontBytes)

        // 把值填到欄位裡
        let documentnumber = createDocumentForm.getTextField(`documentnumber`)
        documentnumber.setText(postDoc.issue_number)
        documentnumber.updateAppearances(font)

        let saving = createDocumentForm.getTextField(`saving`)
        saving.setText('')
        saving.updateAppearances(font)

        let attributes = createDocumentForm.getTextField(`attributes`)
        attributes.setText('')
        attributes.updateAppearances(font)

        let register = createDocumentForm.getTextField(`register`)
        register.setText(postDoc.attention)
        register.updateAppearances(font)

        let postdate = createDocumentForm.getTextField(`postdate`)
        postdate.setText(postDoc.issue_date)
        postdate.updateAppearances(font)

        let postnumber = createDocumentForm.getTextField(`postnumber`)
        postnumber.setText(postDoc.issue_number)
        postnumber.updateAppearances(font)

        let species = createDocumentForm.getTextField(`species`)
        species.setText(postDoc.urgency_level_id)
        species.updateAppearances(font)

        let secret = createDocumentForm.getTextField(`secret`)
        secret.setText(postDoc.confidentiality_class_id)
        secret.updateAppearances(font)

        let release = createDocumentForm.getTextField(`release`)
        release.setText(postDoc.release_condition_id)
        release.updateAppearances(font)

        let opendate = createDocumentForm.getTextField(`opendate`)
        opendate.setText(postDoc.confidentiality_period)
        opendate.updateAppearances(font)

        let annex = createDocumentForm.getTextField(`annex`)
        annex.setText(postDoc.file_name)
        annex.updateAppearances(font)

        let subjection = createDocumentForm.getTextField(`subjection`)
        subjection.setText(postDoc.subject)
        subjection.updateAppearances(font)

        let content = createDocumentForm.getTextField(`content`)
        content.setText(postDoc.instruction)
        content.updateAppearances(font)

        let original = createDocumentForm.getTextField(`original`)
        original.setText(postDoc.original)
        original.updateAppearances(font)

        let copy = createDocumentForm.getTextField(`copy`)
        copy.setText(postDoc.copy)
        copy.updateAppearances(font)

        let decision = createDocumentForm.getTextField(`decision`)
        decision.setText(postDoc.decition_implementation_level_id)
        decision.updateAppearances(font)

        // 下載這個pdf ,第一個參數放pdf，第二個參數是載下來的檔案名稱
        require('downloadjs')(await createDocumentDoc.save(), '檔案名稱', 'application/pdf')
      }
    }
  }
}
