<template>
	<view class="page-wrap">
		<view class="search-bar">
			<uni-icons type="settings-filled" color="#2979ff" size="28" @click="show = true" />
			<u-popup :show="show" mode="top" :round="10" @close="close" @open="open">
				<view class="filter-form">
					<view class="filter-row">
						<text class="filter-field-title">作业环节</text>
						<u--input
							placeholder="请输入作业环节"
							v-model="filterModel.operationStep.NAME"
							customStyle="height: 20px"
							fontSize="13px"
						></u--input>
					</view>
					<view class="filter-row"	@click.stop="handleShowPicker({title: '任务类型', name: 'TASK_LEVEL'})">
						<text class="filter-field-title">任务类型</text>
						<u--input
								placeholder="选择任务类型"
								suffixIcon="arrow-down"
								customStyle="height: 20px"
								fontSize="13px"
								v-model="filterModel.TASK_LEVEL.NAME"
								suffixIconStyle="color: #909399"
						></u--input>
					</view>
					<view class="filter-row"	@click.stop="handleShowPicker({title: '是否关键任务', name: 'IS_IMPORTANT'})">
						<text class="filter-field-title">是否关键任务</text>
						<u--input
								suffixIcon="arrow-down"
								customStyle="height: 20px"
								fontSize="13px"
								v-model="filterModel.IS_IMPORTANT.NAME"
								suffixIconStyle="color: #909399"
						></u--input>
					</view>
					<view class="filter-row"	@click.stop="handleShowPicker({title: '许可作业审批层级', name: 'AUDIT_LEVEL'})">
						<text class="filter-field-title">许可作业审批层级</text>
						<u--input
								suffixIcon="arrow-down"
								customStyle="height: 20px"
								fontSize="13px"
								v-model="filterModel.AUDIT_LEVEL.NAME"
								suffixIconStyle="color: #909399"
						></u--input>
					</view>
					<view class="filter-row"	@click.stop="handleShowPicker({title: '状态', name: 'STATUS'})">
						<text class="filter-field-title">状态</text>
						<u--input
								suffixIcon="arrow-down"
								customStyle="height: 20px"
								fontSize="13px"
								v-model="filterModel.STATUS.NAME"
								suffixIconStyle="color: #909399"
						></u--input>
					</view>
					<view class="filter-row"	@click.stop="handleShowPicker({title: '作业周期', name: 'CYCLE_TYPE'})">
						<text class="filter-field-title">作业周期</text>
						<u--input
								suffixIcon="arrow-down"
								customStyle="height: 20px"
								fontSize="13px"
								v-model="filterModel.CYCLE_TYPE.NAME"
								suffixIconStyle="color: #909399"
						></u--input>
					</view>
					<view class="filter-row">
						<text class="filter-field-title">岗位</text>
						<u--input
							placeholder="岗位"
							customStyle="height: 20px"
							fontSize="13px"
							v-model="filterModel.POST.NAME"
						></u--input>
					</view>
					<view class="btn-groups">
						<button @click="handleReset" size='mini' type="default">重置</button>
						<button @click="handleSearch" size='mini' type="primary">查询</button>
					</view>
				</view>
			</u-popup>
			<u-picker
			  :show="comPickerInfo.showPicker"
			  :columns="comPickerInfo.columns"
			  @confirm="onConfirmPicker"
			  @close="closePicker"
			  @cancel="closePicker"
			  keyName="NAME"
			></u-picker>
		</view>
		<view class="list-wrap">
			<common-card v-if="lists.length" :titleIcon="true" :dataSource="item" v-for="item in lists" @click="viewDetail(item.ID)">
				<view slot="title" style="width: 100%">
					<view class="card-title">
						<view class="left">
							<uni-icons class="title-icon" type="gear-filled" :size="20" color="#297aff" />
							<text class="content-text">
								{{item.Nav_OperationStep.NAME}}
							</text>
						</view>
						<!-- <view class="more">
							<uni-icons class="more-icon" type="forward" :size="16" />
						</view> -->
					</view>
				</view>
				<view class="content">
