<template>
	<div class="app-container">
		<el-row :gutter="10" class="mb8">
			<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
				label-width="68px">
				<el-form-item label="" prop="keywords">
					<el-input v-model="queryParams.param.keywords" placeholder="支持姓名、全拼、简拼、手机号吗、证件号码" clearable
						@keyup.enter.native="handleQuery" clearable/>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
					<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
						重置
					</el-button>
				</el-form-item>
			</el-form>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>
		<el-tabs v-model="queryParams.param.status" @tab-click="handleClick">
			<el-tab-pane label="待随访" name="0"></el-tab-pane>
			<el-tab-pane label="已随访" name="1"></el-tab-pane>
			<el-tab-pane label="失访" name="2"></el-tab-pane>
		</el-tabs>
		<el-table v-loading="loading" :data="listDat" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="工单id" fixed show-overflow-tooltip align="center" prop="id" min-width="200" />
			<el-table-column fixed label="随访队列" show-overflow-tooltip align="center" prop="queueName" min-width="150" />
			<el-table-column fixed label="患者姓名" align="center" prop="name" min-width="100" />
			<el-table-column label="性别" align="center" prop="gender" show-overflow-tooltip min-width="60">
				<template slot-scope="scope">
					<span v-if="scope.row.gender == 0">男</span>
					<span v-if="scope.row.gender == 1">女</span>
				</template>
			</el-table-column>

			<el-table-column label="年龄" align="center" prop="age" show-overflow-tooltip min-width="60" />
			<el-table-column label="证件号码" align="center" prop="idCard" show-overflow-tooltip min-width="180" />
			<el-table-column label="手机号码" align="center" prop="phone" show-overflow-tooltip min-width="150" />
			<el-table-column label="随访序号" align="center" prop="phone" show-overflow-tooltip min-width="150" />
			<el-table-column label="开始时间" align="center" prop="startTime" show-overflow-tooltip min-width="150">
				<template slot-scope="scope">
					<span>{{
          parseTime(scope.row.startTime, "{y}-{m}-{d} {h}:{i}")
        }}</span>
				</template>
			</el-table-column>
			<el-table-column label="结束时间" align="center" prop="endTime" show-overflow-tooltip min-width="150">
				<template slot-scope="scope">
					<span>{{
          parseTime(scope.row.endTime, "{y}-{m}-{d} {h}:{i}")
        }}</span>
				</template>
			</el-table-column>
			<!--待随访： 随访状态（待随访/即将超期/超期） -->
			<!--已随访： 随访状态（已随访/超随访期） -->
			<!--待随访： 随访状态（待随访） -->
			<el-table-column label="随访状态" align="center" prop="phone" show-overflow-tooltip min-width="150">
				<template slot-scope="scope">
					{{ status[scope.row.status] }}
				</template>
			</el-table-column>
			<!-- <el-table-column
        label="失访状态"
        align="center"yuy
        prop="phone"
        show-overflow-tooltip
        width="150"
      /> -->
			<el-table-column v-if="queryParams.param.status == 2" label="失访原因" align="center" prop="reason" show-overflow-tooltip width="200" />
			<el-table-column fixed="right" label="操作" align="center" width="150">
				<template slot-scope="scope">
					<el-button :disabled="scope.row.status != 0" size="mini" type="text" icon="el-icon-s-check"
						@click="handleFollow(scope.row)">
						随访
					</el-button>
					<el-button :disabled="scope.row.status != 0" size="mini" type="text" icon="el-icon-s-release"
						@click="handleLossFollow(scope.row)">
						失访
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 失访 -->
		<el-dialog class="popup" title="失访" :visible.sync="open" width="780px" append-to-body>
			<el-form class="formStep" ref="form" :model="form" :rules="rules" label-width="90px">
				<el-form-item label="随访次数" prop="times">
					<el-input v-model="form.times" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="失访原因" prop="reason">
					<el-select v-model="form.reason" placeholder="请选择">
						<el-option-group v-for="group in options" :key="group.label" :label="group.label">
							<el-option v-for="item in group.options" :key="item.value" :label="item.label"
								:value="item.value">
							</el-option>
						</el-option-group>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="open = false">取 消</el-button>
			</div>
		</el-dialog>
		<!-- 随访 -->
		<el-dialog class="popup" title="随访" :visible.sync="open1" width="780px" append-to-body>
			<el-form class="formStep" ref="form1" :model="form" :rules="rules" label-width="90px">
				<el-form-item label="随访人" prop="followuper">
					<el-input v-model="form.followuper" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="随访时间" prop="followupTime">
					<el-date-picker format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
						v-model="form.followupTime" type="datetime" placeholder="选择日期">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="随访次数" prop="times">
					<el-input v-model="form.times" placeholder="请输入" />
				</el-form-item>
				<el-form-item label="随访内容" prop="followupText">
					<el-input v-model="form.followupText" placeholder="请输入" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm1">确 定</el-button>
				<el-button @click="open1 = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		queryTask,
		updStatus,
		followPatient
	} from "@/api/followupFile";
	export default {
		name: "Notice",
		data() {
			return {
				options: [{
						label: "患者原因",
						options: [{
								value: "患者病情加重或死亡，无法参与随访",
							},
							{
								value: "患者依从性差，不配合随访",
							},
							{
								value: "患者主动退出或不愿继续随访",
							},
							{
								value: "患者搬迁、工作变动、联系方式更换，无法联系到患者",
							},
							{
								value: "信息记录不准确，错误记录患者联系方式或地址",
							},
						],
					},
					{
						label: "医疗机构原因",
						options: [{
								value: "随访管理不足，未及时安排随访",
							},
							{
								value: "随访人员更换，管理混乱导致遗漏",
							},
							{
								value: "转诊或转院",
							},
							{
								value: "医疗资源受限",
							},
						],
					},
					{
						label: "不可抗力",
						options: [{
								value: "社会动荡或自然灾害",
							},
							{
								value: "疫情或公共卫生事件",
							},
						],
					},
				],
				activeName: "",
				status: {
					0: "待随访",
					1: "已随访",
					2: "失访",
					3: "即将超期",
					4: "超期未随访",
					5: "超期已随访",
				},
				idCardType: [{
						label: "身份证",
						value: 0,
					},
					{
						label: "护照或外国人永居证",
						value: 1,
					},
					{
						label: "港澳居民来往内地通行",
						value: 2,
					},
					{
						label: "台湾居民来往大陆通行证",
						value: 3,
					},
					{
						label: "其他",
						value: 4,
					},
				],
				idCardTypeValue: {
					0: "身份证",
					1: "护照或外国人永居证",
					2: "港澳居民来往内地通行",
					3: "台湾居民来往大陆通行证",
					4: "其他",
				},
				loading: false, // 遮罩层
				ids: [], // 选中数组
				single: true, // 非单个禁用
				multiple: true, // 非多个禁用
				showSearch: true, // 显示搜索条件
				total: 0, // 总条数
				listDat: [{}], // 公告表格数据
				title: "", // 弹出层标题
				open: false, // 是否显示弹出层
				open1: false, // 是否显示弹出层
				importOpen: false, // 导入弹窗
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					param: {
						status: "0",
						keywords:'',
					},
				},
				formDisabled: false,
				importform: {},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					followuper: [{
						required: true,
						message: "随访人不能为空",
						trigger: "blur"
					}, ],
					followupTime: [{
						required: true,
						message: "随访时间不能为空",
						trigger: "change"
					}, ],
					followupText: [{
						required: true,
						message: "随访内容不能为空",
						trigger: "blur"
					}, ],
					times: [{
						required: true,
						message: "随访次数不能为空",
						trigger: "blur"
					}, ],

					reason: [{
						required: true,
						message: "失访原因不能为空",
						trigger: "change"
					}, ],
				},
			};
		},
		created() {
			let idCard = this.$route.query.idCard
			this.queryParams.param.keywords = idCard || ''
			this.getList();
		},
		methods: {
			handleClick(tab, event) {
				this.getList();
			},
			/** 查询公告列表 */
			getList() {
				this.loading = true;
				queryTask(this.queryParams).then((res) => {
					this.listDat = res.data.list;
					this.total = res.data.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.queryParams.param.keywords = ''
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map((item) => item.id);
				this.single = selection.length != 1;
				this.multiple = !selection.length;
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
			},
			/** 随访 */
			handleFollow(row) {
				this.open1 = true;
				this.form = {
					id: row.id,
					followuper: "",
					followupTime: "",
					times: "",
					followupText: "",
				};
			},
			/** 失访 */
			handleLossFollow(row) {
				this.open = true;
				this.form = JSON.parse(JSON.stringify(row));
				this.form.status = 2;
			},
			/** 提交按钮 */
			submitForm: function() {
				this.$refs["form"].validate((valid) => {
					if (valid) {
						if (this.form.id != undefined) {
							patientUpd(this.form).then((response) => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							patientAdd(this.form).then((response) => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 失访 */
			submitForm: function() {
				this.$refs["form"].validate((valid) => {
					if (valid) {
						updStatus(this.form).then((response) => {
							this.$modal.msgSuccess("操作成功");
							this.open = false;
							this.getList();
						});
					}
				});
			},
			/** 随访 */
			submitForm1: function() {
				this.$refs["form1"].validate((valid) => {
					if (valid) {
						followPatient(this.form).then((response) => {
							this.$modal.msgSuccess("操作成功");
							this.open1 = false;
							this.getList();
						});
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const idList = row.id ? [row.id] : this.ids;
				this.$modal
					.confirm("是否确认删除当前选择的数据？")
					.then(function() {
						return patientDel({
							idList: idList
						});
					})
					.then(() => {
						this.getList();
						this.$modal.msgSuccess("删除成功");
					})
					.catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download(
					"system/user/export", {
						...this.queryParams.params,
					},
					`患者档案.xlsx`
				);
			},
			/** 导入按钮操作 */
			handleImport() {
				this.upload.title = "用户导入";
				this.upload.open = true;
			},
		},
	};
</script>
<style scoped src="@/assets/styles/common.css"></style>
<style scoped>
	.form-item-age {
		display: flex;
		align-items: center;
	}

	.form-item-age span {
		margin: 0 10px;
	}

	.form-item-age>>>.el-input {
		width: 100px;
	}
</style>
<!-- >>> .el-input__inner {
  padding: 0 15px !important;
} -->