<template>
  <div class="guns-layout">
    <div class="guns-layout-content">
      <div class="guns-layout">
        <div class="guns-layout-content-header" style="height: auto">
          <span>全部租户</span>
          <a-space :size="16">
            <a-button type="primary" class="border-radius" @click="openAddEdit()"><plus-outlined />新建</a-button>
          </a-space>
        </div>
        <div class="guns-layout-content-application">
          <div class="content-mian">
            <div class="content-mian-body">
              <div class="table-content">
                <common-table
                  :columns="columns"
                  :where="where"
                  :showToolTotal="false"
                  :fieldBusinessCode="fieldBusinessCode"
                  :rowSelection="false"
                  showTableTool
                  rowId="tenantId"
                  size="middle"
                  ref="tableRef"
                  url="/tenant/page"
                >
                  <template #toolLeft>
                    <a-input
                      v-model:value="where.searchText"
                      placeholder="租户名称、租户编码（回车搜索）"
                      @pressEnter="reload"
                      :bordered="false"
                      style="width: 300px"
                      class="search-input"
                    >
                      <template #prefix>
                        <icon-font iconClass="icon-opt-search"></icon-font>
                      </template>
                    </a-input>
                  </template>
                  <template #bodyCell="{ column, record }">
                    <template v-if="column.dataIndex == 'tenantName'">
                      <a @click="openDetail(record)">{{ record.tenantName }}</a>
                    </template>
                    <!-- 图标 -->
                    <template v-if="column.dataIndex == 'tenantLogoWrapper'">
                      <img :src="record.tenantLogoWrapper" alt="" class="appIconWrapper" />
                    </template>
                    <!-- 状态 -->
                    <template v-if="column.dataIndex == 'statusFlag'">
                      <span class="tenant-tag enable" v-if="record.statusFlag == 1">启用</span>
                      <span class="tenant-tag error" v-if="record.statusFlag == 2">禁用</span>
                      <span class="tenant-tag delete" v-if="record.statusFlag == 3">准备删除</span>
                    </template>
                    <template v-if="column.dataIndex == 'tenantInitStatus'">
                      <span class="tenant-tag afoot" v-if="record.tenantInitStatus == 1">进行中</span>
                      <span class="tenant-tag" v-if="record.tenantInitStatus == 2 || !record.tenantInitStatus">完成</span>
                      <span class="tenant-tag error" v-if="record.tenantInitStatus == 3">
                        <a-tooltip>
                          <template #title>{{ record?.initErrorLog }}</template>
                          异常
                        </a-tooltip>
                      </span>
                    </template>
                    <template v-if="column.dataIndex == 'activeDate' || column.dataIndex == 'expireDate'">
                      {{ setTime(record[column.dataIndex]) }}
                    </template>
                    <!-- 操作 -->
                    <template v-if="column.key == 'action'">
                      <a-space :size="16">
                        <img
                          src="@/assets/images/start.svg"
                          alt=""
                          v-if="record.statusFlag == 2"
                          title="启用"
                          class="cu-img"
                          @click="statusFlagChange(record, 1)"
                        />
                        <img
                          src="@/assets/images/disabled.svg"
                          alt=""
                          v-if="record.statusFlag == 1"
                          title="禁用"
                          class="cu-img"
                          @click="statusFlagChange(record, 2)"
                        />
                        <img
                          src="@/assets/images/del.svg"
                          alt=""
                          v-if="record.statusFlag == 3"
                          title="取消删除"
                          class="cu-img"
                          @click="statusFlagChange(record, 1)"
                        />
                        <icon-font
                          iconClass="icon-opt-bianji"
                          font-size="24px"
                          title="编辑"
                          color="#60666b"
                          @click="openAddEdit(record)"
                        ></icon-font>
                        <icon-font
                          iconClass="icon-opt-shanchu"
                          font-size="24px"
                          title="删除"
                          color="#60666b"
                          v-if="record.statusFlag != 3"
                          @click="remove(record)"
                        ></icon-font>
                      </a-space>
                    </template>
                  </template>
                </common-table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="TenantManage">
import { TenantApi } from './api/TenantApi';
import { ref, createVNode, onMounted } from 'vue';
import { message, Modal } from 'ant-design-vue/es';
import { useRouter } from 'vue-router';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';

defineOptions({
  name: 'TenantManage'
});

const router = useRouter();

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true,
    customRender: ({ index }) => tableRef.value.tableIndex + index
  },
  {
    dataIndex: 'tenantName',
    title: '租户名称',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'tenantCode',
    title: '租户编码',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'tenantLogoWrapper',
    title: '租户logo',
    width: 80,
    isShow: true
  },
  {
    dataIndex: 'companyName',
    title: '公司',
    width: 100,
    isShow: true,
    ellipsis: true
  },
  {
    dataIndex: 'email',
    title: '申请人邮箱',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'safePhone',
    title: '申请人电话',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'statusFlag',
    title: '状态',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'tenantInitStatus',
    title: '初始化状态',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'activeDate',
    title: '租户生效时间',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'expireDate',
    title: '租户到期时间',
    width: 100,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    width: 80,
    isShow: true
  }
]);
// ref
const tableRef = ref(null);

// 搜索条件
const where = ref({
  searchText: ''
});
// 业务标识的编码
const fieldBusinessCode = ref('TENANT_MANAGEMENT');

onMounted(() => {});

// 点击搜索
const reload = () => {
  tableRef.value.reload();
};

// 新增编辑点击
const openAddEdit = record => {
  let query = {
    type: record ? 'edit' : 'add'
  };
  if (record) query.id = record.tenantId;
  router.push({
    path: '/tenant/manage/edit',
    query: query
  });
};

// 详情点击
const openDetail = record => {
  router.push({
    path: '/tenant/manage/edit',
    query: {
      type: 'detail',
      id: record.tenantId
    }
  });
};

// 删除单个
const remove = record => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的租户吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await TenantApi.delete({ tenantId: record.tenantId });
      message.success(res.message);
      reload();
    }
  });
};

// 切换租户状态
const statusFlagChange = (record, type) => {
  TenantApi.updateStatus({ tenantId: record.tenantId, statusFlag: type }).then(res => {
    message.success(res.message);
    reload();
  });
};

// 设置时间
const setTime = value => {
  let time = '';
  if (value) {
    time = value.substr(0, 10);
  }
  return time;
};
</script>

<style scoped lang="less">
.appIconWrapper {
  width: 22px;
  height: 22px;
}

.tenant-tag {
  padding: 4px 8px;
  border-radius: 4px;
  background: #f3f3f5;
  color: #60666b;
  font-size: 14px;
  font-style: normal;
  font-weight: 400;
  line-height: normal;
}
.afoot {
  background: #e8f3ff;
  color: #165dff;
}
.error {
  background-color: #ffedeb;
  color: #e02c1f;
}
.enable {
  background-color: #e9ffe3;
  color: #00b42a;
}
.delete {
  background-color: #fff7e9;
  color: #f67800;
}
.cu-img {
  cursor: pointer;
  margin-bottom: 2px;
}
</style>
