<template>
	<!-- 顶部导航栏 -->
	<custom-nav-bar :left="false" leftText="" @leftClick="" title="轮灌">
		<template v-slot:right>
			<view class="fixed">
				<view align="right" class="iconfont icon-jia1 icon" @click="addTask()" v-show="tab.current === 0"
					v-hasPermi="['system:irrigateTask:add']" />
				<view align="right" class="iconfont icon-jia1 icon" @click="editScenario()" v-show="tab.current === 1"
					v-hasPermi="['irrigate:irrigateScenario:saveIrrigateScenario']" />
			</view>
		</template>
	</custom-nav-bar>
	<!-- 选项卡 -->
	<uni-segmented-control :current="tab.current" :values="tab.items" @clickItem="onClickItem" styleType="text"
		activeColor="#39ac4f"></uni-segmented-control>

	<view class="content">
		<view v-show="tab.current === 0">
			<z-paging ref="pagingTask" @query="queryTask" v-model="task" :auto-show-system-loading="true"
				:default-page-size="pageSize" empty-view-text="抱歉，暂时还没有相关数据！" class="custom-z-paging">
				<view class="card" v-for="item in task">
					<view class="cards-title">
						<view>
							<view class="text">任务名称：{{item.taskName}}</view>
						</view>
						<view class="title-r">
							<!-- default（灰色）、primary（蓝色）、success（绿色）、warning(黄色)、error(红色)、royal(紫色) -->
							<uni-tag type="default" :circle="true" text="未轮灌" v-if="item.status == 0" />
							<uni-tag type="primary" :circle="true" text="轮灌中" v-else-if="item.status == 1" />
							<uni-tag type="success" :circle="true" text="已完成" v-else-if="item.status == 2" />
							<uni-tag type="warning" :circle="true" text="已终止" v-else-if="item.status == 3" />
							<uni-tag type="royal" :circle="true" text="暂停" v-else-if="item.status == 4" />
							<uni-tag type="error" :circle="true" text="未知" v-else />
						</view>
					</view>
					<view class="content">
						<view class="cards-content">
							<view class="txt-list">
								<view class="list1">
									<view><text>轮灌方案：</text>{{item.scenarioName}}</view>
									<view><text>轮灌组数：</text>{{item.groupNum}}</view>
									<!-- <view><text>起始组：</text>{{item.startGroup}}</view> -->
									<!-- <view><text>总时长：</text>{{item.irrigateTotalDuration}}</view> -->
									<!-- <view><text>是否调压：</text>{{item.isAutoAdjust == 0 ? '否':'是'}}</view> -->
									<!-- <view><text>压力：</text>{{`${item.pressureMin} - ${item.pressureMax}`}}</view> -->
									<view><text>开始时间：</text>{{item.startTime}}</view>
									<view><text>结束时间：</text>{{item.endTime}}</view>
								</view>
								<view class="right" @click="taskDetail(item)"
									v-hasPermi="['system:irrigateTask:query']">
									<view class="iconfont icon-you" />
								</view>
							</view>
							<view class="card-font">
								<u-button v-hasPermi="['system:irrigateTask:remove']" size="mini" type="error" text="删除"
									@click="delTask(item)" v-if="item.status == 0" />
							</view>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
		<view v-show="tab.current === 1">
			<z-paging ref="pagingScenario" @query="queryScenario" v-model="scenario" :auto-show-system-loading="true"
				:default-page-size="pageSize" empty-view-text="抱歉，暂时还没有相关数据！" class="custom-z-paging">
				<view class="card" v-for="item in scenario">
					<view class="cards-title">
						<view>
							<view class="text">方案名称：{{item.scenarioName}}</view>
						</view>
						<view class="title-r">
							<uni-tag class="bg-green" :circle="true" text="启用" v-if="item.delFlag == '0'" />
							<uni-tag class="bg-red" :circle="true" text="禁用" v-else />
						</view>
					</view>
					<view class="content">
						<view class="cards-content">
							<view class="txt-list">
								<view class="list2">
									<view><text>轮灌组数量：</text>{{item.groupNum}}</view>
									<view><text>总时长：</text>{{item.irrigateTotalDuration}}</view>
									<view><text>自动调压：</text>{{item.isAutoAdjust == 0 ? '否':'是'}}</view>
									<view><text>压力：</text>{{`${item.pressureMin} - ${item.pressureMax}`}}</view>
								</view>
								<view class="right" @click="irrigationgroup(item)">
									<view class="iconfont icon-you" />
								</view>
							</view>
							<view class="card-font">
								<u-button v-hasPermi="['irrigate:irrigateScenario:saveIrrigateScenario']" type="success"
									size="mini" text="编辑" @click="editScenario(item)" />
								<u-button v-hasPermi="['irrigate:irrigateScenario:delFlag']" size="mini" type="error"
									text="禁用" @click="updateScenario(item,true)" v-if="item.delFlag == '0'" />
								<u-button v-hasPermi="['irrigate:irrigateScenario:delFlag']" size="mini" type="success"
									text="启用" @click="updateScenario(item,false)" v-else />
							</view>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
	</view>
