<template>
  <schema-table
    ref="table"
    :schema="schema"
  >
    <strat-config-dialog ref="stratConfigDialog" />
    <table-dialog
      ref="historyDialog"
      :title="historyTitle"
      :schema="historySchema"
    />
    <device-service-config-dialog ref="deviceServiceConfigDialog" />
  </schema-table>
</template>

<script>
import { getDevicesByAdmin } from '@/api/device'
import { getHistory } from '../api'
import StratConfigDialog from './StratConfigDialog'
import DeviceServiceConfigDialog from './DeviceServiceConfigDialog'

export default {
  name: 'AITimingTable',
  components: {
    StratConfigDialog,
    DeviceServiceConfigDialog
  },
  props: {
    group: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      schema: {
        condition: { name: '' },
        list: this.getDevices,
        filters: [
          { key: 'name', type: 'search', placeholder: '设备名称' }
        ],
        buttons: [
          { label: '策略配置', on: this.onStratConfig },
          { label: '全部回采记录', on: this.onAllHistory }
        ],
        cols: [
          { prop: 'name', label: '设备名称', 'min-width': 120 },
          { prop: 'serialNumber', label: '序列号' },
          { prop: 'mac', label: 'MAC' },
          { type: 'invoke', width: 180, render: [
            { label: '服务配置', on: this.onDeviceServiceConfig },
            { label: '回采记录', on: this.onDeviceHistory }
          ] }
        ]
      },
      device: null
    }
  },
  computed: {
    historyTitle () {
      return this.device ? `${this.device.name}的回采记录` : '全部回采记录'
    },
    historySchema () {
      return {
        condition: { auditState: void 0 },
        list: this.getHistory,
        transform: this.transform,
        filters: [
          { key: 'auditState', type: 'select', placeholder: '全部状态', options: [
            { value: 7, label: '合规' },
            { value: 2, label: '疑似' },
            { value: 1, label: '不合规' }
          ] }
        ],
        cols: [
          { prop: 'file', label: '截图', type: 'asset' },
          { prop: 'ai', label: 'AI审核', type: 'tag', width: 180 },
          this.device ? null : { prop: 'deviceName', label: '设备名称' },
          { prop: 'screenshotTime', label: '回采时间' }
        ]
      }
    }
  },
  watch: {
    group () {
      this.$refs.table.pageTo(1)
    }
  },
  methods: {
    getDevices (params) {
      return getDevicesByAdmin({
        tenant: this.group,
        ...params
      })
    },
    getHistory (params) {
      return getHistory({
        tenant: this.group,
        deviceId: this.device ? this.device.id : void 0,
        ...params
      })
    },
    transform ({ deviceName, screenshotTime, screenshotUrl, screenshotDeleted, auditState, auditMsg }) {
      return {
        file: screenshotUrl && !screenshotDeleted
          ? { thumbnail: screenshotUrl }
          : null,
        ai: this.getAIState(auditState, auditMsg),
        deviceName,
        screenshotTime
      }
    },
    getAIState (status, msg) {
      switch (status) {
        case 1:
          return {
            type: 'danger',
            label: '不合规'
          }
        case 2:
          return {
            type: 'warning',
            label: '疑似',
            msg
          }
        case 3:
          return {
            type: 'info',
            label: '审核失败',
            msg
          }
        case 4:
        case 5:
        case 6:
          return {
            type: 'primmary',
            label: '审核中'
          }
        case 7:
          return {
            type: 'success',
            label: '通过'
          }
        case 8:
          return {
            type: 'info',
            label: '无法审核',
            msg
          }
        case 9:
          return {
            type: 'info',
            label: '未开启',
            msg
          }
        default:
          return null
      }
    },
    onServiceConfig () {
      this.$refs.serviceConfigDialog.show(this.group)
    },
    onStratConfig () {
      this.$refs.stratConfigDialog.show(this.group)
    },
    onAllHistory () {
      this.device = null
      this.$refs.historyDialog.show()
    },
    onDeviceServiceConfig ({ id, name }) {
      this.$refs.deviceServiceConfigDialog.show(id, name)
    },
    onDeviceHistory ({ id, name }) {
      this.device = { id, name }
      this.$refs.historyDialog.show()
    }
  }
}
</script>
