<template>
  <div class="two-code">
    <main-page
      left-arrow
      :loading="loading"
      @click-left="handleClickLeft"
    >  
    <div class="mx-4 pt-14 pb-4">
      <div class="shadow-md rounded overflow-hidden" id="qrcode">
        <div class="bg-color-primary text-center h-24 pt-6">
          <div class="text-white flex-initial">{{entName}}</div>
        </div>
        <div class="bg-white">
          <div class="flex flex-row justify-around">
            <div class="pt-7 w-1/2 px-5">
              <!-- <van-image class="w-32 h-32"        
                :src="require('@/assets/icons/y-code.png')"
              /> -->
              <div class="w-32 h-32">
                <vue-qr
                  :logoSrc="require('@/assets/icons/js.png')"
                  :text="qrCodeText"
                  :margin="2"
                  :size="400"
                />
              </div>
            </div>
            <div class="w-1/2 space-y-2 flex-col text-base text-left pt-7 px-4">
              <div class="text-lg font-bold pb-1">{{ joinInfo.name || '张三' }}</div>
              <div class="flex items-start">
                <div class="flex-shrink-0 w-12">职位：</div>
                <div class="flex-1">{{ joinInfo.position }}</div>
              </div>
              <div class="flex items-start">
                <div class="flex-shrink-0 w-12">部门：</div>
                <div class="flex-1"> {{depInfo.name}}</div>
              </div>              
              <div>{{ formatDate(Number(joinInfo.joinTime)*1000, 'YYYY-MM-DD') || '2021-09-05' }}</div>
            </div>
          </div>
          <div class=" text-center text-gray-400 pb-8 pt-4">扫描二维码加入{{entName}}</div>
        </div>
      </div>
      <div class="w-24 mt-4 ml-auto">
        <c-button @click="saveQrcode">保存</c-button>
      </div>
      <div class="mt-4 border text-sm text-text-secondary border border-border rounded break-all p-2">
        {{copyText}}
      </div>
      <div class="flex mt-4 items-center justify-between">
        <div class="text-sm text-color-primary bg-color-primary-light px-6 py-2.5 rounded flex-shrink-0">仅适用于网页版打开</div>
        <div class="w-24">
          <c-button
            id="copyBtn"
            :data-clipboard-text="copyText"
            data-clipboard-action="copy"
            @click="copy">复制</c-button>
        </div>
      </div>
      <c-button type="secondary" class="mt-4" @click="handleClickLeft">进入组织架构</c-button>
    </div>  
    </main-page>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Image } from 'vant'
import { formatDate } from '@/util/FormatDate'
import VueQr from 'vue-qr'
import { Department } from '@/Interface'
import { downloadQrCode } from '@/util/DownloadQrcode'
// import download from 'downloadjs'

interface JoinInfoType{
  depId:string,
  entId:string,
  expiration:number,
  hash:string,
  inviterId:string,
  joinTime:number,
  name:string,
  oaServer:string,
  phone:string,
  position:string
}
Vue.use(Image)
export default Vue.extend({  
  name: 'Two-code',
    data() {
    return {
      formatDate,
      entName:'',
      joinInfo: {} as JoinInfoType,
      qrCodeText:'',
      loading: false,
      depInfo: {} as Department
    }
  },
  components: {
    'main-page': () => import('@/layout/main-page.vue'),
    'code-icon':()=>import('@/components/common/Icon.vue'),
    'c-button': () => import('@/components/common/c-button.vue'),
    VueQr,
  },
  created() {
    // console.log(Mock, 'mock')
    this.joinInfo = this.$store.state.acceptJoin
    const urloutput = this.outputUrl(this.joinInfo) 
    let url = window.location.origin + '/oa/team/confirm-join'
    this.qrCodeText = `${url}?${urloutput}`
    // this.drawCanvas()
    this.entName = JSON.parse(localStorage.getItem('ENT_INFO') as string).name
    console.log(this.joinInfo, 'joinInfo')
    this.getDepInfo(this.joinInfo.entId, this.joinInfo.depId)
  },
  computed: {
    copyText(): string {
      return `我和${this.entName}的小伙伴都在警书等你，用这个专属链接加入我们吧！\n${this.qrCodeText}`
    }
  },
  methods: {
    outputUrl(item:Object){
      let res =''
      const arr =  Object.entries(item)
      arr.map((i,index)=>{
        let string 
        if(index+1<arr.length){
            string = `${i[0]}=${encodeURIComponent(i[1])}&`
        }else{
          string = `${i[0]}=${encodeURIComponent(i[1])}`
        }
         res += string
      })
      return res
    },
    handleClickLeft() {
      this.$router.push('/team/team-frame')
    },
    async saveQrcode() {
      await downloadQrCode('qrcode')
    },
    copy() {
      const clipboard = new this.$util.clipboard('#copyBtn')
      clipboard.on('success', () => {
        this.$toast('已经复制到剪切板')
      })
      clipboard.on('error', () => {
        this.$toast('复制失败')
      })
    },
    getDepInfo(entId: string, depId: string) {
      this.loading=true
      this.$service.department.getDep({
        entId,
        id: depId
      }).then((res: any) => {
        this.loading=false
        const {data} = res
        if(data.code === this.$global.success) {
          this.depInfo = data.data
        }else {
          this.$toast(data.msg)
        }
      })
    }
  }
})
</script>

<style lang="less">
  
</style>