<template>
  <ToolsDialog
    v-if="show"
    @close="$emit('update:show', false)"
    title="坐标定位"
    layoutKey="location"
    v-model:layout="toolsLayout"
  >
    <div class="content-wrap">
      <div class="type-wrap flex-align-center">
        <div class="type-item __hover"
             @click="activeFormat = item.value"
             :class="{active: item.value === activeFormat}"
             v-for="item in formatMapper"
             :key="item.value">
          {{item.label}}
        </div>
      </div>
      <template v-if="activeFormat === 1">
        <div class="item full-input flex-space-center">
          <div class="label">经度：</div>
          <el-input v-model="format.longitude"></el-input>
        </div>
        <div class="item full-input flex-space-center">
          <div class="label">纬度：</div>
          <el-input v-model="format.latitude"></el-input>
        </div>
      </template>
      <template v-else>
        <div class="item flex-space-center">
          <div class="label">经度：</div>
          <el-input v-model="format.lon1"/>
          <span>°</span>
          <el-input class="ml-16" v-model="format.lon2"/>
          <span>'</span>
          <el-input class="ml-16" v-model="format.lon3" />
          <span>"</span>
          <el-switch class="ml-20"
                     border-color="#68C3FF"
                     style="--el-switch-on-color: transparent; --el-switch-off-color: transparent"
                     inline-prompt
                     active-text="西"
                     inactive-text="东"
                     v-model="format.isEast"
          ></el-switch>
        </div>
        <div class="item flex-space-center">
          <div class="label">纬度：</div>
          <el-input v-model="format.lat1"/>
          <span>°</span>
          <el-input class="ml-16" v-model="format.lat2"/>
          <span>'</span>
          <el-input class="ml-16" v-model="format.lat3" />
          <span>"</span>
          <el-switch class="ml-20"
                     border-color="#68C3FF"
                     style="--el-switch-on-color: transparent; --el-switch-off-color: transparent"
                     inline-prompt
                     active-text="南"
                     inactive-text="北"
                     v-model="format.isSouth"
          ></el-switch>
        </div>
      </template>
      <div class="item flex-center">
        <div class="btn flex-center __hover" @click="toPosition">确定</div>
      </div>
    </div>
  </ToolsDialog>
</template>

<script lang="ts">
import {
  defineComponent,
  onMounted,
  ref,
  toRefs,
  reactive,
  watch,
  getCurrentInstance,
  ComponentInternalInstance,
  computed, nextTick
} from "vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import ToolsDialog from '../tools-dialog.vue'
export default defineComponent({
  name: "",
  components: {
    ToolsDialog
  },
  props: {
    show: {required: true},
    mapFuncToLocation: {required: true},
  },
  setup(props, { emit }) {
    const store = useStore();
    const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties;
    const formatMapper = [
      {label: '经纬度', value: 1},
      {label: '度分秒', value: 2},
    ]
    const state = reactive({
      activeFormat: 1,
      format: {
        longitude: null,
        latitude: null,
        lon1: null,
        lon2: null,
        lon3: null,
        isEast: true,
        lat1: null,
        lat2: null,
        lat3: null,
        isSouth: true,
      },
      toolsLayout: {
        width: 400,
        height: 265,
        left: document.body.clientWidth - 400 - 64,
        top: document.body.clientHeight - 265 - 70
      }
    });
    const toPosition = () => {
      //度°分′秒″转度
      const convertGPSToXY = (d, f, m) => {
        const _d = d
        let _f = f || 0
        const _m = m || 0
        _f = parseFloat(String(_f)) + parseFloat(String(_m / 60))
        const du = parseFloat(String(_f / 60)) + parseFloat(_d)
        return du
      }
      let coor = [parseFloat(state.format.longitude), parseFloat(state.format.latitude)]
      if (state.activeFormat === 2) {
        const lonS = state.format.isEast ? 1 : -1
        const latS = state.format.isSouth ? 1 : -1
        coor = [lonS * convertGPSToXY(state.format.lon1, state.format.lon2, state.format.lon3), latS * convertGPSToXY(state.format.lat1, state.format.lat2, state.format.lat3)]
      }
      props.mapFuncToLocation({position: coor})
    }
    onMounted(() => {
      nextTick(() => {
      })
    })
    return {
      ...toRefs(state),
      formatMapper,
      toPosition,
    }
  },
});
</script>
<style scoped lang="scss">
:deep(.content-wrap){
  color: rgba(255,255,255, .8);
  padding: 0 10px;
  .ml-16{
    margin-left: 16px;
  }
  .ml-20{
    margin-left: 20px;
  }
  .type-wrap{
    padding: 0 0 4px 13px;
    border-bottom: 1px solid rgba(104, 195, 255, .5);
    margin-bottom: 16px;
    .type-item{
      font-size: 14px;
      color: #C0C4CC;
      user-select: none;
      &.active{
        font-size: 18px;
        color: #68C3FF;
        font-weight: 500;
      }
    }
    .type-item+.type-item{
      margin-left: 24px;
    }
  }
  .item{
    .label{
      width: 70px;
      text-align: right;
      font-size: 14px;
    }

    .el-input{
      width: 50px;
      margin-right: 6px;
    }
    .el-select{
      flex: 1;
      .el-input{
        width: 100%;
      }
    }
    .el-input__inner{
      border-color: rgba(255, 255,255,.4);
      background-color: transparent;
      color: rgba(255,255,255, .8) !important;
      padding: 0;
      text-align: center;
    }
    .el-switch{
      .el-switch__core .el-switch__action{
        background: #68C3FF;
        z-index: 1;
      }
      .el-switch__label--left, .el-switch__label--right{
        color: rgba(255,255,255, .8);
        position: absolute;
      }
      .el-switch__label *{
        font-size: 12px;
      }
      .el-switch__label--left{
        left: 4px;
      }
      .el-switch__label--right{
        right: 4px;
      }
    }
  }
  .item + .item{
    margin-top: 16px;
  }
  .btn{
    width: 80px;
    height: 32px;
    background: linear-gradient(180deg, #4B8CFF 0%, #1152C6 100%);
    border-radius: 4px;
    user-select: none;
    color: #FFFFFF;
  }
  .full-input{
    .el-input{
      flex: 1;
      width: 100%;
      margin-right: unset;
    }
  }
}
</style>
