<template>
  <el-dialog
      class="my-dialog"
      fullscreen
      append-to-body
      destroy-on-close
      :model-value="showDialog"
      @close="closeDialog()"
  >
    <div class="pdf-dialog-box" >
      <div class="title" >
        <div class="name" ></div>
        <div class="pdf-close cursor-pointer" @click="closeDialog"></div>
      </div>
      <div class="pdf-my-body" >
        <div class="pdfBox" id="printTest" >
          <div class="a4">
            <div class="a4_title">确权单</div>
            <div class="a4_sub_title">111</div>
            <table class="a4_table" border=1 style="border-collapse: collapse;">
              <tr>
                <td class="name">农场地址</td>
                <td class="text">{{rowData.address}}</td>
                <td class="name">农场名称</td>
                <td class="text">{{rowData.farmName}}</td>
              </tr>
              <tr>
                <td class="name">创建时间</td>
                <td class="text">{{rowData.createDate}}</td>
                <td class="name">农场面积</td>
                <td class="text">{{rowData.area}}亩</td>
              </tr>
              <tr>
                <td class="name">作物物种</td>
                <td class="text">{{rowData.speciesTypeName}}</td>
                <td class="name">客户姓名</td>
                <td class="text">{{rowData.masterName}}</td>
              </tr>
              <tr>
                <td class="name">联系电话</td>
                <td class="text">{{rowData.masterTel}}</td>
              </tr>
            </table>
            <div class="a4_sub_title">果树分布图</div>
              <img class="img" v-if="imgUrl"  :src="imgUrl" />
              <div class="img" v-else ref="mapRef" id="mapRefId">
              </div>
          </div>
        </div>
      </div>
      <button class="yes-events" v-print="'#printTest'">去打印</button>

    </div>
  </el-dialog>
</template>

<script setup>
import {reactive, ref, toRefs, computed, nextTick, onMounted} from "vue";
import {useStore} from "vuex";
import {WKT} from "ol/format";
import html2canvas from "html2canvas"
import PdfMap from "./pdfMap";
import { dateFormat } from "@/utils/date_util";
import {getAreaByWkt} from "../../utils/map";
const emit = defineEmits(["closeDialog","success"])
const state = useStore().state;
const sendConfirmShow = ref(false)
const mapRef = ref(null)

async function send(){
  closeDialog();
}

const props = defineProps({
  title:{
    type:String,
    required:true
  },
  showDialog: {
    type: Boolean,
    default: true,
  },
  rowId:{
    type: Number,
    required:true,
  }
});

let imgUrl = ref(null)
const executor = ref([])
const executorList = ref([])

const formRef = ref(null);
const {title, rowId} = toRefs(props);
const closeDialog = (key) => {
  switch (key){
    case "sendConfirm":
      sendConfirmShow.value = false;
      return
  }
  emit("closeDialog", "pdf");
};

function  toImg(map) {
  let canvas = map.getViewport().querySelector('canvas');
  let dataURL = canvas.toDataURL('image/png');
  imgUrl.value = dataURL;
}


/**表单验证规则
 * @description:
 * @param {*} computed
 * @return {*}
 */
const rules = computed(() => ({
}));


let pdfMap = new PdfMap()
let rowData = ref({})


onMounted(()=>{
  VE_API.authentic.getDetails({id:rowId.value}).then(({data})=>{
    data.createDate = dateFormat(new Date(), "YYYY-mm-dd HH:MM:SS");
    data.area = getAreaByWkt(data.geom)
    rowData.value = data
    pdfMap.initMap(data,mapRef.value)
    setTimeout(function(){
      toImg(pdfMap.kmap.map)
    },1000)
  })
})

</script>

<style lang="scss" scoped>
$title-height:0px;
$body-height:calc(100% - $title-height);

.pdf-dialog-box{
  font-family: PingFangSC-Regular, PingFang SC;
  position: absolute;
  left: 25%;
  right: 25%;
  top:10%;
  bottom:10%;
  background: rgba(1,17,22,0.8);
  box-shadow: 0px 0px 20px 0px #00FFF0;
  border-radius: 4px;
  border: 2px solid rgba(81,233,240,0.6);

  .title{
    width: 100%;
    height: $title-height;
    box-sizing: border-box;
    background: rgba(0,77,101,0.8);
    border-radius: 4px 4px 0px 0px;
    border-bottom: 2px solid rgba(81,233,240,0.3);
    display: flex;
    align-items: center;
    justify-content: space-between;
    .name{
      margin-left: 20px;
      font-size: 16px;
      font-weight: 600;
      color: #00FFF0;
      height: 22px;
    }
    .pdf-close{
      margin-right: 20px;
      width: 16px;
      height: 16px;
      background-image: url("@/assets/img/close.png");
      background-size: 100% 100%;
    }
  }
  .pdf-my-body{
    width: 100%;
    height: $body-height;
    box-sizing: border-box;
    padding: 20px;
    ::-webkit-scrollbar-thumb {
      /* 滚动条里面小方块 */
      background: rgb(70, 71, 71);;
      border-radius: 6px;
    }
    ::-webkit-scrollbar-track {
      /* 滚动条里面轨道 */
      background: #ededed;
    }
  }

  .pdfBox{
    height: calc(100%);
    width: 100%;
    overflow-y: scroll;
    background-color: rgba(141, 142, 142);
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
  }

  .a4{
    font-family: PingFangSC-Medium, PingFang SC;
    background-color: #FFFFFF;
    width:595px;
    height:842px;
    margin: 5px 0px 5px 0px;
    padding: 19px 63px 19px 63px;
    box-shadow: 0px 0px 10px 0px #071010;
    font-weight: 600;
    color: #000000;
    .a4_title{
      width:595px;
      height: 33px;
      font-size: 24px;
      text-align: center;
      margin: 10px 0px 10px 0px;
    }
    .a4_sub_title{
      font-size: 16px;
      margin: 10px 0px 10px 0px;
    }
    .a4_table{
      height: calc(842px - 33px - 26px - 600px);
      max-height: calc(842px - 33px - 26px - 600px);
      width:595px;
      font-size: 12px;
      .name{
        width: 92px;
        height: 38px;
        line-height: 38px;
        padding-right: 9px;
        box-sizing: border-box;
        text-align: right;
      }
      .text{
        width: 122px;
        height: 38px;
        line-height: 38px;
        text-align: left;
        padding-left: 9px;
        box-sizing: border-box;
      }
    }
    .img{
      width:595px;
      height: 500px;
      z-index: 1000;
    }
  }


}

</style>
