<template>
    <div class="system-dept-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-dept-search mb15">
                <!-- 发布者：<el-input size="default" placeholder="请输入发布用户名称" v-model="state.tableData.param.userName" 
				class="ml10 mr10" style="max-width: 180px" clearable></el-input> -->
                审核状态：<el-select size="default" v-model="state.tableData.param.reviewStatus" placeholder="请选择审核状态" 
                clearable class="ml10 mr10" style="max-width: 180px">
					<el-option v-for="(item,index) in statusList" :key="index" :label="item.label" :value="item.value"></el-option>
                </el-select>
                
				<el-button @click="getTableData" size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
                <el-button @click="reset" size="default" type="primary" class="ml10">
					<el-icon>
						<ele-RefreshRight />
					</el-icon>
					重置
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column align="center" type="index" label="序号"  width="100"/>
				<el-table-column align="center" prop="name" label="作品">
					<template #default="scope">
						<el-image :src="scope.row.path" lazy
						preview-teleported="true" :preview-src-list="[scope.row.path]"/>
					</template>
				</el-table-column>
				<!-- <el-table-column prop="phone" label="发布者" show-overflow-tooltip></el-table-column> -->
				<!-- <el-table-column prop="type" label="类型" show-overflow-tooltip></el-table-column> -->
				<el-table-column prop="reviewStatus" align="center" label="审核结果" show-overflow-tooltip>
					<template #default="scope">
						<el-tag v-if="scope.row.reviewStatus === 1" type="warning">审核中</el-tag>
						<el-tag v-else-if="scope.row.reviewStatus === 2" type="success">通过</el-tag>
						<el-tag v-else-if="scope.row.reviewStatus === 3" type="danger">未通过</el-tag>
						<el-tag v-else>未发布</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createtime" align="center" label="时间" :formatter="dateFormatter" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="200" align="center">
					<template #default="scope">
						<!-- <el-button size="small" text type="primary" @click="toShowDetail(scope.row.id)">查看详情</el-button> -->
						<el-button size="small" v-if="scope.row.reviewStatus === 1" text type="primary" @click="audit(scope.row)">审核</el-button>
						<el-button size="small" v-else-if="scope.row.reviewStatus === null" text type="primary" @click="toPublic(scope.row)">发布</el-button>
						<el-button size="small" v-else text type="primary" @click="audit(scope.row)">重新审核</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.current"
				background
				v-model:page-size="state.tableData.param.size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<!-- <MessageDialog ref="messageDialogRef" /> -->
		<!-- 回复弹窗 -->
		<el-dialog style="height: 200px;" :title="state.auditDialog.title" v-model="state.auditDialog.show" width="400px">
			<div class="dialog-content">
				<el-form ref="replayFormRef" v-loading="state.auditDialog.loading" label-width="80px" :model="state.auditDialog.form">
					<el-radio-group class="dialog-radio" v-model="state.auditDialog.form.status">
						<el-radio :value="2">通过</el-radio>
						<el-radio :value="3">不通过</el-radio>
					</el-radio-group>
					<div class="dialog-footer">
						<el-button @click="state.auditDialog.show = false">取消</el-button>
						<el-button type="primary" @click="commitAudit">确定</el-button>
					</div>
				</el-form>
			</div>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="shares">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { sharesApi } from '/@/api/service/shares';
import { ElMessage, ElMessageBox, TableColumnCtx } from 'element-plus';

// 引入组件
// const MessageDialog = defineAsyncComponent(() => import('/@/views/message/dialog.vue'));
// const messageDialogRef = ref();

// 表格数据
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			current: 1,
			size: 10,
			reviewStatus: ''
		},
	},
	auditDialog: {
		loading: false,
		form: {
			id: '',
			status: 2
		},
		show: false,
		title: '作品审核',
		message: '',
	}
});

// 状态列表
const statusList = ref([
    // {label: '未发布', value: ''},
	{label: '审核中', value: 1},
    {label: '通过', value: 2},
    {label: '未通过', value: 3},
]);

// 引入 api 请求接口
const shareApi = sharesApi();

// 获取表格数据
const getTableData = async() => {
	try {
		state.tableData.loading = true;
		let res = await shareApi.getReleaseList(state.tableData.param);
		if(res?.success){
			state.tableData.data = res.data.records;
			state.tableData.total = res.data.total;
		}else{
			ElMessage.error('发布作品列表获取失败！');
		}
	} catch (error) {
	} finally {
		state.tableData.loading = false;
	}
};

// 重置
const reset = () =>{
	state.tableData.param = {
		current: 1,
		size: 10,
		reviewStatus: ''
	}
	getTableData();
}

// 文章详情
// const toShowDetail = (id: any) => {
//     messageDialogRef.value.openDialog(id);
// }

const replayFormRef = ref();

// 发布
const toPublic = (row: any) => {
	ElMessageBox.confirm('确定要发布该作品吗?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
	.then(async() => {
		try {
			state.tableData.loading = true;
			let res = await shareApi.saveRelease({ creationId: row.creationId });
			if(res?.success){ 
				await getTableData();
				ElMessage.success('发布成功！');
			} else ElMessage.error('发布失败！');
		} catch(e) {
			ElMessage.error('处理失败！');
		} finally {
			state.tableData.loading = false;
		}
	})
}

// 审核
const audit = (row: any) => {
	state.auditDialog.form.id = row.id;
	console.log(row.id);
	
	state.auditDialog.form.status = 2;
	state.auditDialog.show = true;
}

// 提交审核
const commitAudit = async() => {
	try {
		state.auditDialog.loading = true;
		let res = await shareApi.auditRelease(state.auditDialog.form);
		if(res?.success){
			ElMessage.success('审核成功！');
			state.auditDialog.show = false;
			getTableData();
		}else{ElMessage.error('审核失败！');}
	} catch (error) {
		ElMessage.error('审核失败！');
	}finally{
		state.auditDialog.loading = false;
	}
}

// 日期格式化
const dateFormatter = (row: any, column: TableColumnCtx<String>) => {
    if(row.createtime === null) return '暂无';
	let date = new Date(row.createtime);
  	return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.size = val;
	getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.current = val;
	getTableData();
};

onMounted(() => {
	getTableData();
})

</script>

<style scoped lang="scss">
.system-dept-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
.dialog-content{
	padding: 0;
	display: flex;
	flex-direction: column;
}
.dialog-radio{
	display: flex;
	justify-content: center;
}
.dialog-footer{
	margin-top: 20px;
	display: flex;
	justify-content: center;
}
::v-deep .el-image {
  width: 250px; // 固定图片宽度
//   height: 150px; // 固定图片高度
  object-fit: cover; // 图片居中剪裁
//   display: block;
}
</style>