<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="120px">
            <el-form-item label="信用分奖惩名称" prop="ruleName">
              <el-input v-model="queryParams.name" placeholder="请输入信用分奖惩名称" clearable @keyup.enter="handleQuery" />
            </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]">
          <el-table v-loading="loading" :data="extractLogList">
            <el-table-column
              :label="label"
              align="center"
              prop="expertName"
              min-width="100px"
              v-for="{ label, prop } in headers"
              :key="prop"
            >
              <template #default="{ row }">
                <template v-if="prop === 'rewardName'">
                  <span>{{ row[prop] || '-' }}</span>
                </template>
                <template v-else-if="prop === 'rewardType'">
                  <dict-tag :options="reward_type" :value="row[prop]" />
                </template>
                <template v-else-if="prop === 'rewardContent'">
                  <span>
                    {{
                      (credit_title_type.credit_title_type
                      && Array.isArray(credit_title_type.credit_title_type.value)
                        ? credit_title_type.credit_title_type.value.find(item => item.label === row.rewardName)?.remark
                        : null) || row.rewardContent || '-'
                    }}
                </span>

                </template>
                <template v-else>
                  <span>{{ row[prop] || '-' }}</span>
                </template>
              </template>
<!--              <template #default="{ row }">
                <span v-if="prop == 'rewardUntil'">永久</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 } from '@/api/credit/rule';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { reward_type } = toRefs<any>(proxy?.useDict('reward_type'));
const credit_title_type = toRefs<any>(proxy?.useDict('credit_title_type'));


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: 'rewardName'
  },
  {
    label: '奖惩类型',
    prop: 'rewardType'
  },
  {
    label: '奖惩内容',
    prop: 'rewardContent'
  },
  {
    label: '奖惩截止时间',
    prop: 'rewardUntil'
  }
]);
const page = ref({
  pageNum: 1,
  pageSize: 10,
  total: 0
});
const queryParams = ref<any>({
  name: ''
});
/** 查询expert列表 */
const getList = async () => {
  loading.value = true;
  const res = await getTableList({ ...queryParams.value, ...page.value });
  extractLogList.value = res;
  page.value.total = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
onActivated(() => {
  getList();
});
onMounted(() => {
  getList();
});
</script>
