<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.userDevice.filter.system')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="(item, index) in permissionsSysOriginPlatforms"
          :key="index"
          :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>
      <account-input
        v-model="listQuery.userId"
        :sys-origin="listQuery.sysOrigin"
        class="filter-item"
        :placeholder="$t('pages.userDevice.filter.userId')"
      />
      <el-input
        v-model.trim="listQuery.imei"
        :placeholder="$t('pages.userDevice.filter.deviceNo')"
        style="width: 200px;"
        class="filter-item"
      />
      <div class="filter-item">
        <el-date-picker
          v-model="rangeDate"
          value-format="timestamp"
          type="datetimerange"
          :picker-options="pickerOptions"
          :range-separator="$t('pages.userDevice.dateRange.separator')"
          :start-placeholder="$t('pages.userDevice.dateRange.start')"
          :end-placeholder="$t('pages.userDevice.dateRange.end')"
        />
      </div>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        :disabled="searchDisabled"
        @click="handleSearch"
      >
        {{ $t('pages.userDevice.action.search') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="$t('pages.userDevice.loading')"
      fit
      highlight-current-row
    >
      <el-table-column prop="id" label="ID" align="center" width="200" />
      <el-table-column prop="sysOrigin" :label="$t('pages.userDevice.table.system')" align="center" min-width="50">
        <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.userDevice.table.user')" align="center" min-width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.userId"
            type="text"
            @click="queryUserDetails(scope.row.userId)"
          >{{ scope.row.userNickname }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="registerNumber" :label="$t('pages.userDevice.table.registerCount')" align="center" min-width="80" />
      <el-table-column prop="ip" :label="$t('pages.userDevice.table.latestLoginIp')" align="center" min-width="200" />
      <el-table-column prop="imei" :label="$t('pages.userDevice.table.deviceNo')" align="center" min-width="80">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="copyContent(scope.row.imei)"
          >{{ $t('pages.userDevice.action.copy') }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="requestClient" :label="$t('pages.userDevice.table.requestPlatform')" align="center" min-width="80" />
      <el-table-column prop="phoneModel" :label="$t('pages.userDevice.table.phoneModel')" align="center" min-width="80" />
      <el-table-column prop="phoneSysVersion" :label="$t('pages.userDevice.table.operatingSystem')" align="center" min-width="80" />
      <el-table-column prop="appVersion" :label="$t('pages.userDevice.table.appVersion')" align="center" min-width="80" />
      <el-table-column prop="createTime" :label="$t('pages.userDevice.table.createdAt')" align="center" width="200">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" :label="$t('pages.userDevice.table.updatedAt')" align="center" width="200">
        <template slot-scope="scope">
          {{ scope.row.updateTime | dateFormat }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />

    <user-deatils-drawer
      v-if="userDeatilsDrawer"
      :user-id="thatSelectedUserId"
      @close="userDeatilsDrawer = false"
    />
  </div>
</template>

<script>
import { getUserDeviceTable } from '@/api/app-user'
import Pagination from '@/components/Pagination'
import { pickerOptions } from '@/constant/el-const'
import { sysOriginPlatforms } from '@/constant/origin'
import { copyText } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  name: 'UserDevice',
  components: { Pagination },
  data() {
    return {
      pickerOptions,
      rangeDate: '',
      thatRow: {},
      sysOriginPlatforms,
      userDeatilsDrawer: false,
      list: [],
      checkList: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        sysOrigin: '',
        startTime: '',
        endTime: ''
      },
      listLoading: true,
      searchDisabled: false
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatforms'])
  },
  watch: {
    rangeDate: {
      immediate: true,
      deep: true,
      handler(newVal) {
        if (newVal && newVal.length > 0) {
          this.listQuery.startTime = newVal[0]
          this.listQuery.endTime = newVal[1]
          return
        }
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    const that = this
    const querySystem = this.permissionsSysOriginPlatforms[0]
    if (!querySystem) {
      return
    }
    that.listQuery.sysOrigin = querySystem.value
    that.renderData()
  },
  methods: {
    renderData(isReset) {
      const that = this
      if (isReset === true) {
        that.listQuery.cursor = 1
      }
      that.listLoading = true
      getUserDeviceTable(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    queryUserDetails(userId) {
      this.userDeatilsDrawer = true
      this.thatSelectedUserId = userId
    },
    copyContent(text) {
      const that = this
      copyText(text).then(() => {
        that.$opsMessage.success()
      }).catch(er => {
        that.$opsMessage.fail()
      })
    }
  }
}
</script>