<!--					<view class="field">-->
<!--						<text class="left">作业环节：</text>-->
<!--						<text class="value">{{item.Nav_OperationStep.NAME}}</text>-->
<!--					</view>-->
<!--					<view class="field">-->
<!--						<text class="left">是否关键任务：</text>-->
<!--						<text class="value">{{item.IS_IMPORTANT === 1 ? '是' : '否'}}</text>-->
<!--					</view>-->
					<view class="field">
						<text class="left">作业许可类别：</text>
						<text class="value">{{item.Nav_WorkPermitType.NAME}}</text>
					</view>
					<view class="field">
						<text class="left">许可作业审批层级：</text>
						<text class="value">{{numConversionZh(item.AUDIT_LEVEL)}}</text>
					</view>
					<view class="field">
						<text class="left">任务类型：</text>
						<text class="value">{{typeConversionText(item.TASK_LEVEL)}}</text>
					</view>
<!--					<view class="field">-->
<!--						<text class="left">作业周期：</text>-->
<!--						<text class="value">{{cycleConversionText(item.CYCLE_TYPE)}}</text>-->
<!--					</view>-->
<!--					<view class="field">-->
<!--						<text class="left">状态：</text>-->
<!--						<text class="value">{{item.STATUS === 0 ? '启用' : '停用'}}</text>-->
<!--					</view>-->
<!--					<view class="field">-->
<!--						<text class="left">岗位：</text>-->
<!--						<text class="value">{{item.POST}}</text>-->
<!--					</view>-->
<!--					<view class="field">-->
<!--						<text class="left">生产单元：</text>-->
<!--						<text class="value">{{item.Nav_Unit.NAME || ''}}</text>-->
<!--					</view>-->
				</view>
			</common-card>
			<u-empty v-else text="暂无数据" icon="/static/empty@2x.png">
			</u-empty>
		</view>
	</view>
</template>

