<template>
  <div class="user-room-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺名称')"
        prop="room_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('房间名称')"
        prop="room_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('房间封面')"
        prop="room_poster"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.room_poster"
            :preview-src-list="[scope.row.room_poster]"
            :src="scope.row.room_poster"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('直播状态')"
        prop="room_state"
        show-overflow-tooltip
        width="150px"
      />

      <el-table-column
        align="center"
        :label="__('创建时间')"
        prop="room_stime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('结束时间')"
        prop="room_etime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('允许直播')"
        prop="room_enable"
        width="125"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.room_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('是否推荐')"
        prop="room_recommend"
        width="125"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.room_recommend"

            :active-value="1"

            :inactive-value="0"
            @change="sRecChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('是否推荐')"
        prop="room_recommend"
        show-overflow-tooltip
      />
    </el-table>

    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList, doEdit } from '@/api/user/room'

  export default {
    name: 'UserRoom',

    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      formatState(row) {
        return row.room_state ? this.__('直播中') : this.__('离线')
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      async sChange(row) {
        const param = {
          room_id: row.room_id,
          room_enable: row.room_enable,
        }

        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },

      async sRecChange(row) {
        const param = {
          room_id: row.room_id,
          room_recommend: row.room_recommend,
        }

        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },
    },
  }
</script>
