<script setup>
import PopupLayout from "@/layouts/PopupLayout.vue";
import ctxRef from "@/utils/ctx_ref.js"
import Context from "./addGroupPopup.js"
import PostForm from "@/components/form/PostForm.vue";
import TDivider from "@/components/ui/TDivider.vue";
import TButton from "@/components/ui/TButton.vue";
import TSelect from "@/components/ui/TSelect.vue";
import TOption from "@/components/ui/TOption.vue";
import MoreOptionsIndicator from "@/components/common/MoreOptionsIndicator.vue";
import TTextarea from "@/components/ui/TTextarea.vue";
import TCheckboxBinary from "@/components/ui/TCheckboxBinary.vue";
import SubmitButton from "@/components/form/SubmitButton.vue";
import TLabel from "@/components/ui/TLabel.vue";
import PlusButton from "@/components/common/PlusButton.vue";
import DefinitionTable from "@/components/common/DefinitionTable.vue";

const ctx = ctxRef(new Context())

</script>

<template>
	<PopupLayout>

		<header v-if="!ctx.isUpdating">添加条件分组</header>
		<header v-if="ctx.isUpdating">修改条件分组</header>
		<PostForm @success="ctx.success" action="$">
			<input type="hidden" name="condGroupJSON" :value="JSON.stringify(ctx.group)"/>
			<DefinitionTable>
				<tbody>
				<tr>
					<td class="title">子条件列表</td>
					<td style="word-break: break-all">
						<div class="label-group" v-if="ctx.group.conds.length > 0">
							<TLabel outlined="" v-for="(cond, index) in ctx.group.conds">
						<span>
							<var v-if="cond.type.length == 0 || cond.type == 'params'" style="font-style: normal">{{ cond.param }} <var>{{ cond.operator }}</var></var>
							<var v-if="cond.type.length > 0 && cond.type != 'params'" style="font-style: normal">{{ ctx.typeName(cond) }}: </var>
							{{ cond.value }}
                            <sup v-if="cond.isCaseInsensitive" title="不区分大小写"><i class="icon info small"></i></sup> &nbsp;
							<a href="" title="修改" @click.prevent="ctx.updateCond(index, cond)"><i class="pi pi-pencil small"></i></a> <a href="" title="删除" @click.prevent="ctx.removeCond(index)"><i class="pi pi-remove"></i></a> </span>
								<var v-if="ctx.index < ctx.group.conds.length - 1"> {{ ctx.group.connector }} &nbsp;</var>
							</TLabel>
							<TDivider></TDivider>
						</div>
						<PlusButton type="button" @click.prevent="ctx.addCond()">+</PlusButton>
					</td>
				</tr>
				<tr>
					<td>子条件之间关系</td>
					<td>
						<TSelect v-model="ctx.group.connector">
							<TOption value="and">和</TOption>
							<TOption value="or">或</TOption>
						</TSelect>
						<p class="comment" v-if="ctx.group.connector == 'and'">必须满足所有条件才能成立。</p>
						<p class="comment" v-if="ctx.group.connector == 'or'">只要满足其中一个条件即可成立。</p>
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<MoreOptionsIndicator></MoreOptionsIndicator>
					</td>
				</tr>
				</tbody>
				<tbody v-show="ctx.moreOptionsVisible">
				<tr>
					<td>补充说明</td>
					<td>
						<TTextarea rows="3" v-model="ctx.group.description" maxlength="100" class="w-full"></TTextarea>
					</td>
				</tr>
				<tr>
					<td>启用当前分组</td>
					<td>
						<TCheckboxBinary v-model="ctx.group.isOn"/>
					</td>
				</tr>
				</tbody>
			</DefinitionTable>
			<SubmitButton>确定</SubmitButton>
		</PostForm>
	</PopupLayout>
</template>

<style lang="postcss" scoped>
</style>