<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 workshopList" :key="index">
					<view class="content" @click="editWorkshop(item)">
						<view class="cu-form-group">
							<view class="title" style="min-width: calc(6em + 15px);">制衣生产车间</view>
							<text class="text-grey" style="flex: 1;">{{item.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>
					<view class="move">
						<view class="bg-grey" @click="editWorkshop(item)">修改</view>
						<view class="bg-red" @click="deleteWorkshop(item)">删除</view>
					</view>
				</view>
			</view>
			<u-toast ref="uToast" />
		</scroll-view>
	</view>
</template>

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

			loadData() {
				list().then(res => {
					if (res.success) {
						this.workshopList = res.data
					} else {}
				})
			},
			editWorkshop(item) {
				uni.navigateTo({
					url: '/pages/workshop/edit?workshopInfo=' + 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
			},
			deleteWorkshop(item) {
				const arr = []
				arr.push(item)
				deleteWorkshop(arr).then(res => {
					if (res.success) {
						this.$refs.uToast.show({
							type: 'success',
							message: "删除成功",
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
						})
						this.removeWorkshopInfo(item)
					}else{
						this.$refs.uToast.show({
							type: 'error',
							message: res.message,
							iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
						})
					}
				})
			},
			removeWorkshopInfo(item) {
				const index = this.workshopList.findIndex(ele => {
					return ele.id == item.id
				})
				this.workshopList.splice(index, 1)
			}
		},
		onLoad() {
			this.loadData()
			uni.$on('loadData', () => {
				this.loadData();
			})
		}
	}
</script>

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

	}
</style>
