<template>
	<view class="content">
		<nav-bar title="指令操作"></nav-bar>
		<view class="search-confirm-wrap">
			<view class="search-wrap">
				<view class="search-item">
					<label class="search-label">生产区域</label>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index2" :localdata="options2" @change="selectChange2"></uni-data-select>
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">状态</label>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">指令号</label>
					<view class="filter_input_wraper">
						<input type="text" class="search-input-l" placeholder="指令号、MES号" v-model="instNum">
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">任务描述</label>
					<view class="filter_input_wraper">
						<input type="text" class="search-input-l" placeholder="车号" v-model="taskName">
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">条码</label>
					<view class="filter_input_wraper">
						<search-box
						placeholder="入箱条码、出箱条码"
						v-model="val1"
						/>
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">起点</label>
					<view class="filter_input_wraper">
						<input type="text" class="search-input-l" placeholder="起始点位、起始设备" v-model="startPoint">
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">终点</label>
					<view class="filter_input_wraper">
						<input type="text" class="search-input-l" placeholder="目的点位、目的设备" v-model="endPoint">
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">开始日期</label>
					<view class="filter_input_wraper">
						<input type="text" class="search-input-l" v-model="startDate"  @click="open1">
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">结束日期</label>
					<view class="filter_input_wraper">
						<input type="text" class="search-input-l" v-model="endDate"  @click="open2">
					</view>
				</view>
			</view>
			<view class="confirm-button-wrap">
				<button class="confirm-button" @tap="toSearch()">查询</button>
				<button class="confirm-button" :disabled="disabled" @tap="toSure('8')">取消</button>
				<button class="confirm-button" :disabled="disabled" @tap="toSure('7')">完成</button>
				<button class="confirm-button" :disabled="disabled" @tap="toSure('5')">重发</button>
			</view>
		</view>
		<view class="grid-wrap">
			<table class="grid-table">
				<thead>
					<tr>
						<th>选择</th>
						<th>任务编号</th>
						<th>任务类型</th>
						<th>任务描述</th>
						<th>起点设备</th>
						<th>目的设备</th>
						<th>料箱码</th>
						<th>状态</th>
						<th>物料编码</th>
						<th>创建时间</th>
					</tr>
				</thead>
				<tbody>
					<tr v-for="e in dataList" :key="e.instruct_uuid"  @click="toRadio(e)">
						<td>
							<view class="iconfont icon-check" :class="{'icon-checked': pkId === e.instruct_uuid}"></view>
						</td>
						<td>{{e.instructoperate_num}}</td>
						<td>{{e.task_type}}</td>
						<td>{{e.task_name}}</td>
						<td>{{e.startpoint_code}}</td>
						<td>{{e.nextpoint_code}}</td>
						<td>{{e.vehicle_code}}</td>
						<td>{{e.status_name}}</td>
						<td>{{e.processmaterial_code}}</td>
						<td>{{e.create_time}}</td>
					</tr>
				</tbody>
			</table>
		</view>
		<uni-load-more color="#007AFF" iconType="circle" :status="status" :icon-size="14" :content-text="contentText" v-if="dataList.length > 0"/>
		<view>
			<uni-calendar 
			ref="calendar1"
			:insert="false"
			:date="startDate"
			@confirm="confirm1"
			/>
		</view>
		<view>
			<uni-calendar 
			ref="calendar2"
			:insert="false"
			:date="endDate"
			@confirm="confirm2"
			/>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {instStatusQuery, instPageQuery, instOperation} from '@/utils/getData1.js'
	import {dateFtt} from '@/utils//utils.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				page: 1,
				options1: [],
				index1: '',
				options2: [{text: 'A1', value: 'A1'}, {text: 'A2', value: 'A2'}],
				index2: 'A1',
				startDate: dateFtt(new Date()),
				endDate: dateFtt(new Date(new Date().setDate(new Date().getDate() + 1))),
				instNum: '',
				taskName: '',
				startPoint: '',
				endPoint: '',
				val1: '',
				dataList: [],
				pkId: '',
				disabled: false,
				reload: false,
				status: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				totalCount: 0,
				pageNum: 1,
				pageSize: 100
			};
		},
		created() {
			this._instStatusQuery()
		},
		methods: {
			open1(){
				this.$refs.calendar1.open()
			},
			confirm1(e) {
				var compare = Date.parse(new Date(e.fulldate)) - Date.parse(new Date(this.endDate))
				if (compare < 0) {
					this.startDate = e.fulldate
				}
			},
			open2(){
				this.$refs.calendar2.open()
			},
			confirm2(e) {
				var compare = Date.parse(new Date(e.fulldate)) - Date.parse(new Date(this.startDate))
				if (compare > 0) {
					this.endDate = e.fulldate
				}
			},
			/** 选择器1 */
			selectChange1(e) {
				this.index1 = e
			},
			/** 选择器1 */
			selectChange2(e) {
				this.index2 = e
			},
			async _instStatusQuery (id) {
				let res = await instStatusQuery(id)
				this.options1 = [...res]
			},
			toSearch () {
				this.dataList = []
				this.pageNum = 1
				this._instPageQuery()
			},
			/** 初始化查询 */
			async _instPageQuery () {
				let form = {
					page: this.pageNum + '',
					size: this.pageSize + '',
					status: this.index1,
					inst_num: this.instNum,
					task_name: this.taskName,
					task_type: this.taskName,
					product_area: this.taskName,
					vehicle_code: this.val1,
					start_point: this.startPoint,
					end_point: this.endPoint,
					start_date: this.startDate || '',
					end_date: this.endDate || ''
				}
				let res = await instPageQuery(form)
				this.totalCount = res.size
				if (res.size > 0) {
					const dataMap = res.content
					this.dataList = this.reload ? dataMap : this.dataList.concat(dataMap)
					this.reload = false
				} else {
					this.dataList = []
				}
				if (this.totalCount == this.dataList.length) {
					this.reload = false
					this.status = 'noMore'
				}
				
			},
			onReachBottom () {
				if (this.totalCount > this.dataList.length) {
					this.status = 'loading'
					setTimeout(() => {
						this.pageNum++
						this._instPageQuery()
					}, 1000)
				} else { //停止加载
					this.status = 'noMore'
				}
			},
			async toSure (type) {
			  this.disabled = true
			  if (!this.pkId) {
				uni.showToast({
					title: '请选择',
					icon: 'none'
				})
				this.disabled = false
				return
			  }
			  try {
				let res = await instOperation(this.pkId, type)
				this.disabled = false
				this.toSearch()
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled = false
			  }
			},
			toRadio (e) {
			  this.pkId = this.pkId === e.instruct_uuid ? '' : e.instruct_uuid
			}
		}
	}
</script>

<style lang="stylus" scoped>
@import '../../common/style/mixin.styl';
.grid-wrap
	height: auto
	overflow: auto
	height: calc(100% - 337px); /** 42+ 15*8+ 35*5 */
/deep/ .uni-calendar-item__weeks-box-item
	height: 40px !important
</style>