<template>
  <div v-if="lists && lists.length">
    <el-table :data="lists" class="bg-title" style="width: 100%">
      <el-table-column prop="name" label="业务名称"> </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="changeDate" label="创建日期" sortable></el-table-column>
      <el-table-column label="操作" key="slot">
        <template #default="scope">
          <el-popconfirm
            title="确定删除这条业务吗?"
            confirm-button-text="确定"
            cancel-button-text="再想想"
            @confirm="deleteBusiness(scope.row)"
          >
            <template #reference>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-popconfirm>
          <el-button type="text" size="small" disabled>配置</el-button>..
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <p>没有记录哦~</p>
      </template>
    </el-table>
  </div>
  <el-empty v-else description="暂无业务信息"></el-empty>
</template>

<script setup>
import { useRouter } from 'vue-router';
import { defineProps, defineEmits } from 'vue';
import { useStore } from 'vuex';
import time from 'utils/time';
import { ElMessage } from 'element-plus';
import { delBusiness } from '@/apis/business.js';

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 },
});

const emit = defineEmits(['handleQueryBusiness']);

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

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

function changeDate(row) {
  const value = row.createTime;
  return time.dateFormat(value);
}

/**
 * 删除业务
 * @param {String} businessId
 */
async function deleteBusiness(row) {
  try {
    // 这里写接口  记得加 async await
    const params = { param: { businessId: row.id } };
    await delBusiness(params);
    emit('handleQueryBusiness');
    ElMessage.success('删除成功');
  } catch (error) {
    ElMessage.error(error || '删除失败');
    console.error('error: ', error);
  }
}
</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>