</template>

<script>
	import useUserStore from '@/store/modules/user'
	const permissions = useUserStore.state.permissions
	import * as taskApi from '@/api/irrigate/task.js'
	import * as isApi from '@/api/irrigate/irrigateScenario.js'

	export default {
		data() {
			return {
				tab: {
					items: ['轮灌详情', '轮灌方案'],
					current: 0,
				},
				landId: null,
				pageSize: 20,
				landId: null,
				task: [],
				scenario: [],
				permission: {
					task: false,
					scenario: false,
				}
			};
		},
		onShow() {
			if (this.landId) {
				this.reload();
			}
			if (getApp().ijs.land) {
				this.landId = getApp().ijs.land.id;
			}
		},
		mounted() {
			this.permission.task = this.hasPermissions("system:irrigateTask:list");
			this.permission.scenario = this.hasPermissions("irrigate:irrigateScenario:list");
		},
		methods: {
			hasPermissions(key) {
				if (permissions.includes("*:*:*") || permissions.includes(key)) {
					return true;
				} else {
					return false;
				}
			},
			reload() {
				if (this.tab.current == 0) {
					this.$refs.pagingTask.reload();
				}
				if (this.tab.current == 1) {
					this.$refs.pagingScenario.reload();
				}
			},
			onClickItem(e) {
				if (this.tab.current != e.currentIndex) {
					this.tab.current = e.currentIndex;
				}
				this.reload();
			},
			async queryTask() {
				if (!this.permission.task) {
					this.$refs.pagingTask.completeByTotal([], 0);
					return;
				}
				taskApi.listTask({
					landId: this.landId || -1
				}).then(res => {
					this.$refs.pagingTask.completeByTotal(res.rows, res.total);
				});
			},
			async queryScenario() {
				if (!this.permission.scenario) {
					this.$refs.pagingScenario.completeByTotal([], 0);
					return;
				}
				isApi.list({
					landId: this.landId || -1,
					scenarioType: 2,
					status: "0",
					// delFlag: "0",
				}).then(res => {
					this.$refs.pagingScenario.completeByTotal(res.rows, res.total);
				})
			},
			/********************************* 任务 *********************************/
			//新增任务
			addTask(item = null) {
				let url = `/pages/irrigation/task/task?landId=${this.landId}`
				uni.navigateTo({
					url: url
				});
			},
			//删除任务
			delTask(item) {
				this.$showConfirm(`删除"${item.taskName}"?`).then(() => {
					taskApi.delTask(item.id).then(res => {
						this.$toast("删除成功");
						this.reload();
					}).catch(err => {
						this.$showConfirm(err, false).then(() => {
							this.reload();
						})
					})
				}).catch(() => {});
			},
			//任务详情
			taskDetail(item) {
				uni.navigateTo({
					url: `/pages/irrigation/task/detail?id=${item.id}`
				});
			},

			/********************************* 方案 *********************************/
			//方案编辑
			editScenario(item = null) {
				let url = `/pages/irrigation/scenario/scenario`
				if (item) {
					url = `${url}?id=${item.id}`
				} else {
					url = `${url}?landId=${this.landId}`
				}
				uni.navigateTo({
					url: url
				});
			},
			//方案详情
			irrigationgroup(item) {
				uni.navigateTo({
					url: `/pages/irrigation/scenario/detail?id=${item.id}`
				});
			},
			//方案删除
			updateScenario(item, disabled) {
				if (disabled) {
					this.$showConfirm(`禁用"${item.scenarioName}"?`).then(() => {
						isApi.disabled(item.id).then(res => {
							this.$toast("禁用成功");
							this.reload();
						})
					}).catch(() => {});
				} else {
					this.$showConfirm(`启用"${item.scenarioName}"?`).then(() => {
						isApi.enable(item.id).then(res => {
							this.$toast("启用成功");
							this.reload();
						})
					}).catch(() => {});
				}
			}



			// // 页面跳转
			// config() {
			// 	uni.navigateTo({
			// 		url: '/pages/irrigation/config'
			// 	});
			// }

		},

	};
</script>

<style lang="scss" scoped>
	// 选项卡
	.segmented-control {
		background-color: #fff;
	}

	.segmented-control__item--text {
		font-weight: bold;
	}

	::v-deep .segmented-control__text {
		font-size: 16px;
	}

	.custom-z-paging {
		/* #ifdef H5 */
		margin-top: 80px;
		/* #endif */
		/* #ifdef APP-PLUS */
		margin-top: 105px;
		/* #endif */
	}
</style>