<script lang="ts" setup>
import { chunk, clone, difference } from 'lodash-es'

import {
  computed,
  defineEmits,
  defineProps,
  onMounted,
  onUnmounted,
  ref,
  watch,
} from 'vue'
import { VueDraggable } from 'vue-draggable-plus'
import { hideDownload } from '@/utils/image'
import { workPackageMap } from '@/config/workorder'
import { useUser } from '@/store/modules/user'
import { TASK_STATUS_OBJ } from '@/enums/index'
import {
  getTaskDetailInfo,
} from '@/api/task/task'
import { getAllfieldList, getfieldList } from '@/api/home/filter'
import { useKeydown } from '@/hooks/event/useKeydown'
import { formatToDateHMS } from '@/utils/dateUtil'

const props = defineProps({
  reviewType: {
    type: Number,
    default: () => 1,
    require: true,
  },
})
const emit = defineEmits(['passfun', 'nopassfun'])
const imageRef = ref<ComponentElRef | null>()
const show = ref(false)
const detailobj = ref(null)
const taskDetailInfo = ref<any>({})
const taskTableData = ref<any[]>([])
const userStore = useUser()
async function showModal(item: any, packid, imgid) {
  getTableData()
  taskDetailInfo.value = await getTaskDetailInfo(item.id, packid, imgid)
  detailobj.value = item
  show.value = true
}
function previewHandler(event: MouseEvent) {
  event.stopImmediatePropagation()
  event.stopPropagation()

  if (imageRef.value && (imageRef.value as any).src)
    (imageRef.value as any).mergedOnClick()
}
async function getTableData() {
  const useInfo = userStore.getUserInfo
  const listData = []
  const reviewType = 3 // 类型
  let res = await getAllfieldList(reviewType)
  const fieldList = (res as any)?.data
  res = await getfieldList(reviewType, useInfo.id)
  const userFieldList = (res as any)?.data.userFieldFixed
  const blueList = [
    '拜访终端名称',
    '定位信息',
    '拜访日期',
    '定位距离',
    '拜访小结',
    '拜访项目类别',
  ]

  fieldList.forEach((v) => {
    if (userFieldList.includes(v.name)) {
      let locationobj = { address: '' }

      if (v.name == 'location' && taskDetailInfo.value.ocrPicture[v.name] != null && taskDetailInfo.value.ocrPicture[v.name] != 'null') {
        locationobj = JSON.parse(
          taskDetailInfo.value.ocrPicture ? taskDetailInfo.value.ocrPicture[v.name] : {},
        )
      }
      const item = {
        label: v.fieldDesc,
        value:
          v.name == 'location'
            ? locationobj.address
            : taskDetailInfo.value.ocrPicture
              ? taskDetailInfo.value.ocrPicture[v.name]
              : '',
        key: v.name,
        blue: blueList.includes(v.fieldDesc),
      }
      listData.push(item)
    }
    return v
  })
  taskTableData.value = chunk(listData, 2)
  console.log(taskDetailInfo)
}
function closeModal() {
  show.value = false
}
function passfun(e = taskDetailInfo.value) {
  emit('passfun', e)

  // closeModal()
}
function nopassfun(e = taskDetailInfo.value) {
  emit('nopassfun', e)
  //  closeModal()
}

onMounted(async () => {

})
onUnmounted(() => {

})
defineExpose({
  showModal,
  closeModal,
  passfun,
  nopassfun,
  taskDetailInfo,
})
</script>

