<template>
  <a-modal
    v-model:visible="openReassignModal"
    width="900px"
    wrapClassName="reassign-modal"
    title="重新匹配"
  >
    <div class="reassign-table">
      <div class="row header">
        <div class="cell left">
          <a-checkbox
            :indeterminate="indeterminate"
            :checked="checkAll"
            @change="onCheckAllChange"
          >
            场景名称A
          </a-checkbox>
          <a-button type="link" size="small" @click="openPreview"
            >查看户型图</a-button
          >
        </div>
        <div class="cell right">
          <span>场景名称B</span>
          <a-button type="link" size="small" @click="openPreview"
            >查看户型图</a-button
          >
        </div>
      </div>
      <div v-for="(row, idx) in rows" :key="idx" class="row">
        <div class="cell left">
          <a-checkbox v-model:checked="row.checked">{{ row.a }}</a-checkbox>
        </div>
        <div class="cell right">
          <a-select
            v-model:value="row.b"
            style="width: 100%"
            :options="options"
          />
        </div>
      </div>
    </div>

    <template #footer>
      <a-button key="cancel" @click="cancelRelative">取消关联</a-button>
      <div>
        <a-button key="back" @click="openReassignModal = false">取消</a-button>
        <a-button
          key="submit"
          type="primary"
          :loading="loading"
          @click="handleOk"
          >确定</a-button
        >
      </div>
    </template>
  </a-modal>

  <a-modal
    v-model:visible="previewVisible"
    width="1000px"
    title="查看户型图"
    :footer="null"
  >
    <iframe
      ref="iframeRef"
      class="preview-iframe"
      :src="previewUrl"
      frameborder="0"
      @load="handleLoad"
    ></iframe>
  </a-modal>
</template>

<script lang="ts" setup>
import { ref, defineProps, reactive, computed, toRaw } from 'vue'

import type { ProjectScene } from '@/store'
// import { ui18n } from '@/lang'

defineOptions({ name: 'Reassign' })

const props = defineProps<{
  visible?: boolean
  scene: ProjectScene
  onSave: (data: any) => void
  onCancel: () => void
}>()
const openReassignModal = ref(props.visible ?? true)
const loading = ref(false)

const cancelRelative = () => {
  props.onCancel()
}

// 生成行数据
const count = (props.scene as any)?.shootCount || 2
const pad = (n: number) => (n < 10 ? `0${n}` : `${n}`)
type Row = { a: string; b: string; checked: boolean }
const rows = reactive<Row[]>(
  Array.from({ length: count }).map((_, i) => ({
    a: `点位${pad(i + 1)}`,
    b: `点位${pad(i + 1)}`,
    checked: false
  }))
)

const options = computed(() => rows.map(r => ({ label: r.b, value: r.b })))

const checkAll = computed({
  get() {
    return rows.length > 0 && rows.every(r => r.checked)
  },
  set(val: boolean) {
    rows.forEach(r => (r.checked = val))
  }
})
const indeterminate = computed(
  () => rows.some(r => r.checked) && !rows.every(r => r.checked)
)
const onCheckAllChange = (e: any) => {
  checkAll.value = e.target.checked
}

const previewVisible = ref(false)
const previewUrl = '/spg.html?m=SG-8PQm0PDdSZF&lang=zh&panoLabel'
const openPreview = () => {
  previewVisible.value = true
}

const handleOk = async () => {
  const payload = toRaw(rows).map((r, index) => ({ index, ...r }))
  await props.onSave(payload)
  openReassignModal.value = false
}

const iframeRef = ref<HTMLIFrameElement>()
const handleLoad = () => {
  let timer = setInterval(() => {
    if (iframeRef.value?.contentWindow?.__sdk) {
      clearInterval(timer)
      iframeRef.value?.contentWindow?.__sdk.Scene.whenLoaded(() => {
        iframeRef.value?.contentWindow?.__sdk.Camera.floorplan()
      })
    }
  }, 100)
}
</script>

<style lang="scss" scoped>
.reassign-table {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid #f0f0f0;
  border-radius: 4px;
  overflow: hidden;
  border-bottom: 0;
}

.row {
  display: contents;
}
.header .cell {
  background: #fafafa;
  font-weight: 600;
}

.cell {
  padding: 10px 12px;
  border-bottom: 1px solid #f0f0f0;
  border-right: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.cell.right {
  border-right: 0;
}
.preview-iframe {
  width: 100%;
  height: 70vh;
}
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;

  p {
    margin-bottom: 0;
    color: #646566;
    font-size: 14px;
  }
}
</style>
<style lang="scss">
.reassign-modal {
  .ant-modal-body {
    max-height: 660px;
    overflow: auto;
  }
  .ant-modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .ant-btn {
    min-width: 110px;
  }
  .controls-right-buttons {
    display: none;
  }
}
</style>
