<template>
  <div class="edit-add-detail detail show-scrollbar">
    <div
      v-loading="detailLoading"
      class="form-content"
      element-loading-background="rgba(10 ,38 ,61, 0.9)"
      element-loading-text="详情加载中..."
    >
      <el-form
        ref="formInfo"
        :model="detailForm"
        class="detail-form"
        label-position="right"
        label-suffix="："
      >
        <div class="rows">
          <div class="rows-left">
            <div class="rows-hr"></div>
          </div>
          <div class="rows-title">
            <div class="text-overflow">视频巡检任务管理</div>
          </div>
        </div>
        <el-row :gutter="24" style="flex-wrap: wrap" type="flex">
          <el-col :span="6">
            <el-form-item label="所属区域">
              <span :title="detailForm.roadName" class="text">
                {{ detailForm.roadName || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="24" style="flex-wrap: wrap" type="flex">
          <el-col :span="6">
            <el-form-item label="模版名称">
              <span :title="detailForm.confName" class="text">
                {{ detailForm.confName || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row :gutter="24" style="flex-wrap: wrap" type="flex">
          <el-col :span="6">
            <el-form-item label="巡检类型">
              <span
                :title="dictionaryEscape('xjlx-xjrw', detailForm.type)"
                class="text"
              >
                {{ dictionaryEscape('xjlx-xjrw', detailForm.type) || '-' }}
              </span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-button
              type="primary"
              :plain="activeName !== 'taskRecord' ? true : false"
              size="small"
              @click="handlechangeType('taskRecord')"
            >
              巡检记录
            </el-button>
            <el-button
              type="warning"
              :plain="activeName !== 'abnormalReport' ? true : false"
              size="small"
              @click="handlechangeType('abnormalReport')"
            >
              异常记录
            </el-button>
            <!-- <el-tabs v-model="activeName">
              <el-tab-pane name="taskRecord" label="巡检记录"> d </el-tab-pane>
              <el-tab-pane name="abnormalReport" label="异常上报">
                b
              </el-tab-pane>
            </el-tabs> -->
          </el-col>
          <el-col :span="24">
            <template v-if="activeName === 'taskRecord'">
              <el-form-item label="巡检记录"> </el-form-item>
              <el-row v-show="detailForm.taskConfVideo.length">
                <el-table
                  stripe
                  row-key="id"
                  class="table-list"
                  ref="multipleSelection"
                  :height="tableMaxHeight"
                  :data="detailForm.taskConfVideo"
                >
                  <el-table-column
                    align="center"
                    label="序号"
                    type="index"
                    width="120"
                  />
                  <el-table-column
                    align="center"
                    label="视频点名称"
                    prop="videoName"
                    show-overflow-tooltip
                  />
                  <el-table-column
                    align="center"
                    label="视频点编码"
                    prop="videoId"
                    show-overflow-tooltip
                  />
                </el-table>
              </el-row>
            </template>
            <template v-if="activeName === 'abnormalReport'">
              <el-form-item label="异常记录"> </el-form-item>
            </template>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <el-backtop :visibility-height="20" target=".detail-form">
      <div class="top-box">UP</div>
    </el-backtop>
  </div>
</template>

<script>
import {taskDetail} from '@/api/patrolAndRescue/videoPatrol/videoConfig'
import PubSub from 'pubsub-js'
import {dictionaryEscape} from '@/utils/dictionary'

export default {
  name: 'DetailPage',
  props: {
    componentName: {
      default: 'TablePage',
      type: String,
    },
  },
  components: {},
  data() {
    return {
      tableMaxHeight: 460,
      detailForm: {
        taskConfVideo: [],
      },
      detailLoading: true,
      pubSub: null,
      activeName: 'taskRecord',
    }
  },
  watch: {
    componentName: {
      handler(newVal) {
        if (newVal === this.$options.name) {
          this.openPage()
        } else {
          this.closePage()
        }
      },
    },
  },
  mounted() {
    this.pubSub = PubSub.subscribe('DetailPage', (_, data) => {
      console.log(data)

      if (data.id) {
        this.getDetail(data.id)
      }
    })
  },
  methods: {
    dictionaryEscape,

    // 打开页面执行
    openPage() {
      this.detailLoading = false
    },

    // 关闭页面执行
    closePage() {
      // 重置组件数据
      Object.assign(this.$data, this.$options.data.call(this), {
        pubSub: this.pubSub,
      })
      this.$nextTick((_) => {
        this.$refs.formInfo.$el.scrollTop = 0
      })
    },

    handlechangeType(type) {
      this.activeName = type
    },

    // 获取详情
    async getDetail(id) {
      this.detailLoading = false
      return
      try {
        const {
          videoIds = '',
          confType = '',
          type = '',
          taskConfVideo = [],
          ...detailForm
        } = await taskDetail(id)

        this.detailForm = {
          taskConfVideo,
          confType: confType.toString(),
          type: type.toString(),
          ...detailForm,
        }
      } finally {
        this.detailLoading = false
      }
    },
  },
  beforeDestroy() {
    // 在组件销毁之前 清除订阅消息
    PubSub.unsubscribe(this.pubSub)
  },
}
</script>

<style lang="scss" scoped>
.image-box {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 15px;
  margin: 5px 0;

  ::v-deep .el-image {
    width: 100px;
    height: 100px;
    border-radius: 4px;
    user-select: none;
    position: relative;
    transition: all 0.15s;
    box-shadow: 3px 3px 10px rgba(darken($--system-background-color, 30%), 0.3);

    .el-image__inner {
      transition: transform 0.2s;
      position: absolute;
    }

    &:hover {
      .el-image__inner {
        transform: scale(1.5);
      }
    }

    .el-image__error {
      background-color: $--system-box-color2;
    }

    .el-image__placeholder {
      background-color: $--color-text-placeholder;
    }

    &:has(.el-image__error, .el-image__placeholder) {
      box-shadow: none;
    }
  }
}
</style>
