<script lang="ts" setup>
/**
 * @Name: index.vue
 * @Description: 用户之声-首页界面
 * @Author: xinyan
 */

import VerticalDivider from '/src/components/VerticalDivider/index.vue';
import { RefreshLeft, Search } from '@element-plus/icons-vue';
import { useTemplateRef } from 'vue';
import { useTableHeight } from '/@/utils/useTableHeight';
import DataTable from '/@/views/customers-voice/components/DataTable.vue';

const titleContainer: Ref<HTMLElement | null> = useTemplateRef('titleContainer');
const queryContainer: Ref<HTMLElement | null> = useTemplateRef('queryContainer');
const { tableHeight } = useTableHeight(titleContainer, queryContainer);

const tableRef: Ref<any> = useTemplateRef('table');

const btnLoading = ref(false);
const resetLoading = ref(false);

const formInline = reactive<any>({
	asin: '',
	date: [new Date(new Date().setDate(new Date().getDate() - 30)), new Date()],
});
provide('query-parameter', formInline);

async function handleQuery() {
	btnLoading.value = true;
	await tableRef.value?.fetchList(true);
	btnLoading.value = false;
}

async function resetParameter() {
	formInline.asin = '';
	formInline.date = [new Date(new Date().setDate(new Date().getDate() - 30)), new Date()];
	resetLoading.value = true;
	await tableRef.value?.fetchList(true);
	resetLoading.value = false;
}

const disabledDate = (time: Date) => {
	return time.getTime() > Date.now();
};
</script>

<template>
	<div class="p-5">
		<el-card class="h-full" style="color: rgba(0, 0, 0, 0.88)">
			<div ref="titleContainer" class="text-xl font-semibold pb-5">买家之声</div>
			<!-- 查询条件 -->
			<div ref="queryContainer" class="flex justify-between">
				<div class="flex flex-1">
					<div class="w-full whitespace-nowrap">
						<el-row :gutter="20" style="margin-bottom: 5px">
							<el-col :span="5">
								<div class="flex items-center">
									<el-input v-model="formInline.asin" clearable placeholder="ASIN/SKU">
										<template #prepend>
											<el-icon>
												<Search />
											</el-icon>
										</template>
									</el-input>
								</div>
							</el-col>
							<el-col :span="6">
								<div class="flex items-center">
									<span class="mr-2">日期</span>
									<el-date-picker
										v-model="formInline.date"
										:disabled-date="disabledDate"
										end-placeholder="结束日期"
										range-separator="至"
										start-placeholder="开始日期"
										type="daterange"
										:clearable="false"
									/>
								</div>
							</el-col>
						</el-row>
					</div>
				</div>
				<VerticalDivider />
				<div class="flex gap-1.5 ml-5">
					<el-button :icon="Search" :loading="btnLoading" type="primary" @click="handleQuery"> 查 询</el-button>
					<el-button :icon="RefreshLeft" :loading="resetLoading" color="#ECECF1C9" style="width: 88px; color: #3c3c3c" @click="resetParameter">
						重 置
					</el-button>
				</div>
			</div>
			<el-divider ref="dividerContainer" style="margin: 20px 0 12px 0" />
			<div :style="{ height: tableHeight + 'px' }">
				<DataTable ref="table" />
			</div>
		</el-card>
	</div>
</template>

<style scoped></style>
