<template>
  <el-table v-loading="loading" :data="data" v-on="$listeners">
    <el-table-column type="selection" width="55" align="center" />
    <!--      <el-table-column label="设备ID" align="center" prop="deviceId" width="100"/>-->
    <el-table-column label="图片" align="center" prop="picture" width="100">
      <template slot-scope="scope">
        <image-preview v-if="isEmpty(scope.row.customPicture)" :src="scope.row.picture" :width="50" :height="50"/>
        <image-preview v-else :src="scope.row.customPicture" :width="50" :height="50"/>
      </template>
    </el-table-column>
    <el-table-column label="二维码" align="center" width="80">
      <template slot-scope="d">
        <el-popover
          placement="top"
          width="180"
          trigger="hover">
          <div class="qr-code-box">
            <qr-code :text="qrCodeText(d.row)" :width="150" :height="150" />
            <div>{{d.row.deviceNo | defaultValue}}</div>
          </div>
          <el-button slot="reference" type="text" icon="el-icon-picture">查看</el-button>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="MAC-1" align="center" prop="mac" min-width="100" v-if="isAdmin">
      <device-link slot-scope="d" :text="d.row.mac" :id="d.row.deviceId"/>
    </el-table-column>
    <el-table-column label="MAC-2" align="center" prop="mac2" min-width="100" v-if="isAdmin">
      <device-link slot-scope="d" :text="d.row.mac2" :id="d.row.deviceId"/>
    </el-table-column>
    <el-table-column label="设备SN" align="center" prop="deviceNo" min-width="100">
      <template slot-scope="d">
        <el-link type="danger" v-if="d.row.deviceNo == null && isAdmin" @click="handleBindSn(d.row)">点击绑定</el-link>
        <device-link v-else :text="d.row.deviceNo" :id="d.row.deviceId"/>
      </template>
    </el-table-column>
    <el-table-column label="设备名称" align="center" prop="deviceName" >
      <device-link slot-scope="d" :text="d.row.deviceName" :id="d.row.deviceId"/>
    </el-table-column>
    <el-table-column label="型号" align="center" prop="model" />
    <el-table-column label="在线状态" align="center" prop="onlineStatus" width="80">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.sm_device_online_status" :value="scope.row.onlineStatus"/>
      </template>
    </el-table-column>
    <el-table-column label="状态" align="center" prop="status" width="80">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.sm_device_status" :value="scope.row.status"/>
      </template>
    </el-table-column>
    <el-table-column label="服务模式" align="center" prop="serviceMode" v-if="isAdmin">
      <template slot-scope="d">
        <dict-tag :options="dict.type.device_service_mode" :value="d.row.serviceMode"/>
      </template>
    </el-table-column>
    <el-table-column label="归属信息" align="center" width="180" >
      <template slot-scope="d">
        <template v-if="d.row.userId != null" >
          商户:
          <user-link v-if="!isEmpty(d.row.userMobile)" :id="d.row.userId" :name="d.row.userMobile"/>
          <user-link v-else :id="d.row.userId" :name="d.row.userName"/>
          <br/>
        </template>
        <template v-if="d.row.storeId != null">
          店铺:<store-link :id="d.row.storeId" :name="d.row.storeName"/><br/>
        </template>
        <template v-if="d.row.agentId != null && isAdmin">
          代理:<user-link :id="d.row.agentId" :name="d.row.agentName"/>
        </template>
      </template>
    </el-table-column>
    <el-table-column label="服务费" align="center" prop="serviceRate" width="180">
      <template slot-scope="d">
        <template v-if="d.row.monthFee">
          月费：{{d.row.monthFee | money | defaultValue}} 元 / 月 <br/>
        </template>
        <template v-if="d.row.serviceMode === DeviceServiceMode.DIRECT">
          {{isAdmin ? '平台' : '服务费'}}：{{d.row.realServiceRate | money | defaultValue}} % <br/>
        </template>
        <template v-if="d.row.serviceMode === DeviceServiceMode.AGENT">
          {{isAdmin ? '代理' : '服务费'}}：{{d.row.agentServiceRate | money | defaultValue}} % <br/>
        </template>
      </template>
    </el-table-column>
    <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip v-if="isAdmin"/>
    <el-table-column label="最后在线" align="center" prop="lastOnlineTime" width="100"/>
    <el-table-column label="绑定时间" align="center" prop="activationTime" width="100"/>
    <el-table-column label="到期时间" align="center" prop="rentTime" width="100"/>
    <el-table-column label="创建时间" align="center" prop="createTime" width="100" v-if="isAdmin"/>
    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
      <template slot-scope="scope">
        <slot :row="scope.row" name="operate"/>
      </template>
    </el-table-column>
  </el-table>
</template>
<script >
import QrCode from '@/components/QrCode/index.vue'
import StoreLink from '@/components/Business/Store/StoreLink.vue'
import DeviceLink from '@/components/Business/Device/DeviceLink.vue'
import UserLink from '@/components/Business/SmUser/UserLink.vue'
import { isEmpty } from '@/utils'
import { getWxIndexUrl } from '@/utils/wx'
import { DeviceServiceMode } from '@/utils/constants'
import { $smRoles, $view } from '@/utils/mixins'

export default {
  name: "DeviceTable",
  mixins: [$view, $smRoles],
  components: { UserLink, DeviceLink, StoreLink, QrCode },
  dicts: ['sm_device_online_status', 'sm_device_status', 'sm_device_outage_way','sm_device_notice_way', 'service_type', 'time_unit', 'device_service_mode'],
  props: {
    loading: {
      type: Boolean,
      default: false
    },
    data: {
      type: Array,
      default: () => {
        return []
      }
    },
  },
  computed: {
    DeviceServiceMode() {
      return DeviceServiceMode
    },
    // 二维码文本
    qrCodeText() {
      return (device) => {
        return getWxIndexUrl({ s: device.deviceNo});
      }
    },
  },
  methods: {
    isEmpty,
    handleBindSn(row) {
      this.form.deviceId = row.deviceId;
      this.showBindSn = true;
    },
  }
}
</script>

<style scoped>
.qr-code-box {
  text-align: center;
}
</style>

