<template>
  <div class="sfg-device-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.deviceId" clearable placeholder="请输入查询条件" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="品牌" prop="brand" show-overflow-tooltip />
      <el-table-column align="center" label="型号" prop="model" show-overflow-tooltip />
      <el-table-column align="center" label="系统版本" prop="system" show-overflow-tooltip />
      <el-table-column align="center" label="屏幕宽度（px）" prop="screenWidth" show-overflow-tooltip />
      <el-table-column align="center" label="屏幕高度（px）" prop="screenHeight" show-overflow-tooltip />
      <el-table-column align="center" label="像素比" prop="pixelRatio" show-overflow-tooltip />
      <el-table-column align="center" label="语言" prop="language" show-overflow-tooltip />
      <el-table-column align="center" label="版本号" prop="version" show-overflow-tooltip />
      <el-table-column align="center" label="基础库版本" prop="SDKVersion" show-overflow-tooltip />
      <el-table-column align="center" label="创建时间" show-overflow-tooltip>
        <template #default="{ row }">
          {{ formatTime(row.createdAt) }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getDeviceList } from '@/api/system'
  import { formatTime } from '@/utils'

  export default {
    name: 'SFGDeviceManagement',
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          deviceId: '',
          appId: '69665538a49b8ae3be50fe5d',
        },
        timeOutID: null,
      }
    },
    created() {
      const { deviceId } = this.$route.query || {}
      if (deviceId) {
        this.queryForm.deviceId = deviceId
        this.queryForm.pageNo = 1
      }
      this.fetchData()
    },

    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      formatTime,
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getDeviceList(this.queryForm)
        this.list = data.list
        this.total = data.totalCount
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