<script>
	import {
		initFilter,
		extendRule,
		extendInclude
	} from '../../../../utils/common'

	import {
		getOperationLinkFullOrderPaged
	} from '../../../../services/apply/HMServices/HMServices.js'

	export default {
		data() {
			return {
				lists: [],
				show: false,
				pageIndex: 1,
				total: 0,
				filterModel: {
					operationStep: {},
					TASK_LEVEL: {},
					IS_IMPORTANT: {},
					STATUS: {},
					AUDIT_LEVEL: {},
					CYCLE_TYPE: {},
					POST: {}
				},
				comPickerInfo: {
					showPicker: false,
					columns: [],
					title: '',
					name: ''
				}
			}
		},
		onLoad(option) {
			this.loadData()
		},
		methods: {
			handleSearch() {
				this.loadData()
				this.show = false
				this.comPickerInfo.showPicker = false
			},
			handleReset() {
				this.filterModel = {
					operationStep: {},
					TASK_LEVEL: {},
					IS_IMPORTANT: {},
					STATUS: {},
					AUDIT_LEVEL: {},
					CYCLE_TYPE: {},
					POST: {}
				}
				this.loadData()
				this.show = false
				this.comPickerInfo.showPicker = false
			},
			close() {
				this.show = false
			},
			open() {

			},
			viewDetail(id) {
				uni.navigateTo({
					url: '/pages/apply/subPages/BS/workTaskView?id=' + id
				})
			},
			onConfirmPicker(e) {
				const { name } = this.comPickerInfo
				this.filterModel[name] = {
					NAME: e.value[0].NAME,
					VALUE: e.value[0].ID
				}
				this.comPickerInfo.showPicker = false
			},
			handleShowPicker(val) {
				let column = []
				if (val.name === 'TASK_LEVEL') {
				  column = [
				    {NAME: '一般任务', ID: 1},
				    {NAME: '关键任务非许可作业', ID: 2},
				    {NAME: '许可作业', ID: 3},
				  ]
				}
				if (val.name === 'IS_IMPORTANT') {
				  column = [
				    {NAME: '否', ID: 0},
				    {NAME: '是', ID: 1}
				  ]
				}
				if (val.name === 'AUDIT_LEVEL') {
				  column = [
				    {NAME: '无审批层级', ID: 0},
				    {NAME: '一级审批', ID: 1},
				    {NAME: '二级审批', ID: 2},
				    {NAME: '三级审批', ID: 3},
				    {NAME: '四级审批', ID: 4},
				  ]
				}
				if (val.name === 'STATUS') {
				  column = [
				    {NAME: '启用', ID: 0},
				    {NAME: '停用', ID: 1}
				  ]
				}
				if (val.name === 'CYCLE_TYPE') {
				  column = [
				    {NAME: '需求作业', ID: 0},
				    {NAME: '每日作业', ID: 10}
				  ]
				}
				if (column.length) {
				  this.comPickerInfo = {
				    showPicker: true,
				    title: val.title,
				    name: val.name,
				    formIndex: val.formIndex,
				    columns: [column]
				  }
				}
			},
			handleTab(t) {
				this.currentTab = t
			},
			async loadData() {
				const orgId = uni.getStorageSync('orgId')
				const json = initFilter(orgId, "", "Nav_OperationStep.NAME", 0, this.pageIndex)
				Object.keys(this.filterModel).forEach(i => {
					if (Reflect.has(this.filterModel[i], 'NAME')) {
						if (i === 'operationStep') {
							extendRule(json, 'Nav_OperationStep.NAME', 9, this.filterModel[i].NAME)
						}
						if (i === 'POST') {
							extendRule(json, i, 9, this.filterModel[i].NAME)
						}
						if (i === 'TASK_LEVEL' || i === 'IS_IMPORTANT' || i === 'AUDIT_LEVEL' || i === 'STATUS' || i === 'CYCLE_TYPE') {
							if (this.filterModel[i].VALUE !== undefined) {
								extendRule(json, i, 1, this.filterModel[i].VALUE)
							}
						}
					}
				})
				extendInclude(json, "Nav_LinkPost.Nav_Post")
				extendInclude(json, "Nav_OperationStep")
				extendInclude(json, "Nav_WorkPermitType")
				extendInclude(json, "Nav_Unit")
				json.SelectField = [
					"Nav_LinkPost.Nav_Post.NAME",
					"Nav_OperationStep.NAME",
					"IS_IMPORTANT",
					"Nav_WorkPermitType.NAME",
					"AUDIT_LEVEL",
					"NAME",
					"TASK_LEVEL",
					"CYCLE_TYPE",
					"STATUS",
					"POST",
					"Nav_Unit.NAME",
					"ID",
					"ORG_ID"
				]
				getOperationLinkFullOrderPaged(json).then(res => {
					if (res.IsSuccessful) {
						this.total = res.TotalCount
						const newRes = res.Data.map(item => {
							return {
								title: item.Nav_OperationStep.NAME,
								Nav_Unit: {},
								Nav_WorkPermitType: {},
								...item
							}
						})
						if (this.pageIndex !== 1) {
						  this.lists = this.lists.concat(newRes)
						} else {
						  this.lists = newRes
						}
					}
				})
			},
			closePicker() {
				this.comPickerInfo = {
					showSheet: false,
					columns: [],
					title: '',
					name: '',
					formIndex: undefined
				}
			},
			numConversionZh(num) {
				const zh = ['', '一', '二', '三', '四', '五', '六', '七', '八']
				if (num === 0) {
					return '无审批层级'
				}
				return zh[num] + '级审批'
			},
			typeConversionText(type) {
				const text = ['', '一般任务', '关键任务非许可作业', '许可作业']
				return text[type]
			},
			cycleConversionText(cycle) {
				switch(cycle) {
					case 0:
					return '需求作业';
					case 10:
					return '每日作业';
					default:
					return ''
				}
			}
		},
		onReachBottom() {
			if (this.total > this.pageIndex * 10) {
				this.pageIndex++
			}
		},
		watch: {
			pageIndex() {
				this.loadData()
			}
		},
		computed: {}
	}
</script>


<style scoped>
	@import url("../../../../style/css/editTemplate.css");
	.filter-form {
		padding: 16px;
	}
	.filter-row {
		margin-bottom: 8px;
	}
	.filter-field-title {
		font-size: 14px;
		color: #8f9ca2;
		margin-bottom: 8px;
		display: flex;
		flex-direction: row;
	}
	.search-bar {
		margin-bottom: 10px;
		text-align: right;
	}
	.btn-groups {
		display: flex;
		justify-content: space-between;
	}
	.btn-groups button {
		width: 48%;
	}
	.content-list {
		padding: 10px 0;
	}

	.content .field {
		font-size: 14px;
		line-height: 18px;
		margin-bottom: 4px;
	}

	.content .field .label {
		color: #666;
	}

	.content .field .value {
		color: #333333;
	}

	.card-title {
		margin-bottom: 6px;
		display: flex;
		font-weight: bold;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		font-size: 16px;
	}

	.card-title .left {
		display: flex;
		align-items: center;
	}

	.card-title .title-icon {
		margin-right: 4px;
	}
</style>
