<template>
	<view>
		<scroll-view :scroll-y="true">
			<view class="cu-bar bg-white solid-bottom">
				<view class="action">
					<text class="cuIcon-title text-orange "></text> 班组列表
				</view>
				<view class="action">
					<button class="cu-btn bg-blue shadow" @click="addClick">新增</button>
				</view>
			</view>
			<view class="cu-list menu card-menu margin-top">
				<view class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''" @touchstart="ListTouchStart"
					@touchmove="ListTouchMove" @touchend="ListTouchEnd" :data-target="'move-box-' + index"
					style="margin-top: 10rpx;" v-for="(item,index) in teamList" :key="index">
					<view class="content" @click="editTeam(item)">
						
						<view class="cu-form-group">
							<view class="title" style="min-width: calc(6em + 15px);">制衣班组</view>
							<text class="text-grey" style="flex: 1;">{{item.zyTeam}}</text>
						</view>
						<view class="cu-form-group">
							<view class="title" style="min-width: calc(6em + 15px);">制衣生产车间</view>
							<text class="text-grey" style="flex: 1;">{{item.workshop.zyProductWorkshop}}</text>
						</view>
						<view class="cu-form-group">
							<view class="title" style="min-width: calc(6em + 15px);" >状态</view>
							<text v-if="item.status" class="text-grey" style="flex: 1;">正常</text>
							<text v-else="item.status" class="text-grey" style="flex: 1;">禁用</text>
						</view>
<!-- 						<view class="cu-form-group">
							<view class="title" style="min-width: calc(6em + 15px);">订单号</view>
							<text class="text-grey" style="flex: 1;">{{item.orderNo}}</text>
						</view>
						<view class="cu-form-group"> -->
<!-- 							<view class="title" style="min-width: calc(6em + 15px);">交期</view>
							<text class="text-grey" style="flex: 1;">{{item.deliveryDate}}</text>
						</view> -->
					</view>
					<view class="move">
						<view class="bg-grey" @click="editTeam(item)">修改</view>
						<view class="bg-red" @click="deleteTeam(item)">删除</view>
					</view>
				</view>
			</view>
			<u-toast ref="uToast" />
		</scroll-view>
	</view>
</template>

<script>
	import {
		list,
		deleteTeam
	} from '@/api/biz/team.js'
	export default {
		data() {
			return {
				teamList: [],
				modalName: null,
			}
		},
		methods: {
			addClick() {
				uni.navigateTo({
					url: '/pages/team/add'
				})
			},

			loadData() {
				list().then(res => {
					if (res.success) {
						this.teamList = res.data
					} else {}
				})
			},
			editTeam(item) {
				uni.navigateTo({
					url: '/pages/team/edit?teamInfo=' + encodeURIComponent(JSON.stringify(item))
				})
			},
			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
			},

			// ListTouch计算方向
			ListTouchMove(e) {
				this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left'
			},

			// ListTouch计算滚动
			ListTouchEnd(e) {
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			},
			deleteTeam(item) {
				const arr = []
				arr.push(item)
				deleteTeam(arr).then(res => {
					if (res.success) {
						this.$refs.uToast.show({
							type: 'success',
							message: "删除成功",
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
						})
						this.removeTeamInfo(item)
				
					}else{
						this.$refs.uToast.show({
							type: 'error',
							message: res.message,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
						})
					}
				}) 
			},
			removeTeamInfo(item) {
				const index = this.teamList.findIndex(ele => {
					return ele.id == item.id
				})
				this.teamList.splice(index, 1)
			}
		},
		onLoad() {
			this.loadData()
			uni.$on('loadData', () => {
				this.loadData();
			})
		}
	}
</script>

<style>
	.cu-form-group {
		min-height: 0;
		border: none;

	}
</style>
