<template>
	<div>
		<div class="head">
			<el-row>
				<el-form-item>
					<el-input v-model="searchInfo.name" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" style="margin-left: 10px;" @click="GetList()">搜索</el-button>
					<el-button type="success" style="margin-left: 10px;" @click="Add()">添加</el-button>
				</el-form-item>
			</el-row>
		</div>
		<el-table :data="tableData" style="width: 100%;" row-key="id" v-loading="loading">
			<el-table-column prop="name" label="名称" />
			<el-table-column prop="type" label="类型">
				<template #default="scope">
					 <el-tag v-if="scope.row.type==1" class="ml-2"  >医院</el-tag>
					 <el-tag v-if="scope.row.type==2"  class="ml-2" type="info">企业</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="status" label="状态">
				<template #default="scope">
					 <el-tag v-if="scope.row.status==1" class="ml-2" type="success">正常</el-tag>
					 <el-tag v-if="scope.row.status==0"  class="ml-2" type="danger">停用</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="url" label="Url" />
			<el-table-column label="操作" width="100">
				<template #default="scope">
					<el-button type="primary" @click="EditFunc(scope.row)" :icon="Edit" circle />
					<el-button type="danger" @click="Del(scope.row)" :icon="Delete" circle />
				</template>
			</el-table-column>
		</el-table>

		<div class="page">
			<el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize"
				:page-sizes="[15, 50, 100, 200]" layout="total,sizes, prev, pager, next" :total="total"
				@size-change="PageSizeChange" @current-change="PageCurrentChange" />
		</div>
		<el-dialog v-model="dialogVisible" title="机构信息" width="60%">
			<el-form :model="Info" label-width="100px" v-loading="loading">

				<el-form-item label="名称">
					<el-input v-model="Info.name"  style="width: 300px;"/>
				</el-form-item>
				<el-form-item label="图片">
					<el-upload class="avatar-uploader" :action="upfileurl" :headers="headerObj" :show-file-list="false"
						:on-success="upLoadSuccess">
						<img v-if="logo" :src="logo" class="avatar" style="width: 60px;" />
						<el-icon v-else class="avatar-uploader-icon">
							<Plus />
						</el-icon>
					</el-upload>
				</el-form-item>
				<el-form-item label="Url">
					<el-input v-model="Info.url" />
				</el-form-item>
				<el-form-item label="类型">
					<el-select v-model="Info.type" placeholder="选择类型" size="large" style="width: 240px">
						<el-option label="医院" :value="1" />
						<el-option label="企业" :value="2" />
					</el-select>
				</el-form-item>
				<el-form-item label="状态">
					<el-switch v-model="Info.status" size="large" active-text="正常" inactive-text="停用" :active-value="1"
						:inactive-value="0" />
				</el-form-item>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="dialogVisible = false">取消</el-button>
					<el-button type="primary" @click="Save">
						确定
					</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup>
	import {
		onBeforeUnmount,
		ref,
		shallowRef,
		onMounted
	} from 'vue'
	import {
		Editor,
		Toolbar
	} from '@wangeditor/editor-for-vue'
	import {
		ElMessage
	} from 'element-plus'
	import {
		UpFileUrl,
		LinkUnitsGetList,
		LinkUnitsSave,
		LinkUnitsDel
	} from '@/api/api.js'
	import {
		Edit,
		Delete
	} from '@element-plus/icons-vue'
	let loading = ref(false)
	let tableData = ref([])
	let currentPage = ref(1) //当前页码
	let pageSize = ref(15) //每页数量
	let total = 0 //总数量
	let dialogVisible = ref(false)
	const PageSizeChange = (e) => { // 修改每页数量
		pageSize.value = e
		GetList()
	}
	const PageCurrentChange = (e) => { //切换页码
		currentPage.value = e
		GetList()
	}
	let upfileurl = UpFileUrl()
	let headerObj = {
		Authorization: 'Bearer ' + sessionStorage.getItem("token")
	}
	let logo = ref('');
	//上传成功钩子
	let BaseUrl = ref(import.meta.env.VITE_APP_FILE)
	const upLoadSuccess = (res) => {
		logo.value = BaseUrl.value + res.data
		Info.value.logo = res.data
	}
	let searchInfo = ref({});
	let Info = ref({
		id: 0
	});
	const GetList = () => {
		loading.value = true
		LinkUnitsGetList({
			page: currentPage.value,
			pageSize: pageSize.value,
			searchInfo: searchInfo.value
		}).then(res => {
			loading.value = false
			if (res.status) {
				tableData.value = res.data.list
				total = res.data.count
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	const Add = () => {
		Info.value = {}
		dialogVisible.value = true
		Info.value.status=1
		Info.value.id = 0
		logo.value=''
	}
	const Save = () => {
		loading.value = true
		LinkUnitsSave({
			Info: Info.value
		}).then(res => {
			loading.value = false
			if (res.status) {
				dialogVisible.value = false
				GetList()
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	const Del = (row) => {
		loading.value = true
		LinkUnitsDel({
			id: row.id
		}).then(res => {
			loading.value = false
			if (res.status) {
				GetList()
			} else {
				ElMessage.error(res.msg)
			}
		})
	}
	const EditFunc = (row) => {
		dialogVisible.value = true
		Info.value = row
		logo.value=BaseUrl.value+row.img
	}
	onMounted(() => {
		GetList()
	})
</script>

<style scoped>
	 
</style>