<template>
  <div class="body-layer">
    <div class="body-head">
      <h3>{{title}}</h3>
    </div>

    <el-table
      ref="multipleTable"
      :data="dataList.state"
      tooltip-effect="dark"
      style="width: 100%"
    >
      
      <el-table-column label="小区编码" v-slot:default="{ row }">
        {{row.gardenNumber}}
      </el-table-column>
      <el-table-column label="小区名称" v-slot:default="{ row }">
        {{row.gardenName}}
      </el-table-column>

      <el-table-column label="城市" prop="cityName"></el-table-column>
      <el-table-column label="片区|商圈" v-slot:default="{ row }" >
        <span v-if="row.regionName || row.geographyAreaName">{{row.regionName}}|{{row.geographyAreaName}}</span>
      </el-table-column>
      <el-table-column label="物业类型" prop="propertySortName"></el-table-column>
      
      <el-table-column label="建成年代" v-slot:default="{ row }">
        <span>{{row.buildStartTime}}-{{row.buildEndTime}}</span>
      </el-table-column>
      <el-table-column label="创建人" prop="creatorName"></el-table-column>
      <el-table-column label="所属公司" prop="companyName"></el-table-column>
      <el-table-column label="创建时间" prop="createTime"></el-table-column>
      <el-table-column label="状态" v-slot:default="{row}" width="90">
        <div class="status" :class="{interactive: row.auditStatus !== draftStatus}" @click="row.auditStatus !== draftStatus && selectRow(row)">
          <span>{{getStatusName(row.auditStatus)}}</span>
          <template v-if="row.auditStatus !== draftStatus || row.auditorName || row.auditTime">
            <i class="el-icon-document" v-if="row.auditStatus !== noAdoptStatus && row.auditStatus !== noComAdoptStatus"></i>
            <i class="el-icon-warning-outline error" v-else></i>
          </template>
        </div>
      </el-table-column>
      <el-table-column label="审核人" prop="auditorName"></el-table-column>
      <el-table-column label="审核时间" prop="auditTime"></el-table-column>
      <el-table-column label="操作" v-slot:default="{ row }">
        <span class="oper-span" @click="shareRow(row)" v-if="false">预览</span>
        <span class="oper-span"
          :class="{
            disable: !(
              (row.auditStatus === noAuditStatis && (user.role === 'admin' || user.role === 'group')) ||
              (row.auditStatus === noComAuditStatus && (user.role === 'admin' || user.role === 'region'))
            )
          }" 
          @click="() => {(
            (row.auditStatus === noAuditStatis && (user.role === 'admin' || user.role === 'group')) ||
            (row.auditStatus === noComAuditStatus && (user.role === 'admin' || user.role === 'region'))
          ) && oper.readyUpdate(row); oper.state.operStatus = examineMap.find(({name}) => name === '通过').value}" 
          v-if="auth.update">
          审核
        </span>
        <!-- <span class="oper-span" @click.stop="offlineItem(row)" :class="{disable: !~[adoptStatus, uplineStatus].indexOf(row.auditStatus)}">
          {{row.auditStatus === uplineStatus ? '下线' : '上线' }}
        </span> -->

      </el-table-column>
    </el-table>
    <com-pagination
      @size-change="pag.sizeChange"
      @current-change="pag.currentChange"
      :current-page="pag.state.currPage"
      :page-size="pag.state.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pag.state.total"/>
  </div>

  <com-record v-model:show="recordData.show" :data="recordData.id" v-if="recordData.show" />
  
  <com-dialog title="审核" v-model:show="oper.state.show" @submit="updateExamine()">
    <el-form ref="form" :model="oper.state" label-width="120px" class="examine-from">
      <el-form-item label="审核结果：">
        <el-radio-group v-model="oper.state.operStatus">
          <el-radio v-for="item in examineMap" :key="item.value" :label="item.value">{{item.name}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核备注：">
        <el-input type="textarea" v-model="oper.state.maker" maxlength="500" show-word-limit></el-input>
      </el-form-item>
    </el-form>
  </com-dialog>

  
  <com-share
    :setLogo="auth.update"
    v-if="shareData.show"
    v-model:show="shareData.show" 
    :items="shareData.data" />

</template>

<script>

import axios from 'axios';
import { computed } from "vue";
import qrcode from 'qrcode'
import comDialog from "@/components/dialog";
import comPagination from "@/components/pagination";
import comRecord from "@/view/properties/community/record";
import comShare from "@/view/properties/community/share";
import user from '@/state/user'
import {
  communityExamine,
  communityOnline
} from '@/request/config'
import { 
  HOUSE_TYPE_SELECT, 
  EXAMINE_REDIRECT_GROUP_SELECT,
  EXAMINE_REDIRECT_REGION_SELECT,
  EXAMINE_SHOW_SELECT_STATUS_3 as EXAMINE_SHOW_SELECT_STATUS, 
  EXAMINE_NO_ADOPT_STATUS,
  EXAMINE_NO_COM_ADOPT_STATUS,
  EXAMINE_NO_AUDIT_STATUS,
  EXAMINE_ADOPT_STATUS,
  EXAMINE_NO_COM_AUDIT_STATUS,
  EXAMINE_DRAFT_STATUS,
  getName,
  
  EXAMINE_UP_ONLINE_STATUS,

  REDIRECT_AUDIT_UP_STATUS,
  REDIRECT_AUDIT_OFF_STATUS,
  getRedirectCommunityAuditBody,
  REDIRECT_AUDIT_REJECT_STATUS,
  REDIRECT_AUDIT_REGION_STATUS,
  REDIRECT_AUDIT_GROUP_STATUS
} from '@/constant'
import getState from './state'

export default {
  name: 'examine',
  props: {
    parentVM: Object
  },
  setup(props) {
    const { headList, ...state } = getState(props.parentVM)
    const title = computed(() => {
      let item = headList.value.find(({value}) => value === state.search.value.state.auditStatus)
      return item ? item.name : ''
    })

    return { ...state, headList, title };
  },
  data() { 
    return {
      recordData: { show: false, id: 0 },
      examineMap: user.value.role === 'region' ? EXAMINE_REDIRECT_REGION_SELECT : EXAMINE_REDIRECT_GROUP_SELECT,
      draftStatus: EXAMINE_DRAFT_STATUS,
      noAdoptStatus: EXAMINE_NO_ADOPT_STATUS,
      adoptStatus: EXAMINE_ADOPT_STATUS,
      noAuditStatis: EXAMINE_NO_AUDIT_STATUS,
      noComAuditStatus: EXAMINE_NO_COM_AUDIT_STATUS,
      noComAdoptStatus: EXAMINE_NO_COM_ADOPT_STATUS,
      houseType: HOUSE_TYPE_SELECT,
      shareData: { show: false, data: 0 },
      user,
    }
  },

  methods: {
    selectRow(data) {
      this.recordData.show = true
      this.recordData.id = data.vrGardenId
    },
    async shareRow(raw, type = 1) {
      if (!raw.vrUrl) {
        return this.$confirm('暂无预览链接，请先设置初始场景。', '提示')
      }

      const vrLink = process.env.VUE_APP_DOMAIN + raw.vrUrl
      const QrCode = await qrcode.toDataURL(vrLink)

      let shareItems = [{
        QrCode: QrCode,
        type: 'building',
        noRandow: true,
        houseTitle: raw.gardenName,
        epcVrLink: vrLink.replace('hengda.html', 'epc.html'),
        vrLink: vrLink,
        innerVrLink: process.env.VUE_APP_INTRANET_DOMAIN + raw.vrUrl,
        status: raw.auditStatus,
      }]

      if (type === 1) {
        this.shareData.show = true
        this.shareData.data = shareItems
      } else {
        window.open(type === 1 ? shareItems[0].vrLink : shareItems[0].epcVrLink)
      }
    },
    getStatusName: getName(EXAMINE_SHOW_SELECT_STATUS),
    showSelectRow(data) {
      this.recordData.show = true
      this.recordData.id = data.id
    },
    async updateExamine() {
      if (!this.oper.state.operStatus) {
        return this.$alert('审核结果不能为空！', '提示')
      }

      const current = ~this.oper.state.operStatus 
        ? this.oper.state.auditStatus === EXAMINE_NO_AUDIT_STATUS 
          ? REDIRECT_AUDIT_GROUP_STATUS
          : REDIRECT_AUDIT_REGION_STATUS
        : this.oper.state.auditStatus === EXAMINE_NO_AUDIT_STATUS 
          ? REDIRECT_AUDIT_REJECT_STATUS
          : REDIRECT_AUDIT_REJECT_STATUS
      
      await axios.post(
        communityExamine, 
        getRedirectCommunityAuditBody(
          this.oper.state,
          current,
          this.oper.state.maker
        )
      )
      

      if (current === REDIRECT_AUDIT_GROUP_STATUS) {
        await axios.post(communityExamine, 
          getRedirectCommunityAuditBody(
            this.oper.state,
            REDIRECT_AUDIT_UP_STATUS,
            this.oper.state.maker
          )
        )
      }

      this.$alert('审核成功', '提示')
      
      this.oper.reset()
      this.dataList.refer()
    },

    async offlineItem(data) {
      if (data.auditStatus !== EXAMINE_ADOPT_STATUS && data.auditStatus !== EXAMINE_UP_ONLINE_STATUS) return;

      if (data.auditStatus === EXAMINE_UP_ONLINE_STATUS) {
        if (!(await this.$msgbox({
          message: '下线后VR链接将无法继续访问，确定要下线吗？', 
          title: '下线',
          showCancelButton: true,
          confirmButtonText: '下线',
          cancelButtonText: '取消',
        }))) {
          return;
        }
      }

      const status = data.auditStatus === EXAMINE_UP_ONLINE_STATUS ? REDIRECT_AUDIT_OFF_STATUS : REDIRECT_AUDIT_UP_STATUS

      await axios.post(
        communityOnline,
        getRedirectCommunityAuditBody(data, status)
      )

      this.$alert((status === REDIRECT_AUDIT_UP_STATUS ? '上' : '下') + '线成功', '提示')
      this.dataList.refer()
    },
    preview(data) {
      this.shareRow(data, 0)
    },
    
  },
  components: {
    "com-dialog": comDialog,
    "com-record": comRecord,
    "com-share": comShare,
    "com-pagination": comPagination
  },
};
</script>

<style lang="scss" scoped>
.table-ctrl-right {
  .search-scene {
    margin: 0 20px 0 26px;
  }
  i {
    margin-left: 20px;
    font-size: 1.7rem;
    vertical-align: middle;
    cursor: pointer;

    &.active {
      color: var(--primaryColor);
    }
  }
}

.examine-from {
  width: 320px;
  margin: 0 auto;
}

.interactive {
  cursor: pointer;
}

.error {
  color: var(--primaryColor);
}

</style>