<template>
    <div>
        <LHeader v-bind:text="text"></LHeader>
        <div style="border: 2px solid #E6E6E6 ;background-color: white">
            <vue-esign ref="esign" :width="800" :height="600" :isCrop="isCrop" :lineWidth="lineWidth"  :lineColor="lineColor" :bgColor.sync="bgColor" />
        </div>
        <div class="page-footer">
            <p>请在上方空白处书写您的签字</p>
            <van-button round type="primary" size="large" @click="handleReset">清空画板</van-button> 
            <van-button round type="info" size="large" @click="handleGenerate">生成图片</van-button>
        </div>
        <tab-bar :index="1"></tab-bar>
    </div>
</template>

<script>
import tabBar from "@/components/TabBar";
import LHeader from '@/components/header.vue'
export default {
    name: 'sign',
    components: {
        "LHeader":LHeader,
        "tabBar":tabBar
    },
    data() {
        return {
            text:'电子签名',
            lineWidth: 6,
            lineColor: 'red',
            bgColor: '',
            resultImg: '',
            confirmImgl:'',
            isCrop: false,
            value:'',
            result:[],
            checkedAll:'',
            img:'',
            // img2:'',
            confirmImg:'',
        };
    },
    mounted(){
        if(this.$route.query.name=='sign'){
            this.img=this.$route.query.img
            // this.img2=this.$route.query.img2
        }
        if(this.$route.query.name=='confirmNote'){
            this.confirmImg=this.$route.query.confirmImg
        }
        if(this.$route.query.name=='confirmNotel'){
            this.confirmImgl=this.$route.query.confirmImgl
        }
    },
    methods: {
    handleReset () {
      this.$refs.esign.reset()
    },
    // 生成base64格式
    handleGenerate () {
        this.$refs.esign.generate().then(res => {
            this.resultImg = res //把base64赋给img
            //生成图片后存储到session中并跳转把图片是否显示的boolean带过去
            if(this.confirmImg){
                this.$router.push({name:'confirmNote',query:{name:'callbackSign',confirmImg:this.confirmImg}})
            }else if(this.confirmImgl){
                this.$router.push({name:'confirmNote',query:{name:'callbackSignl',confirmImgl:this.confirmImgl}})
            }else{
                this.$router.push({name:'noticeList',query:{name:'sign',img:this.img}})
            }
            //判断是检查组签字还是项目经理签字
            if(this.img){
                sessionStorage.setItem('resultImg',this.resultImg)
            }
            else if(this.confirmImgl){
                sessionStorage.setItem('confirmImgl',this.resultImg)
            }
            else if(this.confirmImg){
                sessionStorage.setItem('confirmImg',this.resultImg)
            }
        }).catch(err => {
            // 画布没有签字时会执行这里
            this.$dialog.alert({
                title: '提示',
                message: '提示：本人签名，非本人签名、代签者将承担一切法律责任及连带后果。',
                theme: 'round-button',
            })
        })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
    .van-button{
        width: 90%;
        margin-top: 0.3rem;
    }
    .page-footer{
        text-align: center;
    }
    .page-footer p{
        font-size: 0.3rem;
        font-weight: none;
    }
</style>