<template>
	<view class="zd_container">
		<nav-bar title="设备作业管理"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label">作业类型</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">设备</span>
					</view>
					<view class="filter_input_wraper">
						<search-box
						v-model="val1"
						@handleChange="handleChange"
						/>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>选择</th>
								<th>作业类型</th>
								<th>作业单号</th>
								<th>设备编号</th>
								<th>设备名称</th>
								<th>状态</th>
								<th>计划开始日期</th>
								<th>计划结束日期</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" :class="{'checked': e.checked}">
								<td><span class="iconfont icon_unchecked" :class="{'icon_checked': e.checked}" @tap="toCheck(e)">&#xe66b;</span></td>
								<td>{{['维修单', '保养单', '点检单', '润滑单'][Number(e.job_type) - 1]}}</td>
								<td class="c_link" @tap="toJump(e)">{{e.job_code}}</td>
								<td>{{e.device_code}}</td>
								<td>{{e.device_name}}</td>
								<td>结束</td>
								<td>{{e.plan_start_date}}</td>
								<td>{{e.plan_end_date}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="submit-bar">
			<button class="submit-button" :class="{'btn-disabled': checkArr.length === 0}" :disabled="disabled" @tap="toSure">审核完成</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {deviceManageGetAllQuery, deviceManageConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				options: [{text: '维修单', value: '1'}, {text: '保养单', value: '2'}, {text: '点检单', value: '3'}, {text: '润滑单', value: '4'}],
				index: '',
				val1: '',
				dataList: [],
				checkArr: [],
				disabled: false
			};
		},
		created () {
			this._deviceManageGetAllQuery(this.val1)
		},
		methods: {
			selectChange (e) {
				this.index = e
				if (e) {
					this._deviceManageGetAllQuery(this.val1)
				}
			},
			handleChange (e) {
				this._deviceManageGetAllQuery(e)
			},
			/** grid查询 */
			async _deviceManageGetAllQuery (e) {
				let res = await deviceManageGetAllQuery(this.index, e)
				res.data.map(el => {
					this.$set(el, 'checked', false)
				})
				this.dataList = [...res.data]	
			},
			/** 确认 */
			async toSure () {
				this.disabled = true
				if (this.checkArr.length === 0) {
					this.disabled = false
					return
				}
				try {
					let res = await deviceManageConfirm(this.checkArr)
					this.disabled = false
					this.checkArr = []
					this._deviceManageGetAllQuery(this.val1)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			toCheck (e) {
				e.checked = !e.checked
				this.checkArr = this.dataList.filter(i => { return i.checked === true })
			},
			toJump (e) {
				this.$store.dispatch('setPublicObj', e)
				uni.navigateTo({
					url: '/pages/device/workDetail'
				})
			}
		}
	}
</script>

<style lang="stylus" scoped>
.c_link
	color #ff6a00
	cursor pointer
</style>
