<template>
  <el-table :data="lists" class="bg-title" style="width: 100%">
    <el-table-column prop="name" label="业务名称1"> </el-table-column>
    <el-table-column prop="appId" label="APPID" width="300"> </el-table-column>
    <el-table-column prop="startUsing" label="状态" key="slot" sortable>
      <template #default="scope">
        <div class="flex flex-row items-center" @click="change(scope.row)">
          <div :class="scope.row.startUsing ? 'point bg-green-500' : 'point bg-red-500'"></div>
          <span style="margin-left: 10px">{{ scope.row.startUsing ? '启动' : '禁用' }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="公开" sortable key="slot" width="180">
      <template #default="scope">
        <div class="flex flex-row items-center" @click="change(scope.row)">
          <div :class="scope.row.pub ? 'point bg-green-500' : 'point bg-red-500'"></div>
          <span style="margin-left: 10px">{{ scope.row.pub ? '公开' : '非公开' }}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" :formatter="dateFormat" label="创建日期" sortable></el-table-column>
    <el-table-column label="操作" key="slot">
      <template #default="scope">
        <el-button type="text" size="small">删除</el-button>
        <el-button type="text" size="small">配置</el-button>..
        <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import { useRouter } from 'vue-router';
import { defineProps } from 'vue';
import { useStore } from 'vuex';

const router = useRouter();
const store = useStore();
defineProps({
  lists: { default: () => [], type: Array },
  currentPage: { default: 1, type: Number },
  pageSize: { default: 10, type: Number },
  total: { default: 0, type: Number },
  showConfig: { default: false, type: Boolean },
});

function change(row) {
  console.log('row: ', row);
}

function handleClick(row) {
  console.log(row);
  router.push({ name: 'business-detail', query: { id: row.id } });
  store.commit('plugin/setBusinessInfo', row);
}

function dateFormat(row) {
  const value = row.createTime;
  if (value) {
    const date = new Date(Number(value)); // 时间戳为秒：13位数
    // let date = new Date(value * 1000)	// 时间戳为毫秒：10位数
    const year = date.getFullYear();
    const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1;
    const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate();
    const hour = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours();
    const minute = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes();
    const second = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds();
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  }
  return '';
}
</script>

<style scoped>
.bg-title >>> thead tr th {
  color: #333;
  background: #fafafa;
  border-top: 1px solid #e8e8e8;
}
.bg-title >>> thead tr th:first-child {
  border-left: 1px solid #e8e8e8;
}
.bg-title >>> thead tr th:last-child {
  border-right: 1px solid #e8e8e8;
}

.point {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}
</style>
