<template>
  <div>
    <div class="title"> 详细信息 </div>
    <div class="infoDiv">
      <a-row>
        <a-col :span="7">
          <span class="infotitle">图片名称</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue_name" v-if="editNameFlag">
            {{ props.nowPreviewRecord.name }}
            <EditOutlined style="font-size: 20px; color: #07aaed" @click="editNameChange" />
          </span>
          <span class="infovalue_name" v-if="!editNameFlag">
            <a-input v-model:value="editName" size="small" />
            <CheckOutlined
              style="margin-left: 10px; color: green"
              @click="pressEnterNameFunction"
            />
            <CloseOutlined style="margin-left: 10px; color: red" @click="editNameBlur" />
          </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">照片类型</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.imgtype }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">任务名称</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.taskname }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">航线名称</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.airlineName }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">照片分辨率</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">
            {{ props.nowPreviewRecord.width + '*' + props.nowPreviewRecord.height }}
          </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">照片大小</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.size }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">拍摄飞机</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.photographFeiji }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">拍摄负载</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.photographNumber }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">拍摄人员</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.photographMan }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">拍摄时间</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue">{{ props.nowPreviewRecord.photographTime }} </span>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">标签</span>
        </a-col>
        <a-col :span="17">
          <span class="infovalue_graffitiNum">
            <a-tag color="success" v-for="la in props.nowPreviewRecord.label" :key="la">
              {{ la }}
            </a-tag>
            <PlusSquareOutlined style="font-size: 20px; color: #07aaed" @click="addLabelChange" />
          </span>
          <a-modal
            title="标签设置"
            :open="addLabelFlag"
            :mask="false"
            :maskClosable="false"
            :closable="false"
            :footer="null"
          >
            <div style="display: block">
              <div
                style="display: flex; flex-wrap: wrap; gap: 5px; width: 96%; margin: 10px"
                v-if="props.nowPreviewRecord.label.length > 0"
              >
                已添加的标签：
                <div
                  v-for="la in props.nowPreviewRecord.label"
                  :key="la"
                  style="border: 1px solid #595959; border-radius: 3px"
                >
                  <span style="margin-left: 5px">{{ la }}</span>
                  <CloseOutlined
                    style="margin-left: 5px; margin-right: 5px"
                    @click="deleteLabel(la)"
                  />
                </div>
              </div>
              <div style="display: inline-flex; gap: 5px; width: 96%; margin: 10px">
                <a-input v-model:value="newLabelName" size="small" placeholder="请输入标签" />
                <a-button type="primary" @click="pressEnterLabelFunction">添加</a-button>
              </div>
              <div
                style="
                  display: inline-flex;
                  gap: 5px;
                  width: 96%;
                  margin: 5px 10px 20px 10px;
                  justify-content: flex-end;
                "
              >
                <a-button
                  @click="
                    addLabelFlag = false;
                    newLabelName = '';
                  "
                >
                  关闭
                </a-button>
              </div>
            </div>
          </a-modal>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">涂鸦总数</span>
        </a-col>
        <a-col
          :span="17"
          style="
            position: relative;
            display: flex;
            align-items: center;
            justify-content: flex-start;
          "
          @mouseenter="showGraffitiNum = true"
          @mouseleave="showGraffitiNum = false"
        >
          <div class="graffitiNum">
            <EditOutlined style="color: #ffffff" />
            <span>
              {{
                props.nowPreviewRecord.graffitiJson
                  ? JSON.parse(props.nowPreviewRecord.graffitiJson).length
                  : 0
              }}
            </span>
          </div>
          <div
            v-if="showGraffitiNum"
            style="position: absolute; top: 36px; left: 5px; width: 60%; z-index: 1000"
            :style="{
              height: `${options.length * 30}px`,
            }"
          >
            <div v-for="op in options" :key="op.text" style="width: 100%; height: 30px">
              <div class="graffitiNum2">
                <span>
                  {{ op.text }}
                </span>
                <span>
                  {{ op.num }}
                </span>
              </div>
            </div>
          </div>
        </a-col>
        <a-col :span="7">
          <span class="infotitle">照片位置</span>
        </a-col>
        <a-col :span="17">
          <span class="infobutton">
            <EnvironmentOutlined
              v-if="props.nowPreviewRecord.addOrRemoveToMap"
              style="font-size: 20px; color: #07aaed"
              @click="flyPoint"
            />
          </span>
        </a-col>
        <a-col :span="24">
          <div class="map">
            <Map
              ref="mapRef"
              :nowPreviewRecord="props.nowPreviewRecord"
              :previewRecordList="props.previewRecordList"
              @chooseNowPreviewRecord="chooseNowPreviewRecord"
              :hideOrShowTextboxFlag="props.hideOrShowTextboxFlag"
            />
          </div>
        </a-col>
      </a-row>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import { ref, computed } from 'vue';
  import {
    EditOutlined,
    PlusSquareOutlined,
    EnvironmentOutlined,
    CheckOutlined,
    CloseOutlined,
  } from '@ant-design/icons-vue';
  import { orgPosGroup } from '@/api/demo/system';
  import { Map } from './preview';
  import { useMessage } from '@/hooks/web/useMessage';
  const { createMessage, createConfirm } = useMessage();

  const props = defineProps(['nowPreviewRecord', 'previewRecordList', 'hideOrShowTextboxFlag']);
  const emit = defineEmits(['chooseNowPreviewRecord', 'reloadTable']);

  // 修改名称--------------------------------
  const editNameFlag = ref(true);
  const editName = ref(props.nowPreviewRecord.name.split('.').slice(0, -1).join('.'));
  function editNameChange() {
    if (props.nowPreviewRecord.name.split('.').length <= 1) {
      editName.value = props.nowPreviewRecord.name;
    }
    editNameFlag.value = false;
  }
  // 修改名称方法
  async function pressEnterNameFunction() {
    let newName: any = null;
    if (props.nowPreviewRecord.name.split('.').length <= 1) {
      newName = editName.value;
    } else {
      newName = editName.value + '.' + props.nowPreviewRecord.name.split('.').pop();
    }
    let query = {
      id: props.nowPreviewRecord.id,
      newName: newName,
    };
    props.nowPreviewRecord.name = newName;
    editNameFlag.value = true;
    emit('reloadTable');
    return;
    // 调用接口
    const data = await orgPosGroup(query);
    if (data) {
      editNameFlag.value = true;
      emit('reloadTable');
      return createMessage.success('修改名称成功');
    } else {
      return createMessage.error('修改名称失败');
    }
  }
  function editNameBlur() {
    editNameFlag.value = true;
    editName.value = props.nowPreviewRecord.name.split('.').slice(0, -1).join('.');
  }

  // 标签--------------------------------
  const addLabelFlag = ref(false);
  const newLabelName = ref('');
  function addLabelChange() {
    addLabelFlag.value = true;
  }
  // 添加标签方法
  async function pressEnterLabelFunction() {
    if (!newLabelName.value) {
      return;
    }
    if (!props.nowPreviewRecord.label.includes(newLabelName.value)) {
      props.nowPreviewRecord.label.push(newLabelName.value);
      let query = {
        id: props.nowPreviewRecord.id,
        newLabel: props.nowPreviewRecord.label,
      };
      addLabelFlag.value = true;
      chooseNowPreviewRecord({
        ...props.nowPreviewRecord,
        label: props.nowPreviewRecord.label,
      });

      emit('reloadTable');
      newLabelName.value = '';
      return;
      // 调用接口
      const data = await orgPosGroup(query);
      if (data) {
        addLabelFlag.value = true;
        emit('reloadTable');
        return createMessage.success('修改名称成功');
      } else {
        return createMessage.error('修改名称失败');
      }
    } else {
      return createMessage.error('此标签已存在！');
    }
  }

  // 删除标签
  function deleteLabel(value) {
    createConfirm({
      iconType: 'info',
      title: '提醒',
      content: '删除标签【' + value + '】，同标签名的涂鸦标记都会被删除！',
      onOk: () => {
        props.nowPreviewRecord.label = props.nowPreviewRecord.label.filter(
          (item) => item !== value,
        );
        let json = JSON.parse(props.nowPreviewRecord.graffitiJson);
        if (json.some((item) => item.text == value)) {
          json = json.filter((item) => item.text !== value);
          props.nowPreviewRecord.graffitiJson = JSON.stringify(json);
        }
        chooseNowPreviewRecord({
          ...props.nowPreviewRecord,
          label: props.nowPreviewRecord.label,
          graffitiJson: JSON.stringify(json),
        });
        emit('reloadTable');
      },
      onCancel: () => {},
    });
  }

  const showGraffitiNum = ref(false);
  const options = computed(() => {
    if (props.nowPreviewRecord.graffitiJson) {
      const map = {};
      // 遍历数组，统计每个 text 出现的次数
      JSON.parse(props.nowPreviewRecord.graffitiJson).forEach((item) => {
        const key = item.text;
        if (map[key]) {
          map[key].num += 1;
        } else {
          map[key] = { text: key, num: 1 };
        }
      });
      // 将 map 的值转成数组返回
      return Object.values(map);
    } else {
      return [];
    }
  });

  // 选择
  function chooseNowPreviewRecord(value) {
    emit('chooseNowPreviewRecord', value);
  }

  // 地图--------------------------------
  const mapRef = ref();
  function flyPoint() {
    mapRef.value?.flyToPoint([props.nowPreviewRecord.lng, props.nowPreviewRecord.lat]);
  }
