<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="积分规则名称" prop="ruleName">
              <el-input v-model="queryParams.ruleName" placeholder="请输入积分规则名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="规则创建时间" prop="createTime" style="width: 330px">
              <el-date-picker
                clearable
                v-model="queryParams.createTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" clearable>
                <el-option v-for="(label, value) in statusOptions" :key="value" :label="label" :value="value" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>

        <el-card shadow="never" class="mt-[10px]">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
          </template>

          <el-table v-loading="loading" :data="extractLogList">
            <el-table-column label="序号" width="50px" type="index"></el-table-column>
            <el-table-column
              :label="label"
              :align="align || 'center'"
              :min-width="minWidth || '100px'"
              v-for="{ label, prop, align, minWidth } in headers"
              :key="prop"
            >
              <template #default="{ row }">
                <span v-if="prop == 'status'">
                  <el-switch v-model="row[prop]" :active-value="1" :inactive-value="0" @change="changeStatus(row)"> </el-switch>
                </span>
                <span v-else-if="prop == 'expireTime'">
                  {{ row.isForever == 1 ? '永久' : row.expireTime || '-' }}
                </span>
                <span v-else-if="prop == 'remark'">
                  评价一颗星积一分
                </span>

                <span v-else>{{ row[prop] || '-' }}</span>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="page.total > 0" :total="page.total" v-model:page="page.pageNum" v-model:limit="page.pageSize" @pagination="getList" />
        </el-card>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { getTableList, setStatus } from '@/api/points/rule';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const extractLogList = ref<any[]>([]);
const loading = ref(true);
const showSearch = ref(true);

const queryFormRef = ref<ElFormInstance>();

interface Header {
  label: string;
  prop: string;
  align?: 'center' | 'left' | 'right';
  minWidth?: string | number;
}
const headers = ref<Header[]>([
  {
    label: '积分规则名称',
    prop: 'ruleName'
  },  {
    label: '说明',
    prop: 'remark'
  },
  {
    label: '规则创建时间',
    prop: 'createTime'
  },
  {
    label: '规则到期时间',
    prop: 'expireTime'
  },
  {
    label: '状态',
    prop: 'status'
  }
]);

const statusOptions = {
  0: '停用',
  1: '启用'
};
const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  ruleName: '',
  status: '',
  createTime: []
});

/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  let startDate = queryParams.value.createTime[0] || '';
  let endDate = queryParams.value.createTime[1] || '';
  const res = await getTableList({ ...queryParams.value, ...page.value, startDate, endDate });
  extractLogList.value = res.data;
  page.value.total = res.total;
  loading.value = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  page.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const changeStatus = async (row: any) => {
  const params = {
    id: row.id,
    status: row.status
  };
  const res = await setStatus(params);
  if (res.code != 200) {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: 'error'
    });
    return;
  }
  ElNotification({
    title: '提示',
    message: '修改状态成功',
    type: 'success'
  });
  await getList();
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
