<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.pinnedRooms.placeholder.system')"
        style="width:120px;"
        class="filter-item"
        @change="changeSysOrigin"
      >
        <el-option
          v-for="item in permissionsSysOriginPlatforms"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span style="float: left;"> <sys-origin-icon :icon="item.value" :desc="item.value" /></span>
          <span style="float: left;margin-left:10px">{{ item.label }}</span>
        </el-option>
      </el-select>

      <div class="filter-item">
        <select-system-region
          ref="regionSelectPolicy"
          v-model="listQuery.region"
          :sys-origin="listQuery.sysOrigin"
          :placeholder="$t('pages.pinnedRooms.placeholder.region')"
        />
      </div>

      <div class="filter-item">
        <search-room-input @success="searchRoomSuccess" @fail="searchRoomFail" @load="loadSearchRoom" />
      </div>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :loading="searchDisabled"
        @click="handleSearch"
      >
        {{ $t('pages.pinnedRooms.action.search') }}
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        {{ $t('pages.pinnedRooms.action.add') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.pinnedRooms.word.loading')"
      fit
      highlight-current-row
      @cell-mouse-enter="handleMouseEnter"
    >
      <el-table-column :label="$t('pages.pinnedRooms.table.system')" align="center">
        <template slot-scope="scope">
          <sys-origin-icon :icon="scope.row.sysOrigin" :desc="scope.row.sysOrigin" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.pinnedRooms.table.region')" align="center" prop="regionName" />
      <el-table-column prop="roomProfile.roomName" :label="$t('pages.pinnedRooms.table.roomName')" align="center">
        <template slot-scope="scope">
          <el-link v-if="scope.row.roomProfile.roomName" @click="queryRoomDetails(scope.row.roomProfile.id)">
            {{ scope.row.roomProfile.roomName }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.pinnedRooms.table.roomCover')" align="center" width="150">
        <template slot-scope="scope">
          <el-image
            :lazy="true"
            style="width: 50px; height: 50px"
            :src="scope.row.roomProfile.roomCover"
            :preview-src-list="[scope.row.roomProfile.roomCover]"
          >
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="weights" :label="$t('pages.pinnedRooms.table.weight')" align="center" />
      <el-table-column :label="$t('pages.pinnedRooms.table.time')" align="center" width="200">
        <template slot-scope="scope">
          <div>{{ scope.row.startTime | dateFormat('yyyy-MM-dd HH:mm:ss') }}</div>
          <div>{{ scope.row.expiredTime | dateFormat('yyyy-MM-dd HH:mm:ss') }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="statusDesc" :label="$t('pages.pinnedRooms.table.status')" align="center" />
      <el-table-column :label="$t('pages.pinnedRooms.table.alwaysShow')" align="center">
        <template slot-scope="scope">
          {{ scope.row.foreverOnline === true ? $t('pages.pinnedRooms.word.yes') : $t('pages.pinnedRooms.word.no') }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.pinnedRooms.table.actions')" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click.native="handleUpdate()">{{ $t('pages.pinnedRooms.action.edit') }}</el-button>
          <el-button type="text" @click.native="handlDel(scope.row)">{{ $t('pages.pinnedRooms.action.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

    <form-edit
      v-if="formEditVisible"
      :row="thatRow"
      @close="formEditVisible = false"
      @success="formEditSuccess"
    />

    <room-deatils-drawer
      v-if="roomDeatilsDrawerVisible"
      :room-id="roomId"
      @close="roomDeatilsDrawerVisible=false"
    />
  </div>
</template>

<script>
import { pageSetTopRoom, removeSetTopRoom } from '@/api/sys'
import Pagination from '@/components/Pagination'
import { sysOriginPlatforms } from '@/constant/origin'
import FormEdit from './form-edit.vue'
import { mapGetters } from 'vuex'
import RoomDeatilsDrawer from '@/components/data/RoomDeatilsDrawer'

export default {
  components: { Pagination, FormEdit, RoomDeatilsDrawer },
  data() {
    return {
      searchDisabled: false,
      thatRow: {},
      // ---
      sysOriginPlatforms,
      txtVal: 0,
      activeGiftId: '',
      pushTextHistoryLoading: false,
      pushTextHistoryVisible: false,
      pushTextHistory: [],
      list: [],
      delarr: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        region: '',
        sysOrigin: 'TIM_CHAT'
      },
      formEditVisible: false,
      textOptTitle: '',
      listLoading: true,
      roomDeatilsDrawerVisible: false,
      roomId: ''
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatforms'])
  },
  created() {
    const that = this
    const querySystem = this.permissionsSysOriginPlatforms[0]
    if (!querySystem) {
      return
    }
    that.listQuery.sysOrigin = querySystem.value
    that.renderData()
  },
  methods: {
    renderData(isClean) {
      const that = this
      if (isClean === true) {
        this.listQuery.cursor = 1
        this.listQuery.list = []
      }
      that.listLoading = true
      pageSetTopRoom(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    renderDataSuccess() {
      this.$opsMessage.success()
      this.renderData()
    },
    queryUserDetails(row) {
      this.userDeatilsDrawer = true
      this.thatSelectedUserId = row.id
    },
    handlDel(row) {
      this.$confirm(this.$t('pages.pinnedRooms.confirm.delete'), this.$t('pages.pinnedRooms.confirm.title'), {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        removeSetTopRoom(row.id).then((res) => {
          this.listLoading = false
          this.$opsMessage.success()
          this.renderData()
        })
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleCreate() {
      this.thatRow = null
      this.formEditVisible = true
    },
    handleUpdate() {
      this.formEditVisible = true
    },
    handleMouseEnter(row) {
      this.thatRow = row
    },
    formEditSuccess() {
      this.formEditVisible = false
      this.renderData()
    },
    loadSearchRoom() {
      this.searchDisabled = true
    },
    searchRoomSuccess(res) {
      this.searchDisabled = false
      if (!res) {
        return
      }
      this.listQuery.roomId = res.id
    },
    searchRoomFail() {
      this.listQuery.roomId = ''
      this.searchDisabled = false
    },
    queryRoomDetails(roomId) {
      this.roomDeatilsDrawerVisible = true
      this.roomId = roomId
    },
    changeSysOrigin() {
      this.$refs.regionSelectPolicy.clearValue()
      this.handleSearch()
    }
  }
}
</script>
<style scoped lang="scss">
.popover-content {
  max-width: 300px;
  line-height: 20px;
}
</style>
