<template>
	<div class="luckey">
		<Card>
			<Row class="region-bar">
				<span class="region-tip">当前校区：<b>{{ regionId || '未识别（请确认账号已绑定校区）' }}</b></span>
				<Button type="primary" icon="ios-refresh" @click="reloadActive" :disabled="!regionId">刷新</Button>
			</Row>

			<Tabs v-model="activeTab" @on-click="onTabChange">
				<!-- 奖池 -->
				<TabPane label="奖池/开奖" name="pool">
					<Alert show-icon>
						现金奖池采用“主动投入”规则：用户投入星球券后即从余额扣除并回收，本期按投入券数加权开奖，投入越多中奖概率越高。
					</Alert>
					<Button type="primary" icon="md-add" @click="openPool()" style="margin-bottom:12px">新建奖池</Button>
					<Table border :loading="pool.loading" :columns="poolColumns" :data="pool.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="pool.pageNumber" :total="pool.total" :page-size="pool.pageSize"
							@on-change="p => changePage('pool', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- 任务 -->
				<TabPane label="券任务规则" name="task">
					<Button type="primary" icon="md-add" @click="openTask()" style="margin-bottom:12px">新增任务</Button>
					<Table border :loading="task.loading" :columns="taskColumns" :data="task.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="task.pageNumber" :total="task.total" :page-size="task.pageSize"
							@on-change="p => changePage('task', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- BUFF -->
				<TabPane label="BUFF配置" name="buff">
					<Button type="primary" icon="md-add" @click="openBuff()" style="margin-bottom:12px">新增BUFF</Button>
					<Table border :loading="buff.loading" :columns="buffColumns" :data="buff.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="buff.pageNumber" :total="buff.total" :page-size="buff.pageSize"
							@on-change="p => changePage('buff', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- 经营系统 -->
				<TabPane label="券树配置" name="tree">
					<Button type="primary" icon="md-add" @click="openOperate('tree')" style="margin-bottom:12px">新增券树周期</Button>
					<Table border :loading="tree.loading" :columns="treeColumns" :data="tree.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="tree.pageNumber" :total="tree.total" :page-size="tree.pageSize"
							@on-change="p => changePage('tree', p)" size="small" show-total></Page>
					</Row>
				</TabPane>
				<TabPane label="仓库等级" name="warehouse">
					<Button type="primary" icon="md-add" @click="openOperate('warehouse')" style="margin-bottom:12px">新增仓库等级</Button>
					<Table border :loading="warehouse.loading" :columns="warehouseColumns" :data="warehouse.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="warehouse.pageNumber" :total="warehouse.total" :page-size="warehouse.pageSize"
							@on-change="p => changePage('warehouse', p)" size="small" show-total></Page>
					</Row>
				</TabPane>
				<TabPane label="防御塔" name="tower">
					<Button type="primary" icon="md-add" @click="openOperate('tower')" style="margin-bottom:12px">新增防御塔等级</Button>
					<Table border :loading="tower.loading" :columns="towerColumns" :data="tower.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="tower.pageNumber" :total="tower.total" :page-size="tower.pageSize"
							@on-change="p => changePage('tower', p)" size="small" show-total></Page>
					</Row>
				</TabPane>
				<TabPane label="搜查规则" name="search">
					<Button type="primary" icon="md-add" @click="openOperate('search')" style="margin-bottom:12px">新增搜查规则</Button>
					<Table border :loading="search.loading" :columns="searchColumns" :data="search.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="search.pageNumber" :total="search.total" :page-size="search.pageSize"
							@on-change="p => changePage('search', p)" size="small" show-total></Page>
					</Row>
				</TabPane>
				<TabPane label="体力道具" name="stamina">
					<Button type="primary" icon="md-add" @click="openOperate('stamina')" style="margin-bottom:12px">新增体力道具</Button>
					<Table border :loading="stamina.loading" :columns="staminaColumns" :data="stamina.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="stamina.pageNumber" :total="stamina.total" :page-size="stamina.pageSize"
							@on-change="p => changePage('stamina', p)" size="small" show-total></Page>
					</Row>
				</TabPane>
				<TabPane label="校园地标" name="landmark">
					<Button type="primary" icon="md-add" @click="openOperate('landmark')" style="margin-bottom:12px">新增校园地标</Button>
					<Table border :loading="landmark.loading" :columns="landmarkColumns" :data="landmark.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="landmark.pageNumber" :total="landmark.total" :page-size="landmark.pageSize"
							@on-change="p => changePage('landmark', p)" size="small" show-total></Page>
					</Row>
				</TabPane>
				<TabPane label="经济审计" name="economy">
					<Table border :loading="economy.loading" :columns="economyColumns" :data="economy.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="economy.pageNumber" :total="economy.total" :page-size="economy.pageSize"
							@on-change="p => changePage('economy', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- 快讯 -->
				<TabPane label="星球快讯" name="news">
					<Button type="primary" icon="md-add" @click="openNews()" style="margin-bottom:12px">新增快讯</Button>
					<Table border :loading="news.loading" :columns="newsColumns" :data="news.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="news.pageNumber" :total="news.total" :page-size="news.pageSize"
							@on-change="p => changePage('news', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- 排行榜 -->
				<TabPane label="通缉榜" name="rank">
					<Table border :loading="rank.loading" :columns="rankColumns" :data="rank.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="rank.pageNumber" :total="rank.total" :page-size="rank.pageSize"
							@on-change="p => changePage('rank', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- 追捕记录 -->
				<TabPane label="追捕记录" name="hunt">
					<Table border :loading="hunt.loading" :columns="huntColumns" :data="hunt.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="hunt.pageNumber" :total="hunt.total" :page-size="hunt.pageSize"
							@on-change="p => changePage('hunt', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- 开奖记录 -->
				<TabPane label="开奖记录" name="draw">
					<Table border :loading="draw.loading" :columns="drawColumns" :data="draw.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="draw.pageNumber" :total="draw.total" :page-size="draw.pageSize"
							@on-change="p => changePage('draw', p)" size="small" show-total></Page>
					</Row>
				</TabPane>

				<!-- 中奖记录 -->
				<TabPane label="中奖记录" name="winner">
					<Table border :loading="winner.loading" :columns="winnerColumns" :data="winner.data"></Table>
					<Row type="flex" justify="end" class="page">
						<Page :current="winner.pageNumber" :total="winner.total" :page-size="winner.pageSize"
							@on-change="p => changePage('winner', p)" size="small" show-total></Page>
					</Row>
				</TabPane>
			</Tabs>
		</Card>

		<!-- 奖池编辑 -->
		<Modal v-model="poolModal" :title="poolForm.id ? '编辑奖池' : '新建奖池'" :mask-closable="false">
			<Form :label-width="90">
				<FormItem label="所属校区"><Input v-model="poolForm.regionId" disabled placeholder="自动取自登录校区" /></FormItem>
				<FormItem label="期号"><Input v-model="poolForm.periodNo" /></FormItem>
				<FormItem label="标题"><Input v-model="poolForm.title" /></FormItem>
				<FormItem label="奖池金额"><InputNumber v-model="poolForm.poolAmount" :min="0" style="width:100%" /></FormItem>
				<FormItem label="开奖时间"><DatePicker type="datetime" v-model="poolForm.drawTime" format="yyyy-MM-dd HH:mm:ss" style="width:100%" /></FormItem>
				<FormItem label="状态">
					<Select v-model="poolForm.status">
						<Option :value="0">进行中</Option>
						<Option :value="1">已开奖</Option>
					</Select>
				</FormItem>
				<FormItem label="备注"><Input v-model="poolForm.remark" type="textarea" /></FormItem>
			</Form>
			<div slot="footer">
				<Button @click="poolModal=false">取消</Button>
				<Button type="primary" @click="savePool">保存</Button>
			</div>
		</Modal>

		<!-- 奖项管理 -->
		<Modal v-model="rewardModal" title="奖项配置" width="720" :mask-closable="false">
			<Button type="primary" icon="md-add" @click="addRewardRow" style="margin-bottom:12px">新增奖项</Button>
			<Table border :columns="rewardColumns" :data="rewardList"></Table>
			<div slot="footer">
				<Button @click="rewardModal=false">关闭</Button>
			</div>
		</Modal>

		<!-- 奖项编辑 -->
		<Modal v-model="rewardEditModal" :title="rewardForm.id ? '编辑奖项' : '新增奖项'" :mask-closable="false">
			<Form :label-width="90">
				<FormItem label="层级">
					<Select v-model="rewardForm.level">
						<Option :value="1">一等奖</Option>
						<Option :value="2">二等奖</Option>
						<Option :value="3">三等奖</Option>
						<Option :value="4">幸运奖</Option>
					</Select>
				</FormItem>
				<FormItem label="奖项名称"><Input v-model="rewardForm.levelName" /></FormItem>
				<FormItem label="奖励类型">
					<Select v-model="rewardForm.rewardType">
						<Option :value="0">现金</Option>
						<Option :value="1">优惠券</Option>
					</Select>
				</FormItem>
				<FormItem label="单份金额"><InputNumber v-model="rewardForm.amount" :min="0" style="width:100%" /></FormItem>
				<FormItem label="优惠券id" v-if="rewardForm.rewardType===1"><Input v-model="rewardForm.couponId" /></FormItem>
				<FormItem label="名额数量"><InputNumber v-model="rewardForm.quota" :min="0" style="width:100%" /></FormItem>
				<FormItem label="排序"><InputNumber v-model="rewardForm.sort" style="width:100%" /></FormItem>
			</Form>
			<div slot="footer">
				<Button @click="rewardEditModal=false">取消</Button>
				<Button type="primary" @click="saveReward">保存</Button>
			</div>
		</Modal>

		<!-- 任务编辑 -->
		<Modal v-model="taskModal" :title="taskForm.id ? '编辑任务' : '新增任务'" :mask-closable="false">
			<Form :label-width="90">
				<FormItem label="所属校区"><Input v-model="taskForm.regionId" disabled placeholder="自动取自登录校区" /></FormItem>
				<FormItem label="任务编码">
					<Select v-model="taskForm.code">
						<Option value="waimai">外卖订单</Option>
						<Option value="group">团购订单</Option>
						<Option value="invite">邀请注册</Option>
						<Option value="sign">每日签到</Option>
					</Select>
				</FormItem>
				<FormItem label="任务名称"><Input v-model="taskForm.name" /></FormItem>
				<FormItem label="描述"><Input v-model="taskForm.description" /></FormItem>
				<FormItem label="奖励券数"><InputNumber v-model="taskForm.rewardTickets" :min="0" style="width:100%" /></FormItem>
				<FormItem label="每日上限"><InputNumber v-model="taskForm.dailyLimit" :min="0" style="width:100%" /></FormItem>
				<FormItem label="排序"><InputNumber v-model="taskForm.sort" style="width:100%" /></FormItem>
				<FormItem label="是否启用">
					<i-switch v-model="taskForm.enabled" :true-value="1" :false-value="0" />
				</FormItem>
			</Form>
			<div slot="footer">
				<Button @click="taskModal=false">取消</Button>
				<Button type="primary" @click="saveTask">保存</Button>
			</div>
		</Modal>

		<!-- BUFF编辑 -->
		<Modal v-model="buffModal" :title="buffForm.id ? '编辑BUFF' : '新增BUFF'" :mask-closable="false">
			<Form :label-width="90">
				<FormItem label="所属校区"><Input v-model="buffForm.regionId" disabled placeholder="自动取自登录校区" /></FormItem>
				<FormItem label="类型">
					<Select v-model="buffForm.type">
						<Option value="double">双倍能量</Option>
						<Option value="shield">星际防护罩</Option>
						<Option value="lucky">幸运星辰</Option>
						<Option value="hunt">追猎雷达</Option>
						<Option value="stealth">隐身斗篷</Option>
					</Select>
				</FormItem>
				<FormItem label="名称"><Input v-model="buffForm.name" /></FormItem>
				<FormItem label="描述"><Input v-model="buffForm.description" /></FormItem>
				<FormItem label="消耗券"><InputNumber v-model="buffForm.costTickets" :min="0" style="width:100%" /></FormItem>
				<FormItem label="持续小时"><InputNumber v-model="buffForm.durationHours" :min="0" style="width:100%" /></FormItem>
				<FormItem label="效果值"><InputNumber v-model="buffForm.effectValue" :min="0" :step="0.1" style="width:100%" /></FormItem>
				<FormItem label="排序"><InputNumber v-model="buffForm.sort" style="width:100%" /></FormItem>
				<FormItem label="是否启用">
					<i-switch v-model="buffForm.enabled" :true-value="1" :false-value="0" />
				</FormItem>
			</Form>
			<div slot="footer">
				<Button @click="buffModal=false">取消</Button>
				<Button type="primary" @click="saveBuff">保存</Button>
			</div>
		</Modal>

		<!-- 快讯编辑 -->
		<Modal v-model="newsModal" :title="newsForm.id ? '编辑快讯' : '新增快讯'" :mask-closable="false">
			<Form :label-width="90">
				<FormItem label="所属校区"><Input v-model="newsForm.regionId" disabled placeholder="自动取自登录校区" /></FormItem>
				<FormItem label="类型">
					<Select v-model="newsForm.type">
						<Option value="sys">系统</Option>
						<Option value="rank">榜单</Option>
						<Option value="hunt">追捕</Option>
						<Option value="box">宝箱</Option>
						<Option value="draw">开奖</Option>
					</Select>
				</FormItem>
				<FormItem label="内容"><Input v-model="newsForm.content" type="textarea" /></FormItem>
				<FormItem label="置顶">
					<i-switch v-model="newsForm.isTop" :true-value="1" :false-value="0" />
				</FormItem>
				<FormItem label="是否展示">
					<i-switch v-model="newsForm.enabled" :true-value="1" :false-value="0" />
				</FormItem>
			</Form>
			<div slot="footer">
				<Button @click="newsModal=false">取消</Button>
				<Button type="primary" @click="saveNews">保存</Button>
			</div>
		</Modal>

		<!-- 经营配置编辑 -->
		<Modal v-model="operateModal" :title="operateTitle" width="620" :mask-closable="false">
			<Form :label-width="120">
				<FormItem label="所属校区"><Input v-model="operateForm.regionId" disabled placeholder="自动取自登录校区" /></FormItem>
				<FormItem v-for="f in operateFields" :key="f.key" :label="f.label">
					<i-switch v-if="f.type==='switch'" v-model="operateForm[f.key]" :true-value="1" :false-value="0" />
					<Select v-else-if="f.type==='select'" v-model="operateForm[f.key]">
						<Option v-for="op in f.options" :key="op.value" :value="op.value">{{ op.label }}</Option>
					</Select>
					<InputNumber v-else-if="f.type==='number'" v-model="operateForm[f.key]" :min="f.min === undefined ? 0 : f.min" :step="f.step || 1" style="width:100%" />
					<Input v-else v-model="operateForm[f.key]" />
				</FormItem>
			</Form>
			<div slot="footer">
				<Button @click="operateModal=false">取消</Button>
				<Button type="primary" @click="saveOperate">保存</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
	import {
		planetPoolPage, planetPoolSave, planetPoolDelete,
		planetRewardList, planetRewardSave, planetRewardDelete,
		planetTaskPage, planetTaskSave, planetTaskDelete,
		planetBuffPage, planetBuffSave, planetBuffDelete,
		planetNewsPage, planetNewsSave, planetNewsDelete,
		planetRankPage, planetHuntPage, planetDrawPage, planetWinnerPage,
		planetManualDraw,
		planetTreePage, planetTreeSave, planetTreeDelete,
		planetWarehousePage, planetWarehouseSave, planetWarehouseDelete,
		planetTowerPage, planetTowerSave, planetTowerDelete,
		planetSearchPage, planetSearchSave, planetSearchDelete,
		planetStaminaPage, planetStaminaSave, planetStaminaDelete,
		planetLandmarkPage, planetLandmarkSave, planetLandmarkDelete,
		planetEconomyPage
	} from '@/api/planet';

	export default {
		name: 'luckey',
		data() {
			return {
				activeTab: 'pool',
				regionId: '',
				pool: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				task: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				buff: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				news: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				rank: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				hunt: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				draw: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				winner: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				tree: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				warehouse: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				tower: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				search: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				stamina: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				landmark: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				economy: { loading: false, data: [], total: 0, pageNumber: 1, pageSize: 10 },
				// 弹窗
				poolModal: false,
				poolForm: {},
				rewardModal: false,
				rewardList: [],
				currentPoolId: '',
				rewardEditModal: false,
				rewardForm: {},
				taskModal: false,
				taskForm: {},
				buffModal: false,
				buffForm: {},
				newsModal: false,
				newsForm: {},
				operateModal: false,
				operateType: '',
				operateForm: {}
			}
		},
		computed: {
			operateTitle() {
				const map = { tree: '券树周期', warehouse: '仓库等级', tower: '防御塔等级', search: '搜查规则', stamina: '体力道具', landmark: '校园地标' }
				return (this.operateForm.id ? '编辑' : '新增') + (map[this.operateType] || '经营配置')
			},
			operateFields() {
				const fields = {
					tree: [
						{ key: 'cycleHours', label: '培育小时', type: 'number' },
						{ key: 'rate', label: '收益率', type: 'number', step: 0.01 },
						{ key: 'minInvest', label: '最低投入', type: 'number' },
						{ key: 'maxInvest', label: '最高投入', type: 'number' },
						{ key: 'maxProfit', label: '单次最高收益', type: 'number' },
						{ key: 'dailyProfitLimit', label: '每日收益上限', type: 'number' },
						{ key: 'sort', label: '排序', type: 'number' },
						{ key: 'enabled', label: '是否启用', type: 'switch' }
					],
					warehouse: [
						{ key: 'level', label: '等级', type: 'number' },
						{ key: 'capacity', label: '容量', type: 'number' },
						{ key: 'upgradeCost', label: '升级消耗券', type: 'number' },
						{ key: 'enabled', label: '是否启用', type: 'switch' }
					],
					tower: [
						{ key: 'level', label: '等级', type: 'number' },
						{ key: 'interceptRate', label: '拦截率(%)', type: 'number', step: 0.1 },
						{ key: 'damage', label: '体力伤害', type: 'number' },
						{ key: 'counterRate', label: '反击率(%)', type: 'number', step: 0.1 },
						{ key: 'upgradeCost', label: '升级消耗券', type: 'number' },
						{ key: 'enabled', label: '是否启用', type: 'switch' }
					],
					search: [
						{ key: 'dailyLimit', label: '每日次数', type: 'number' },
						{ key: 'staminaCost', label: '消耗体力', type: 'number' },
						{ key: 'minGain', label: '最低获得', type: 'number' },
						{ key: 'maxGain', label: '最高获得', type: 'number' },
						{ key: 'targetBalanceRate', label: '目标余额比例', type: 'number', step: 0.01 },
						{ key: 'dailyGainLimit', label: '每日获得上限', type: 'number' },
						{ key: 'targetProtectMinutes', label: '保护分钟', type: 'number' },
						{ key: 'enabled', label: '是否启用', type: 'switch' }
					],
					stamina: [
						{ key: 'name', label: '道具名称' },
						{ key: 'recoverStamina', label: '恢复体力', type: 'number' },
						{ key: 'costTickets', label: '消耗券', type: 'number' },
						{ key: 'dailyBuyLimit', label: '每日购买上限', type: 'number' },
						{ key: 'sort', label: '排序', type: 'number' },
						{ key: 'enabled', label: '是否启用', type: 'switch' }
					],
					landmark: [
						{ key: 'name', label: '地标名称' },
						{ key: 'icon', label: '图标' },
						{ key: 'benefitType', label: '收益类型', type: 'select', options: [
							{ value: 'ticket', label: '每日发券' },
							{ value: 'stamina_speed', label: '体力恢复加速' },
							{ value: 'search_rate', label: '搜查成功率' },
							{ value: 'tower_damage', label: '防御塔伤害' }
						] },
						{ key: 'benefitValue', label: '收益值', type: 'number', step: 0.1 },
						{ key: 'dailyOutputLimit', label: '日输出上限', type: 'number' },
						{ key: 'openStartTime', label: '开放开始' },
						{ key: 'openEndTime', label: '开放结束' },
						{ key: 'settleTime', label: '结算时间' },
						{ key: 'sort', label: '排序', type: 'number' },
						{ key: 'enabled', label: '是否启用', type: 'switch' }
					]
				}
				return fields[this.operateType] || []
			},
			poolColumns() {
				return [
					{ title: '期号', key: 'periodNo', minWidth: 120 },
					{ title: '标题', key: 'title', minWidth: 120 },
					{ title: '商圈', key: 'regionId', width: 100 },
					{ title: '奖池金额', key: 'poolAmount', width: 100 },
					{ title: '开奖时间', key: 'drawTime', minWidth: 160 },
					{ title: '参与', key: 'joinCount', width: 70 },
					{ title: '中奖', key: 'winnerCount', width: 70 },
					{
						title: '状态', width: 90, render: (h, p) => h('Tag', {
							props: { color: p.row.status === 1 ? 'green' : 'blue' }
						}, p.row.status === 1 ? '已开奖' : '进行中')
					},
					{
						title: '操作', width: 280, align: 'center', render: (h, params) => {
							return h('div', [
								h('Button', { props: { type: 'text', size: 'small' }, on: { click: () => this.openPool(params.row) } }, '编辑'),
								h('Button', { props: { type: 'text', size: 'small' }, on: { click: () => this.openReward(params.row) } }, '奖项'),
								h('Button', {
									props: { type: 'text', size: 'small', disabled: params.row.status === 1 },
									on: { click: () => this.doManualDraw(params.row) }
								}, '手动开奖'),
								h('Button', { props: { type: 'text', size: 'small' }, style: { color: '#ed4014' }, on: { click: () => this.delPool(params.row) } }, '删除')
							])
						}
					}
				]
			},
			rewardColumns() {
				return [
					{ title: '层级', key: 'level', width: 70 },
					{ title: '名称', key: 'levelName', minWidth: 100 },
					{ title: '类型', width: 80, render: (h, p) => h('span', p.row.rewardType === 1 ? '优惠券' : '现金') },
					{ title: '金额', key: 'amount', width: 90 },
					{ title: '名额', key: 'quota', width: 70 },
					{
						title: '操作', width: 130, align: 'center', render: (h, params) => {
							return h('div', [
								h('Button', { props: { type: 'text', size: 'small' }, on: { click: () => this.editReward(params.row) } }, '编辑'),
								h('Button', { props: { type: 'text', size: 'small' }, style: { color: '#ed4014' }, on: { click: () => this.delReward(params.row) } }, '删除')
							])
						}
					}
				]
			},
			taskColumns() {
				return [
					{ title: '编码', key: 'code', width: 100 },
					{ title: '名称', key: 'name', minWidth: 120 },
					{ title: '描述', key: 'description', minWidth: 180 },
					{ title: '奖励券', key: 'rewardTickets', width: 80 },
					{ title: '每日上限', key: 'dailyLimit', width: 90 },
					{ title: '商圈', key: 'regionId', width: 90 },
					{ title: '启用', width: 70, render: (h, p) => h('Tag', { props: { color: p.row.enabled === 1 ? 'green' : 'default' } }, p.row.enabled === 1 ? '是' : '否') },
					{
						title: '操作', width: 150, align: 'center', render: (h, params) => {
							return h('div', [
								h('Button', { props: { type: 'text', size: 'small' }, on: { click: () => this.openTask(params.row) } }, '编辑'),
								h('Button', { props: { type: 'text', size: 'small' }, style: { color: '#ed4014' }, on: { click: () => this.delTask(params.row) } }, '删除')
							])
						}
					}
				]
			},
			buffColumns() {
				return [
					{ title: '类型', key: 'type', width: 100 },
					{ title: '名称', key: 'name', minWidth: 120 },
					{ title: '描述', key: 'description', minWidth: 180 },
					{ title: '消耗券', key: 'costTickets', width: 80 },
					{ title: '时长(h)', key: 'durationHours', width: 80 },
					{ title: '效果值', key: 'effectValue', width: 80 },
					{ title: '启用', width: 70, render: (h, p) => h('Tag', { props: { color: p.row.enabled === 1 ? 'green' : 'default' } }, p.row.enabled === 1 ? '是' : '否') },
					{
						title: '操作', width: 150, align: 'center', render: (h, params) => {
							return h('div', [
								h('Button', { props: { type: 'text', size: 'small' }, on: { click: () => this.openBuff(params.row) } }, '编辑'),
								h('Button', { props: { type: 'text', size: 'small' }, style: { color: '#ed4014' }, on: { click: () => this.delBuff(params.row) } }, '删除')
							])
						}
					}
				]
			},
			newsColumns() {
				return [
					{ title: '类型', key: 'type', width: 90 },
					{ title: '内容', key: 'content', minWidth: 260 },
					{ title: '商圈', key: 'regionId', width: 90 },
					{ title: '置顶', width: 70, render: (h, p) => h('span', p.row.isTop === 1 ? '是' : '否') },
					{ title: '展示', width: 70, render: (h, p) => h('span', p.row.enabled === 1 ? '是' : '否') },
					{ title: '时间', key: 'createTime', minWidth: 160 },
					{
						title: '操作', width: 150, align: 'center', render: (h, params) => {
							return h('div', [
								h('Button', { props: { type: 'text', size: 'small' }, on: { click: () => this.openNews(params.row) } }, '编辑'),
								h('Button', { props: { type: 'text', size: 'small' }, style: { color: '#ed4014' }, on: { click: () => this.delNews(params.row) } }, '删除')
							])
						}
					}
				]
			},
			rankColumns() {
				return [
					{ title: '用户id', key: 'userId', minWidth: 120 },
					{ title: '昵称', key: 'nickname', minWidth: 120 },
					{ title: '商圈', key: 'regionId', width: 100 },
					{ title: '当前券', key: 'ticketCount', width: 90 },
					{ title: '累计券', key: 'totalTicket', width: 90 },
					{ title: '连续签到', key: 'consecutiveSignDays', width: 90 },
					{ title: '霸榜天数', key: 'rankKeepDays', width: 90 }
				]
			},
			huntColumns() {
				return [
					{ title: '发起人', key: 'fromUserName', minWidth: 110 },
					{ title: '目标', key: 'toUserName', minWidth: 110 },
					{ title: '结果', width: 90, render: (h, p) => h('span', this.huntResultText(p.row.result)) },
					{ title: '缴获', key: 'gainTickets', width: 70 },
					{ title: '悬赏', key: 'bountyTickets', width: 70 },
					{ title: '日期', key: 'huntDate', width: 120 },
					{ title: '时间', key: 'createTime', minWidth: 160 }
				]
			},
			drawColumns() {
				return [
					{ title: '期号', key: 'periodNo', minWidth: 130 },
					{ title: '商圈', key: 'regionId', width: 100 },
					{ title: '奖池金额', key: 'poolAmount', width: 100 },
					{ title: '参与人数', key: 'joinCount', width: 90 },
					{ title: '券总数', key: 'ticketTotal', width: 90 },
					{ title: '中奖人数', key: 'winnerCount', width: 90 },
					{ title: '开奖时间', key: 'drawTime', minWidth: 160 }
				]
			},
			winnerColumns() {
				return [
					{ title: '期号', key: 'periodNo', minWidth: 130 },
					{ title: '中奖人', key: 'userName', minWidth: 110 },
					{ title: '奖项', key: 'levelName', width: 100 },
					{ title: '金额', key: 'amount', width: 90 },
					{ title: '已领取', width: 80, render: (h, p) => h('Tag', { props: { color: p.row.isReceived === 1 ? 'green' : 'orange' } }, p.row.isReceived === 1 ? '已领' : '未领') },
					{ title: '时间', key: 'createTime', minWidth: 160 }
				]
			},
			treeColumns() {
				return [
					{ title: '周期(h)', key: 'cycleHours', width: 90 },
					{ title: '收益率', key: 'rate', width: 90 },
					{ title: '投入范围', minWidth: 140, render: (h, p) => h('span', `${p.row.minInvest || 0} - ${p.row.maxInvest || '不限'}`) },
					{ title: '单次收益上限', key: 'maxProfit', width: 120 },
					{ title: '日收益上限', key: 'dailyProfitLimit', width: 110 },
					{ title: '启用', width: 70, render: (h, p) => this.enabledTag(h, p.row.enabled) },
					this.operateActionColumn('tree')
				]
			},
			warehouseColumns() {
				return [
					{ title: '等级', key: 'level', width: 90 },
					{ title: '容量', key: 'capacity', width: 100 },
					{ title: '升级消耗券', key: 'upgradeCost', width: 120 },
					{ title: '启用', width: 70, render: (h, p) => this.enabledTag(h, p.row.enabled) },
					this.operateActionColumn('warehouse')
				]
			},
			towerColumns() {
				return [
					{ title: '等级', key: 'level', width: 80 },
					{ title: '拦截率(%)', key: 'interceptRate', width: 110 },
					{ title: '伤害', key: 'damage', width: 90 },
					{ title: '反击率(%)', key: 'counterRate', width: 110 },
					{ title: '升级消耗券', key: 'upgradeCost', width: 120 },
					{ title: '启用', width: 70, render: (h, p) => this.enabledTag(h, p.row.enabled) },
					this.operateActionColumn('tower')
				]
			},
			searchColumns() {
				return [
					{ title: '每日次数', key: 'dailyLimit', width: 90 },
					{ title: '体力消耗', key: 'staminaCost', width: 90 },
					{ title: '获得范围', minWidth: 120, render: (h, p) => h('span', `${p.row.minGain || 0} - ${p.row.maxGain || 0}`) },
					{ title: '目标比例', key: 'targetBalanceRate', width: 90 },
					{ title: '日获得上限', key: 'dailyGainLimit', width: 110 },
					{ title: '启用', width: 70, render: (h, p) => this.enabledTag(h, p.row.enabled) },
					this.operateActionColumn('search')
				]
			},
			staminaColumns() {
				return [
					{ title: '名称', key: 'name', minWidth: 120 },
					{ title: '恢复体力', key: 'recoverStamina', width: 90 },
					{ title: '消耗券', key: 'costTickets', width: 90 },
					{ title: '日购买上限', key: 'dailyBuyLimit', width: 110 },
					{ title: '启用', width: 70, render: (h, p) => this.enabledTag(h, p.row.enabled) },
					this.operateActionColumn('stamina')
				]
			},
			landmarkColumns() {
				return [
					{ title: '名称', key: 'name', minWidth: 120 },
					{ title: '图标', key: 'icon', width: 80 },
					{ title: '收益类型', key: 'benefitType', width: 120 },
					{ title: '收益值', key: 'benefitValue', width: 90 },
					{ title: '开放时间', minWidth: 150, render: (h, p) => h('span', `${p.row.openStartTime || '--'} - ${p.row.openEndTime || '--'}`) },
					{ title: '启用', width: 70, render: (h, p) => this.enabledTag(h, p.row.enabled) },
					this.operateActionColumn('landmark')
				]
			},
			economyColumns() {
				return [
					{ title: '日期', key: 'auditDate', width: 120 },
					{ title: '产出券', key: 'newTickets', width: 90 },
					{ title: '回收券', key: 'burnTickets', width: 90 },
					{ title: '净增', key: 'netTickets', width: 90 },
					{ title: '种植产出', key: 'plantOutput', width: 100 },
					{ title: '地标回收', key: 'landmarkBurn', width: 100 },
					{ title: '升级回收', key: 'upgradeBurn', width: 100 },
					{ title: '活跃持券用户', key: 'activeUserCount', width: 120 },
					{ title: '预警', width: 80, render: (h, p) => h('Tag', { props: { color: p.row.warning === 1 ? 'red' : 'green' } }, p.row.warning === 1 ? '净增' : '平衡') }
				]
			}
		},
		mounted() {
			// 后台按登录代理商所属校区隔离：regionId 取自登录用户的 departmentId，不允许手动输入
			this.regionId = this.resolveRegionId()
			if (this.regionId) {
				this.loadPool()
			} else {
				this.$Message.warning('当前账号未绑定校区，无法管理白嫖星球')
			}
		},
		methods: {
			huntResultText(r) {
				return { success: '缴获成功', shield: '对方防护', fail: '扑空' }[r] || r
			},
			enabledTag(h, enabled) {
				return h('Tag', { props: { color: enabled === 1 ? 'green' : 'default' } }, enabled === 1 ? '是' : '否')
			},
			operateActionColumn(type) {
				return {
					title: '操作', width: 150, align: 'center', render: (h, params) => h('div', [
						h('Button', { props: { type: 'text', size: 'small' }, on: { click: () => this.openOperate(type, params.row) } }, '编辑'),
						h('Button', { props: { type: 'text', size: 'small' }, style: { color: '#ed4014' }, on: { click: () => this.delOperate(type, params.row) } }, '删除')
					])
				}
			},
			onTabChange(name) {
				const map = {
					pool: this.loadPool, task: this.loadTask, buff: this.loadBuff,
					news: this.loadNews, rank: this.loadRank, hunt: this.loadHunt,
					draw: this.loadDraw, winner: this.loadWinner,
					tree: this.loadTree, warehouse: this.loadWarehouse, tower: this.loadTower,
					search: this.loadSearch, stamina: this.loadStamina, landmark: this.loadLandmark,
					economy: this.loadEconomy
				}
				map[name] && map[name]()
			},
			reloadActive() {
				this.onTabChange(this.activeTab)
			},
			changePage(type, p) {
				this[type].pageNumber = p
				this.onTabChange(type)
			},
			baseParams(type) {
				return {
					pageNumber: this[type].pageNumber,
					pageSize: this[type].pageSize,
					regionId: this.regionId || undefined
				}
			},
			fill(type, res) {
				this[type].loading = false
				if (res && res.success && res.result) {
					this[type].data = res.result.records || []
					this[type].total = res.result.total || 0
				}
			},
			// 从登录用户信息中解析所属校区(departmentId)
			resolveRegionId() {
				try {
					const user = this.getStore('user')
					return user ? (JSON.parse(user).departmentId || '') : ''
				} catch (e) {
					return ''
				}
			},
			// 抽奖子系统(奖池/开奖/中奖)必须归属校区，仅限本区域内进行
			guardRegion(type) {
				if (!this.regionId) {
					this[type].data = []
					this[type].total = 0
					this[type].loading = false
					this.$Message.warning('当前账号未绑定校区，无法管理白嫖星球')
					return false
				}
				return true
			},
			// ---- 列表加载（白嫖星球全部功能均限定商圈，未选商圈不查询） ----
			loadPool() { if (!this.guardRegion('pool')) return; this.pool.loading = true; planetPoolPage(this.baseParams('pool')).then(r => this.fill('pool', r)) },
			loadTask() { if (!this.guardRegion('task')) return; this.task.loading = true; planetTaskPage(this.baseParams('task')).then(r => this.fill('task', r)) },
			loadBuff() { if (!this.guardRegion('buff')) return; this.buff.loading = true; planetBuffPage(this.baseParams('buff')).then(r => this.fill('buff', r)) },
			loadNews() { if (!this.guardRegion('news')) return; this.news.loading = true; planetNewsPage(this.baseParams('news')).then(r => this.fill('news', r)) },
			loadRank() { if (!this.guardRegion('rank')) return; this.rank.loading = true; planetRankPage(this.baseParams('rank')).then(r => this.fill('rank', r)) },
			loadHunt() { if (!this.guardRegion('hunt')) return; this.hunt.loading = true; planetHuntPage(this.baseParams('hunt')).then(r => this.fill('hunt', r)) },
			loadDraw() { if (!this.guardRegion('draw')) return; this.draw.loading = true; planetDrawPage(this.baseParams('draw')).then(r => this.fill('draw', r)) },
			loadWinner() { if (!this.guardRegion('winner')) return; this.winner.loading = true; planetWinnerPage(this.baseParams('winner')).then(r => this.fill('winner', r)) },
			loadTree() { if (!this.guardRegion('tree')) return; this.tree.loading = true; planetTreePage(this.baseParams('tree')).then(r => this.fill('tree', r)) },
			loadWarehouse() { if (!this.guardRegion('warehouse')) return; this.warehouse.loading = true; planetWarehousePage(this.baseParams('warehouse')).then(r => this.fill('warehouse', r)) },
			loadTower() { if (!this.guardRegion('tower')) return; this.tower.loading = true; planetTowerPage(this.baseParams('tower')).then(r => this.fill('tower', r)) },
			loadSearch() { if (!this.guardRegion('search')) return; this.search.loading = true; planetSearchPage(this.baseParams('search')).then(r => this.fill('search', r)) },
			loadStamina() { if (!this.guardRegion('stamina')) return; this.stamina.loading = true; planetStaminaPage(this.baseParams('stamina')).then(r => this.fill('stamina', r)) },
			loadLandmark() { if (!this.guardRegion('landmark')) return; this.landmark.loading = true; planetLandmarkPage(this.baseParams('landmark')).then(r => this.fill('landmark', r)) },
			loadEconomy() { if (!this.guardRegion('economy')) return; this.economy.loading = true; planetEconomyPage(this.baseParams('economy')).then(r => this.fill('economy', r)) },
			// ---- 奖池 ----
			openPool(row) {
				this.poolForm = row ? Object.assign({}, row) : { regionId: this.regionId || '', poolAmount: 88.88, status: 0 }
				this.poolModal = true
			},
			savePool() {
				const form = Object.assign({}, this.poolForm)
				if (form.drawTime instanceof Date) {
					form.drawTime = this.fmt(form.drawTime)
				}
				planetPoolSave(form).then(r => {
					if (r && r.success) {
						this.$Message.success('保存成功')
						this.poolModal = false
						this.loadPool()
					} else { this.$Message.error((r && r.message) || '保存失败') }
				})
			},
			delPool(row) {
				this.$Modal.confirm({
					title: '确认删除', content: '删除奖池将同时删除其奖项配置',
					onOk: () => planetPoolDelete(row.id).then(() => { this.$Message.success('删除成功'); this.loadPool() })
				})
			},
			doManualDraw(row) {
				const region = row.regionId || this.regionId
				if (!region) {
					this.$Message.warning('该奖池缺少商圈，无法开奖')
					return
				}
				this.$Modal.confirm({
					title: '确认开奖', content: `立即对期号 ${row.periodNo} 执行加权开奖（商圈 ${region}）？`,
					onOk: () => planetManualDraw(row.id, region).then(r => {
						if (r && r.success) { this.$Message.success('开奖完成'); this.loadPool() }
						else { this.$Message.error((r && r.message) || '开奖失败') }
					})
				})
			},
			// ---- 奖项 ----
			openReward(row) {
				this.currentPoolId = row.id
				this.rewardModal = true
				this.loadReward()
			},
			loadReward() {
				planetRewardList(this.currentPoolId).then(r => {
					if (r && r.success) this.rewardList = r.result || []
				})
			},
			addRewardRow() {
				this.rewardForm = { poolId: this.currentPoolId, regionId: this.regionId || '', level: 1, rewardType: 0, amount: 0, quota: 1, sort: 0 }
				this.rewardEditModal = true
			},
			editReward(row) {
				this.rewardForm = Object.assign({}, row)
				this.rewardEditModal = true
			},
			saveReward() {
				planetRewardSave(this.rewardForm).then(r => {
					if (r && r.success) { this.$Message.success('保存成功'); this.rewardEditModal = false; this.loadReward() }
					else { this.$Message.error((r && r.message) || '保存失败') }
				})
			},
			delReward(row) {
				planetRewardDelete(row.id).then(() => { this.$Message.success('删除成功'); this.loadReward() })
			},
			// ---- 任务 ----
			openTask(row) {
				this.taskForm = row ? Object.assign({}, row) : { regionId: this.regionId || '', code: 'waimai', rewardTickets: 1, dailyLimit: 0, sort: 0, enabled: 1 }
				this.taskModal = true
			},
			saveTask() {
				planetTaskSave(this.taskForm).then(r => {
					if (r && r.success) { this.$Message.success('保存成功'); this.taskModal = false; this.loadTask() }
					else { this.$Message.error((r && r.message) || '保存失败') }
				})
			},
			delTask(row) {
				planetTaskDelete(row.id).then(() => { this.$Message.success('删除成功'); this.loadTask() })
			},
			// ---- BUFF ----
			openBuff(row) {
				this.buffForm = row ? Object.assign({}, row) : { regionId: this.regionId || '', type: 'double', costTickets: 5, durationHours: 24, effectValue: 0, sort: 0, enabled: 1 }
				this.buffModal = true
			},
			saveBuff() {
				planetBuffSave(this.buffForm).then(r => {
					if (r && r.success) { this.$Message.success('保存成功'); this.buffModal = false; this.loadBuff() }
					else { this.$Message.error((r && r.message) || '保存失败') }
				})
			},
			delBuff(row) {
				planetBuffDelete(row.id).then(() => { this.$Message.success('删除成功'); this.loadBuff() })
			},
			// ---- 快讯 ----
			openNews(row) {
				this.newsForm = row ? Object.assign({}, row) : { regionId: this.regionId || '', type: 'sys', isTop: 0, enabled: 1 }
				this.newsModal = true
			},
			saveNews() {
				planetNewsSave(this.newsForm).then(r => {
					if (r && r.success) { this.$Message.success('保存成功'); this.newsModal = false; this.loadNews() }
					else { this.$Message.error((r && r.message) || '保存失败') }
				})
			},
			delNews(row) {
				planetNewsDelete(row.id).then(() => { this.$Message.success('删除成功'); this.loadNews() })
			},
			operateApis(type) {
				return {
					tree: { save: planetTreeSave, del: planetTreeDelete, load: this.loadTree, defaults: { cycleHours: 8, rate: 0.05, minInvest: 1, maxInvest: 50, maxProfit: 5, dailyProfitLimit: 10, sort: 0, enabled: 1 } },
					warehouse: { save: planetWarehouseSave, del: planetWarehouseDelete, load: this.loadWarehouse, defaults: { level: 1, capacity: 10, upgradeCost: 10, enabled: 1 } },
					tower: { save: planetTowerSave, del: planetTowerDelete, load: this.loadTower, defaults: { level: 1, interceptRate: 5, damage: 5, counterRate: 2, upgradeCost: 10, enabled: 1 } },
					search: { save: planetSearchSave, del: planetSearchDelete, load: this.loadSearch, defaults: { dailyLimit: 3, staminaCost: 30, minGain: 1, maxGain: 3, targetBalanceRate: 0.1, dailyGainLimit: 8, targetProtectMinutes: 30, enabled: 1 } },
					stamina: { save: planetStaminaSave, del: planetStaminaDelete, load: this.loadStamina, defaults: { name: '松果能量', recoverStamina: 30, costTickets: 2, dailyBuyLimit: 2, sort: 0, enabled: 1 } },
					landmark: { save: planetLandmarkSave, del: planetLandmarkDelete, load: this.loadLandmark, defaults: { name: '图书馆', icon: 'LIB', benefitType: 'ticket', benefitValue: 5, dailyOutputLimit: 5, openStartTime: '08:00', openEndTime: '22:00', settleTime: '22:00', sort: 0, enabled: 1 } }
				}[type]
			},
			openOperate(type, row) {
				const api = this.operateApis(type)
				this.operateType = type
				this.operateForm = row ? Object.assign({}, row) : Object.assign({ regionId: this.regionId || '' }, api.defaults)
				this.operateModal = true
			},
			saveOperate() {
				const api = this.operateApis(this.operateType)
				api.save(this.operateForm).then(r => {
					if (r && r.success) {
						this.$Message.success('保存成功')
						this.operateModal = false
						api.load()
					} else {
						this.$Message.error((r && r.message) || '保存失败')
					}
				})
			},
			delOperate(type, row) {
				const api = this.operateApis(type)
				this.$Modal.confirm({
					title: '确认删除',
					content: '确认删除该经营配置？',
					onOk: () => api.del(row.id).then(() => { this.$Message.success('删除成功'); api.load() })
				})
			},
			fmt(d) {
				const p = n => (n < 10 ? '0' + n : n)
				return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`
			}
		}
	}
</script>

<style scoped>
	.region-bar {
		margin-bottom: 16px;
	}
	.region-bar .ivu-input-wrapper {
		margin-right: 12px;
	}
	.page {
		margin-top: 16px;
	}
</style>
