<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="100px">
			<el-form-item label="期次" prop="qi">
				<el-cascader v-model="queryParams.qi" placeholder="请选择期次" :props="prop" :options="options" @change="handleChange">
					<template slot-scope="{ node, data }">
					    <span v-if="data.children != null">{{ data.year+'年' }}</span>
					    <span v-else> {{ '第 '+data.year+' 期' }} </span>
					</template>
				</el-cascader>
			</el-form-item>
			<el-form-item label="执业单位" prop="DeptName">
				<el-input v-model="queryParams.DeptName" placeholder="请输入身份证" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="姓名" prop="UserName">
				<el-input v-model="queryParams.UserName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="身份证号" prop="UserCord">
				<el-input v-model="queryParams.UserCord" placeholder="请输入身份证号" clearable @keyup.enter.native="handleQuery" />
			</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>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['jxjy:Tuifeisq:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['jxjy:Tuifeisq:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['jxjy:Tuifeisq:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['jxjy:Tuifeisq:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="TuifeisqList" @selection-change="handleSelectionChange">
			<el-table-column type="index" label="序号" width="55" align="center" />
			<el-table-column label="期次" align="center" prop="id" >
				<template slot-scope="scope">
					{{scope.row.qiciYear+'年第'+scope.row.qiCi+'期'}}
				</template>
			</el-table-column>
			<el-table-column label="姓名" align="center" prop="userName" />
			<el-table-column label="性别" align="center" prop="userSex" />
			<el-table-column label="身份证号" align="center" prop="userCord" width="170" />
			<el-table-column label="证书编号" align="center" prop="zgzsh" width="120" />
			<el-table-column label="执业单位" align="center" prop="deptName" width="160" />
			<el-table-column label="报名时间" align="center" prop="deptName" width="160" >
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.addDate) }}</span>
				</template> 
			</el-table-column>
			<el-table-column label="订单号" align="center" prop="transactionId" width="140" />
			<el-table-column label="交费交易号" align="center" prop="znum" width="140" />
			<el-table-column label="交费金额" align="center" prop="flee" />
			<el-table-column label="申请退款时间" align="center" prop="addtate" width="160">
				<template slot-scope="scope">
					<span>{{ scope.row.createTime }}</span>
				</template>
			</el-table-column>
			<el-table-column label="退费原因" align="center" prop="yy" > 
				<template slot-scope="scope">
					<!-- <span>{{ scope.row.createTime }}</span> -->
					<el-popover
					    placement="top-start"
					    title="退费原因"
					    width="200"
					    trigger="hover"
					    :content="scope.row.yy">
					    <el-button type="text" slot="reference">查看</el-button>
					  </el-popover>
				</template>
			</el-table-column>
			<!-- <el-table-column label="审核状态" align="center" prop="shflg">
				<template slot-scope="scope">
					<span></span>
				</template>
			</el-table-column>
			<el-table-column label="审核日期" align="center" prop="shdate" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.shdate, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column> -->
			<el-table-column label="退费状态" align="center" prop="tfflg" >
				<template slot-scope="scope">
					<span v-if="scope.row.shflg == 1" style="color:#67C23A ;">已退费</span>
					<span v-else-if="scope.row.shflg == 0" style="color: #E6A23C;">未退费</span>
					<el-popover
					v-else
					    placement="top-start"
					    title="原因"
					    width="200"
					    trigger="hover"
					    :content="scope.row.shyy">  
						<el-button type="text" style="color:#F56C6C ;" slot="reference">不同意退费</el-button>
					</el-popover>
				</template>
			</el-table-column>
			<el-table-column label="退费时间" align="center" prop="tfdate" width="180">
			</el-table-column>
			<el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" v-if="scope.row.shflg==0" type="text" icon="el-icon-refresh-left"  @click="handleUpdate(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" />

	</div>
</template>

<script>
	import {
		listTuifeisq,
		getTuifeisq,
		delTuifeisq,
		addTuifeisq,
		updateTuifeisq,
		xiehuiTuifei
	} from "@/api/xiehui/jxjy/Tuifeisq";
	import { searchQici } from "@/api/xiehui/jxjy/Training"
	export default {
		name: "Tuifeisq",
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 退费申请表格数据
				TuifeisqList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					UserName: null,
					UserCord: null,
					QiciYear: null,
					QiCi: null,
					DeptName: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				options:[],
				prop:{
					label:"year",
					value:"year"
				}
			};
		},
		created() {
			this.qiciSearch()
			this.getList();
		},
		methods: {
			qiciSearch(){
				searchQici().then(res=>{
					this.options = res.data
				})
			},
			//选择期次
			handleChange(e){
				console.log(e)
				this.queryParams.QiciYear = e[0]
				this.queryParams.QiCi = e[1]
			},
			/** 查询退费申请列表 */
			getList() {
				this.loading = true;
				listTuifeisq(this.queryParams).then(response => {
					this.TuifeisqList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					pid: null,
					yy: null,
					addtate: null,
					shflg: null,
					shdate: null,
					shyy: null,
					tfflg: null,
					tfdate: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.queryParams.QiCi = null
				this.queryParams.QiciYear = null
				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;
				this.title = "添加退费申请";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				// this.reset();
				// const id = row.id || this.ids
				// getTuifeisq(id).then(response => {
				// 	this.form = response.data;
				// 	this.open = true;
				// 	this.title = "修改退费申请";
				// });
				const h = this.$createElement;
				this.$prompt(`是否同意${row.userName}的退费申请？`,'提示',{
				 //  title: '提示',
				 //  message: h('p', null, [
					// h('span', null, `是否同意${row.userName}的退费申请？`), 
				 //  ]),
				  showInput:true,
				  inputPlaceholder:"不同意退费原因",
				  inputPattern: /\S/,
				  inputErrorMessage: '请输入不同意退费原因',
				  showCancelButton: true,
				  confirmButtonText: '不同意',
				  cancelButtonText: '同意', 
				  distinguishCancelAndClose:true,
				  confirmButtonClass:"el-button--danger",
				  cancelButtonClass:"el-button--primary",
				  beforeClose: (action, instance, done) => { 
					  console.log(instance)
					if (action === 'confirm') {
					  instance.confirmButtonLoading = true;
					  instance.showCancelButton = false;  
					  xiehuiTuifei({pid:row.pid,shflg:2,id:row.id,shyy:instance.inputValue}).then(res=>{
					  	this.$message({
					  		  type:"success",
					  		  message:"提交成功"
					  	  })
					  	  this.getList()
					  	  done();
					  	  setTimeout(() => {
					  		instance.cancelButtonLoading = false;
					  		instance.showConfirmButton = true;
					  	  }, 300)
					  }).catch(error=>{ 
					    done();
					    setTimeout(() => {
					  	instance.cancelButtonLoading = false;
					  	instance.showConfirmButton = true;
					    }, 300);
					  }) 
					   // done();
					   // setTimeout(() => {
					   //   instance.confirmButtonLoading = false;
					   //   instance.showCancelButton = true;
					   // }, 300)
					} else if(action == 'cancel') {
						instance.cancelButtonLoading = true;
						instance.showConfirmButton = false; 
						xiehuiTuifei({pid:row.pid,shflg:1,id:row.id}).then(res=>{
						  this.$message({
							  type:"success",
							  message:"退费成功"
						  })
						  this.getList()
						  done();
						  setTimeout(() => {
							instance.confirmButtonLoading = false;
							instance.showCancelButton = true;
						  }, 300)
						}).catch(error=>{ 
						  done();
						  setTimeout(() => {
							instance.confirmButtonLoading = false;
							instance.showCancelButton = true;
						  }, 300);
						}) 
						 // done();
						 // setTimeout(() => {
							// instance.cancelButtonLoading = false;
							// instance.showConfirmButton = true;
						 // }, 300)
					}else{
						if(instance.cancelButtonLoading != true && instance.confirmButtonLoading != true){
							done();
						}
					}
				  }
				}).then(action => {
				  
				})
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateTuifeisq(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addTuifeisq(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除退费申请编号为"' + ids + '"的数据项？').then(function() {
					return delTuifeisq(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('jxjy/Tuifeisq/export', {
					...this.queryParams
				}, `Tuifeisq_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
<style scoped lang="scss">
	
</style>