</script>
<style lang="less" scoped>
  .title {
    position: relative;
    width: 50%;
    height: 60px;
    font-size: 15px;
    color: white;
    display: flex;
    align-items: center;
  }
  .infoDiv {
    position: relative;
    width: 90%;
    height: 480px;

    .infotitle {
      font-size: 15px;
      color: #ffffff;
      display: flex;
      align-items: center;
      height: 40px;
      margin-left: 10px;
    }

    .infovalue {
      font-size: 15px;
      color: #ffffff;
      display: flex;
      align-items: center;
      height: 40px;
      max-width: 250px;
      flex-wrap: wrap;
    }

    .infovalue_name {
      font-size: 15px;
      color: #ffffff;
      display: flex;
      align-items: center;
      height: 40px;
      max-width: 250px;
    }

    .infovalue_graffitiNum {
      font-size: 15px;
      color: #ffffff;
      display: flex;
      align-items: center;
      height: auto;
      min-height: 40px;
      max-width: 250px;
      flex-wrap: wrap;
    }

    .infobutton {
      font-size: 10px;
      color: #ffffff;
      display: flex;
      align-items: center;
      justify-content: right;
      height: 40px;
    }
  }

  .map {
    width: 100%;
    height: 240px;
    margin-left: 5px;
    border-radius: 5px;
  }

  .graffitiNum {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #ffffff;
    width: 60%;
    background: #3c3c3c;
    height: 30px;
    border-radius: 5px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .graffitiNum2 {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #ffffff;
    width: 95%;
    background: #1c1c1c;
    height: 40px;
    padding-left: 10px;
    padding-right: 10px;
  }
</style>
