<template>
	<div class="app-container">
		<!-- 搜索工作栏 -->
		<search-bar
			:formConfigs="formConfig"
			ref="searchBarForm"
			@headBtnClick="buttonClick" />
		<!-- 列表 -->
		<base-table
			:page="queryParams.pageNo"
			:limit="queryParams.pageSize"
			:table-props="tableProps"
			:table-data="list"
			:max-height="tableH">
			<method-btn
				v-if="tableBtn.length"
				slot="handleBtn"
				:width="120"
				label="操作"
				:method-list="tableBtn"
				@clickBtn="handleClick" />
		</base-table>
		<pagination
			:page.sync="queryParams.pageNo"
			:limit.sync="queryParams.pageSize"
			:total="total"
			@pagination="getList" />
		<!-- 新增 -->
		<base-dialog
			:dialogTitle="addOrEditTitle"
			:dialogVisible="centervisible"
			@cancel="handleCancel"
			@confirm="handleConfirm"
			:before-close="handleCancel"
			width="50%">
			<schedulingRuleConfigAdd ref="classList" @successSubmit="successSubmit" />
		</base-dialog>
	</div>
</template>

<script>
import {
	getGroupRulePage,
	deleteGroupRule,
	updateGroupRule,
	getGroupRule,
} from '@/api/base/groupSchedulingRule';
import schedulingRuleConfigAdd from './components/schedulingRuleConfigAdd.vue';
import { formatDate } from '@/utils';
import tableHeightMixin from '@/mixins/tableHeightMixin';
const tableProps = [
	{
		prop: 'enableTimeStr',
		label: '排班日期',
		minWidth: 200,
	},
	{
		prop: 'planName',
		label: '排班计划',
	},
	{
		prop: 'str',
		label: '排班规则',
		minWidth: 100,
	},
	{
		prop: 'enabled',
		label: '状态',
		filter: (val) => (val ? '正常' : '作废'),
	},
];
export default {
	name: 'schedulingRuleConfig',
	components: { schedulingRuleConfigAdd },
	mixins: [tableHeightMixin],
	data() {
		return {
			formConfig: [
				{
					type: 'input',
					label: '班次',
					placeholder: '班次',
					param: 'name',
				},
				{
					type: 'button',
					btnName: '查询',
					name: 'search',
					color: 'primary',
				},
				{
					type: 'separate',
				},
				{
					type: this.$auth.hasPermi('base:group-scheduling-rule:create')
						? 'button'
						: '',
					btnName: '新增',
					name: 'add',
					color: 'success',
					plain: true,
				},
			],
			tableProps,
			tableBtn: [
				this.$auth.hasPermi('base:group-scheduling-rule:cancel')
					? {
							type: 'cancel',
							btnName: '作废',
							showParam: {
								type: '&',
								data: [
									{
										type: 'unequal',
										name: 'enabled',
										value: 0,
									},
								],
							},
					  }
					: undefined,
				this.$auth.hasPermi('base:group-scheduling-rule:update')
					? {
							type: 'edit',
							btnName: '编辑',
							showParam: {
								type: '&',
								data: [
									{
										type: 'unequal',
										name: 'enabled',
										value: 0,
									},
								],
							},
					  }
					: undefined,
				this.$auth.hasPermi('base:group-scheduling-rule:delete')
					? {
							type: 'delete',
							btnName: '删除',
					  }
					: undefined,
			].filter((v) => v),
			// 总条数
			total: 0,
			// 班次基础信息列表
			list: [],
			// 弹出层标题
			addOrEditTitle: '',
			// 是否显示弹出层
			centervisible: false,
			// 查询参数
			queryParams: {
				pageNo: 1,
				pageSize: 20,
				name: null,
			},
		};
	},
	created() {
		this.getList();
	},
	methods: {
		buttonClick(val) {
			switch (val.btnName) {
				case 'search':
					this.queryParams.pageNo = 1;
					this.queryParams.classesName = val.name || undefined;
					this.getList();
					break;
				default:
					this.addOrEditTitle = '新增';
					this.centervisible = true;
					this.$nextTick(() => {
						this.$refs.classList.init();
					});
			}
		},
		/** 查询列表 */
		getList() {
			getGroupRulePage(this.queryParams).then((res) => {
				if (res.code === 0 && res.data.list && res.data.list.length > 0) {
					res.data.list.map((item) => {
						item.enableTimeStr =
							formatDate(item.startDay) +
							'至' +
							(item.endDay ? formatDate(item.endDay) : '永久');
						item.str = item.strList.join(',');
					});
					this.list = res.data.list;
					this.total = res.data.total;
				} else {
					this.list = [];
					this.total = 0;
				}
			});
		},
		handleClick(val) {
			switch (val.type) {
				case 'edit':
					this.addOrEditTitle = '编辑';
					this.$nextTick(() => {
						this.$refs.classList.init(val.data.id);
					});
					this.centervisible = true;
					break;
				case 'cancel':
					this.discard(val.data);
					break;
				default:
					this.handleDelete(val.data);
			}
		},
		handleCancel() {
			this.$refs.classList.formClear();
			this.centervisible = false;
			this.addOrEditTitle = '';
		},
		handleConfirm() {
			this.$refs.classList.submitForm();
		},
		successSubmit() {
			this.handleCancel();
			this.getList();
		},
		discard(row) {
      let _this = this
      _this.$modal
					.newConfirm(
						'是否确认作废序号为"' + row.planName + '"的数据项?'
					,'提示')
				.then(() => {
					getGroupRule(row.id).then((response) => {
						let datas = response.data;
						datas.list = datas.sequenceList;
						datas.enabled = 0;
						updateGroupRule(datas).then(() => {
							_this.getList();
							_this.$modal.msgSuccess('操作成功');
						});
					});
				})
				.catch(() => {});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			this.$modal
				.delConfirm(row.planName)
				.then(function () {
					return deleteGroupRule(row.id);
				})
				.then(() => {
					this.getList();
					this.$modal.msgSuccess('删除成功');
				})
				.catch(() => {});
		},
	},
};
</script>