<template>
  <n-modal v-model:show="show" transform-origin="center">
    <n-card
      class="cardstyle"
      :bordered="false"
      size="huge"
      role="dialog"
      aria-modal="true"
    >
      <div class="topline" />
      <div class="bottomline" />
      <div class="wrapper">
        <div class="wrapper-title" style="color: #333;font-size:20px">
          任务ID:{{ detailobj.fromTaskName }}
        </div>
        <div class="close" @click="closeModal">
          <SvgIcon size="20" class="icon" name="close-none-border" />
        </div>
      </div>
      <div style="display:flex;margin-top:25px">
        <div
          class="left"
          :style="
            {
              backgroundImage: `url(${detailobj?.imgUrl})`,
            }

          "
        >
          <div class="preview" style="z-index: 5" @click="previewHandler">
            <SvgIcon size="16" name="zoom-out" />
          </div>
          <div class="big-mark" />
          <div class="time">
            <div class="time-item">
              <SvgIcon class="svg-time" color="#FFF" size="16" name="camera-time" />

              <span>{{ detailobj?.photoDateTimestamp ?? "- -" }}</span>
            </div>
            <div class="time-item time-item2">
              <SvgIcon class="svg-time" color="#FFF" size="16" name="submit-time" />

              <span>{{
                detailobj?.submitDateTimestamp
                  ? formatToDateHMS(Number(detailobj.submitDateTimestamp))
                  : "- -"
              }}</span>
            </div>
          </div>
          <div style="display: none">
            <n-image
              ref="imageRef"
              :img-props="{ onClick: hideDownload }"
              :src="detailobj?.imgUrl"
            />
          </div>
        </div>
        <div
          class="right"
        >
          <n-scrollbar style="max-height: 100%">
            <div
              style="
                  font-size: 22px;
                  font-weight: 500;
                  color: #0d0b22;
                  white-space: nowrap;
                  overflow: hidden;
                  text-overflow: ellipsis;
                     padding-left: 10px;
                "
            >
              <n-ellipsis style="font-size:22px;font-weight:550">
                {{ detailobj.fromTaskName }}
              </n-ellipsis>
            </div>
            <div class="tags">
              <div
                v-if="detailobj?.historyStates == 1"
                class="tag tag-submiting"
              >
                待审批
              </div>
              <div
                v-else-if="
                  detailobj?.historyStates == 2
                    || detailobj?.historyStates == 3
                "
                class="tag tag-submited"
              >
                已审批
              </div>
              <div
                v-if="detailobj?.historyStates == 2"
                class="tag tag-passed"
              >
                {{ TASK_STATUS_OBJ[detailobj.historyStates] }}
              </div>
              <div
                v-else-if="taskDetailInfo?.userapprove?.historyStates == 3"
                class="tag tag-not-passed"
              >
                {{ TASK_STATUS_OBJ[detailobj?.historyStates] }}
              </div>
            </div>
            <n-divider class="divider-line" />
            <div :style="{ width: '90%', marginLeft: '16px', marginTop: '-6px' }">
              <div
                v-for="(item, index) in taskTableData"
                :key="index"
                :style="{ width: '100%', fontWeight: '450' }"
              >
                <div
                  v-if="item[0].label == '拜访终端名称'"
                  :style="{ display: 'flex', height: '100%', width: '100%' }"
                >
                  <div
                    :style="{
                      width: '115px',
                      borderRight: '2px solid',
                      marginTop: '4px',
                      color: '#333333',
                      fontWeight: 'bold',
                    }"
                  >
                    {{ item[0].label }}
                  </div>
                  <div
                    :style="{
                      marginLeft: '15px',
                      fontSize: '16px',
                      textOverflow: 'ellipsis',
                      paddingTop: '2px',
                      color: '#333333',
                      fontWeight: 'bold',
                      overflow: 'hidden',
                    }"
                  >
                    <n-ellipsis style="font-weight:550">
                      {{ item[0].value }}
                    </n-ellipsis>
                  </div>
                </div>

                <div v-if="!(item[0].label == '拜访终端名称')" class="viewlabel">
                  {{ item[0].label }}
                </div>
                <div v-if="!(item[0].label == '拜访终端名称')" class="viewvalue">
                  {{ item[0].value }}
                </div>
                <div
                  v-if="item[1] && !(item[1].label == '拜访终端名称')"
                  class="viewlabel"
                >
                  {{ item[1].label }}
                </div>
                <div
                  v-if="item[1] && !(item[1].label == '拜访终端名称')"
                  class="viewvalue"
                >
                  {{ item[1].value }}
                </div>
              </div>
            </div>
          </n-scrollbar>
        </div>
      </div>
      <n-divider class="divider-line" />
      <div class="btnlist">
        <n-button
          v-if="!(detailobj?.historyStates == 2 || detailobj?.historyStates == 3)" type="info"
          style="
     width:72px;
     height:38px; "
          @click="passfun"
        >
          通过
        </n-button>
        <n-button
          v-if="!(detailobj?.historyStates == 2 || detailobj?.historyStates == 3)" type="error"
          style="
     width:72px;
     height:38px;
     background-color: #FF4E4F;"
          @click="nopassfun"
        >
          不通过
        </n-button>
        <n-button
          style="
     width:72px;
     height:38px;
      position: absolute;
    right: 36px;" @click="closeModal"
        >
          取消
        </n-button>
      </div>
    </n-card>
  </n-modal>
</template>

<style lang="less" scoped>
.topline{
      background: #E0DFDF;
    height: 0.5px;
    width: 1200px;
    margin-left: -40px;
    position: absolute;
    top: 85px;
}
.bottomline{
     background: #E0DFDF;
    height: 1px;
    width: 1200px;
    margin-left: -40px;
    position: absolute;
    bottom: 73px;
}
 .big-mark {
        width: 100%;
        height: 210px;
        background: linear-gradient(
          180deg,
          rgba(0, 0, 0, 0) 0%,
          rgba(0, 0, 0, 0) 29%,
          rgba(3, 0, 0, 0.73)
        );
        position: absolute;
        left: 0;
        bottom: 0;
        z-index: 0;
        border-radius: 0 0 8px 8px;
      }
   .time {
        position: absolute;
        z-index: 3;
        left: 16px;
        bottom: 16px;
        .time-item {
          display: flex;
          align-items: center;
          font-size: 14px;
          font-family: PingFang SC, PingFang SC-Medium;
          font-weight: 500;
          color: #ffffff;
          margin-bottom: 4px;
        }

        .time-item2 {
          margin-bottom: 0;
        }

        .svg-time {
          margin-right: 5px;
        }
      }
       .preview {
        position: absolute;
        right: 10px;
        top: 10px;
        z-index: 3;
        width: 30px;
        height: 30px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 6px;
        backdrop-filter: blur(10px);
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
      }
