<template>
  <div class="edit-add-detail detail">
    <div class="form-content">
      <el-form
        ref="formInfo"
        :model="detailForm"
        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="margin-left: 80px">
          <el-col :span="6">
            <el-form-item label="队伍名称">
              <span :title="detailForm.orgName" class="text">
                {{ detailForm.orgName || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-left: 80px">
          <el-col :span="6">
            <el-form-item label="班种">
              <span :title="detailForm.name" class="text">
                {{ detailForm.name || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-left: 80px">
          <el-col :span="6">
            <el-form-item label="上班时间">
              <span :title="detailForm.workStartTime" class="text">
                {{ detailForm.workStartTime || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-left: 80px">
          <el-col :span="6">
            <el-form-item label="下班时间">
              <span :title="detailForm.workEndTime" class="text">
                {{ detailForm.workEndTime || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-left: 80px">
          <el-col :span="12">
            <el-form-item label="休息开始时间">
              <span :title="detailForm.restStartTime" class="text">
                {{ detailForm.restStartTime || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-left: 80px">
          <el-col :span="6">
            <el-form-item label="休息结束时间">
              <span :title="detailForm.restEndTime" class="text">
                {{ detailForm.restEndTime || '-' }}
              </span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" style="margin-left: 80px">
          <el-col :span="6">
            <el-form-item label="显示颜色">
              <div
                :style="{background: detailForm.markColor}"
                class="markColor"
              ></div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import {dictionaryEscape} from '@/utils/dictionary'
import {orgTree, tableDetail} from '@/api/patrolAndRescue/roadWatch/inspectScheduling/inspectWorkGroupContent'
import PubSub from 'pubsub-js'

export default {
  name: 'DetailPage',
  props: {
    componentName: {
      default: 'TablePage',
      type: String,
    },
  },
  components: {},
  data() {
    return {
      detailForm: {},
      pubSub: null,
    }
  },
  watch: {
    componentName: {
      handler(newVal) {
        if (newVal === this.$options.name) {
          this.openPage()
        } else {
          this.closePage()
        }
      },
    },
  },
  mounted() {
    this.pubSub = PubSub.subscribe('DetailPage', (_, data) => {
      if (data.id) {
        this.getOrgData(data)
      }
    })
  },
  methods: {
    dictionaryEscape,
    // 打开页面执行
    openPage() {},
    // 关闭页面执行
    closePage() {
      // 重置组件数据
      Object.assign(this.$data, this.$options.data.call(this), {
        pubSub: this.pubSub,
      })
      this.$nextTick((_) => {
        this.$refs.formInfo.$el.scrollTop = 0
      })
    },
    async getOrgData(data) {
      try {
        this.treeLoading = true
        let {value} = await orgTree() // 调用 API 获取树形数据
        this.orgOptions = value
        this.orgOptions = this.orgOptions.reduce((accumulator, item) => {
          return accumulator.concat(item.teamList);
        }, []);
        await this.getDetail(data.id)
      } finally {
        this.treeLoading = false // 无论成功或失败，都关闭加载状态
      }
    },
    // 获取详情
    async getDetail(id) {
      const data = await tableDetail(id)
      this.detailForm = {...data}
      this.detailForm.orgName = this.orgOptions.filter(item => this.detailForm.orgId === item.id)[0].name
    },
  },
  beforeDestroy() {
    // 在组件销毁之前 清除订阅消息
    PubSub.unsubscribe(this.pubSub)
  },
}
</script>

<style lang="scss" scoped>
.form-content {
    height: 0;
}
.markColor{
    width: 50%;
    height: 20px;
    margin-left: 40px;
}
</style>