.close{
        position: absolute;
    right: 40px;
    font-size: 20px;
    cursor: pointer;
}
.wrapper {
  display: flex;
  flex-direction: column;
 // border-bottom: 1px solid #e8e8e8;
  padding-bottom: 32px;

  &-title {
    font-weight: bold;
    font-size: 16px;
  }

  &-bar {
    width: 100%;
    margin-top: 20px;
  }

  &-footer {
    display: flex;
    justify-content: flex-end;
  }

  &-info {
    font-weight: bold;
    position: relative;
    color: #333333;
    font-size: 14px;

    &:before {
      background-color: #507afd;
      content: "";
      width: 5px;
      border-radius: 2px;
      top: 0;
      bottom: 0;
      position: absolute;
    }
  }
}

.dragcardStyle {
  --n-padding-bottom: 0px !important;
  --n-padding-left: 0px !important;
}

.cardstyle {
  width: 1200px;
  height: 678px;
  --n-padding-bottom: 20px;
  --n-padding-left: 24px;
}
.left{
     width: 65%;
     height: 456px;
     //     flex: 0.6;
      background-size: auto 100%;
      background-position: center;
      background-repeat: no-repeat;
      border-radius: 8px;
      position: relative;
    background-color: #f2f2f2;
}
.right{
    width: 30%;
//flex:0.4;
  margin-left: 16px;
  height: 463px;
  background: #FAFAFA;
  border-radius: 8px;
::v-deep(.n-divider .n-divider__line){
  background-color: #E8E8E8 !important;

}
.viewlabel {
  margin-top: 10px;
  font-size: 14px;
  font-family: PingFang SC, PingFang SC-Regular;
  font-weight: Regular;
  text-align: left;
  color: #999999;
  line-height: 20px;
  height: 20px;
}
.viewvalue {
  font-size: 14px;
  font-family: PingFang SC, PingFang SC-Regular;
  font-weight: Regular;
  text-align: left;
  color: #333333;
  line-height: 20px;
  height: 100%;
  margin: 3px 0 10px 0;
  max-width: 650px;
}
.tags {
      padding-left: 22px;
    display: flex;
    margin-top: 8px;
        margin-bottom: -12px;
  }

  .tag {
    border: 1px solid #d8d8d8;
    box-sizing: border-box;
    border-radius: 8px;
    color: #666666;
    margin-right: 12px;
    padding: 4px 8px;
    font-size: 12px;
    line-height: 16px;
  }

  .tag-submited {
    color: #507afd;
    border: 1px solid #507afd;
  }

  .tag-submiting {
    color: #ffb800;
    border: 1px solid #ffb800;
  }

  .tag-passed {
    color: #02c984;
    border: 1px solid #02c984;
  }

  .tag-not-passed {
    color: #ff7575;
    border: 1px solid #ff7575;
  }

  .tag-approved {
    color: #507afd;
    border: 1px solid #507afd;
  }
}
.textbtnStyle {
  cursor: pointer;
  color: #1980ff;
}

.btnlist{
       width: 165px;
    display: flex;
    float: right;
    margin-right: 90px;
    justify-content: space-between;
}

::v-deep(.n-card.n-card--content-segmented > .n-card__content:not(:first-child)) {
  border: 0px;
}

::v-deep(.n-card > .n-card-header) {
  --n-padding-top: 0px;
  --n-padding-bottom: 12px;
}
::v-deep(.n-card > .n-card-header .n-card-header__main) {
  font-weight: lighter !important;
  font-size: 14px;
}
::v-deep(.n-scrollbar) {
  border-top: none !important;
}
::v-deep(.n-button--info-type) {
  background: #507afd !important;
}
::v-deep(.checkAll .n-checkbox.n-checkbox--indeterminate .n-checkbox-box) {
  background: none;
  border: none;
}
::v-deep(.checkAll .n-checkbox-box__border) {
  border: 1px solid #e8e8e8 !important;
}
::v-deep(.checkAll .n-checkbox-icon) {
  border: 3px solid #fff;
  background: #1980ff;
}
::v-deep(.checkAll .n-checkbox-icon svg) {
  display: none !important;
}
::v-deep(.n-card-header__main) {
  color: #666 !important;
}
::v-deep(.n-button--secondary) {
  background-color: #fff;
  border: 1px solid #cad2dd !important;
}
::v-deep(.n-button--secondary):hover {
  background-color: #fff !important;
  border: 1px solid #cad2dd !important;
}
</style>
