<template>
	<view class="add">
		<!-- 设备信息 -->
		<view class="main">
			<u--form labelPosition="left" labelAlign="center" :model="equip" ref="form1">
				<u-form-item label="设备名称" prop="name" labelWidth="auto" borderBottom ref="item1">
					<u--input v-model="equip.name" border="none" placeholder="请输入设备名称" :readonly="equip.eid!=null"></u--input>
				</u-form-item>
				<u-form-item label="设备编号" prop="eno" labelWidth="auto" borderBottom v-if="equip.eid">
					<u--input v-model="equip.eno" border="none" readonly></u--input>
				</u-form-item>
				<u-form-item label="出库价格" prop="outPrice" labelWidth="auto" borderBottom>
					<u--input v-model="equip.outPrice" border="none" type="number" placeholder="请输入出库价格">
					</u--input>
				</u-form-item>
				<u-form-item label="库存预警" prop="warning" labelWidth="auto" borderBottom>
					<u--input v-model="equip.warning" type="number" border="none" placeholder="请输入库存预警"></u--input>
				</u-form-item>
				<u-form-item label="规格" prop="spec" labelWidth="auto" borderBottom>
					<u--textarea v-model="equip.spec" placeholder="请输入规格" confirmType="done"></u--textarea>
				</u-form-item>
				
				<u-form-item label="存放位置" prop="positionName" labelWidth="auto" borderBottom @click="showPostion = true">
					<u--input v-model="equip.positionName" border="none" placeholder="请选择存放位置"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
					<u-picker :show="showPostion" :columns="columns" keyName="name"  @cancel='showPostion=false' @confirm='confirm'></u-picker>
				</u-form-item>
				<u-form-item label="供应商" prop="supplierName" labelWidth="auto" borderBottom @click="showSupplier = true">
					<u--input v-model="equip.supplierName" border="none" placeholder="请选择供应商"></u--input>
					<u-icon slot="right" name="arrow-right"></u-icon>
					<u-picker :show="showSupplier" :columns="columns2" keyName="name"  @cancel='showSupplier=false' @confirm='confirm2'></u-picker>
				</u-form-item>
				<u-form-item label="备注" prop="remark" labelWidth="auto" borderBottom ref="item1">
					<u--textarea v-model="equip.remark" placeholder="请输入备注信息" confirmType="done"></u--textarea>
				</u-form-item>
			</u--form>

		</view>

		<view class="submit" v-if="equip.eid">
			<u-button type="primary" text="修 改 设 备" @click="submit"></u-button>
		</view>
		<view class="submit" v-else>
			<u-button type="primary" @click="submit" text="添 加 设 备"></u-button>
		</view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				show: true,
				equip: {
					eid: null,
					eno: null,
					name: null,
					outPrice: null,
					positionId: null,
					positionName: null,
					remark: null,
					spec: null,
					supplierId: null,
					supplierName: null,
					warning: null
				},
				pickerTime: Number(new Date()),
				showSupplier: false,
				showPostion: false,
				positionList: [],
				supplierList: [],
				rules: {
					name: [
						{ 
							required: true, 
							message: '请输入设备姓名', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					spec: [
						{
							required: true,
							message: '请输入规格', 
							trigger: ['change','blur'],
						}
					],
					positionName: [
						{
							required: true,
							message: '请选择存放位置', 
							trigger: ['change','blur'],
						}
					],
					supplierName: [
						{
							required: true,
							message: '请选择供应商', 
							trigger: ['change','blur'],
						}
					]
				},
				columns: [],
				columns2: []

			}
		},

		onLoad: function(options) {
			if (options.equip) {
				let data = JSON.parse(options.equip);
				this.equip = data
			}
			this.getPositionList()
			this.getSupplierList()
		},

		created() {},
		filters: {},

		methods: {
			async getPositionList() {
				const {
					data: res
				} = await this.$http('/app/positionList');
				if (res.code) return
				this.positionList = res.data;
				this.columns.push(this.positionList)
			},
			async getSupplierList() {
				const {
					data: res
				} = await this.$http('/app/supplierList');
				if (res.code) return
				this.supplierList = res.data;
				this.columns2.push(this.supplierList)
			},
			submit(){
				if(this.equip.eid){
					// 修改
					this.updateEquip();
				}else{
					this.addEquip();
				}
			},
			// 添加设备
			async addEquip() {
				let flag = true;
				if(this.equip.name==null || this.equip.name=='' ){
					flag = false;
				}
				if(this.equip.outPrice == null || this.equip.outPrice==''){
					flag = false;
				}
				if(this.equip.positionId == null || this.equip.positionId==''){
					flag = false;
				}
				if(this.equip.spec == null || this.equip.spec==''){
					flag = false;
				}
				if(this.equip.supplierId == null || this.equip.supplierId==''){
					flag = false;
				}
				if(this.equip.warning == null || this.equip.warning==''){
					flag = false;
				}
				if(!flag) return uni.showToast({
					title: '请补充设备内容',
					icon: 'error'
				})
				const {
					data: res
				} = await this.$http('/app/addEquip', this.equip)
				if (res.code == 0) {
					uni.showToast({
						title: '添加设备成功',
						icon: 'success'
					});
					this.reset();
					// 设备添加完成返回 设备列表页面
					wx.navigateTo({
						url: 'list',
					})
				}else{
					// 添加失败
					uni.showToast({
						title: res.data.msg,
						icon: 'error'
					})
				}
				
			},
			// 修改设备
			async updateEquip() {
				let flag = true;
				if(this.equip.name==null || this.equip.name=='' ){
					flag = false;
				}
				if(this.equip.outPrice == null || this.equip.outPrice==''){
					flag = false;
				}
				if(this.equip.positionId == null || this.equip.positionId==''){
					flag = false;
				}
				if(this.equip.spec == null || this.equip.spec==''){
					flag = false;
				}
				if(this.equip.supplierId == null || this.equip.supplierId==''){
					flag = false;
				}
				if(this.equip.warning == null || this.equip.warning==''){
					flag = false;
				}
				if(!flag) return uni.showToast({
					title: '请补充设备内容',
					icon: 'error'
				})
				const {
					data: res
				} = await this.$http('/app/updateEquip', this.equip)
				console.log(res);
				if (res.code == 0) {
					
					// this.reset();
					// 设备修改完成返回 设备列表页面
					// wx.navigateTo({
					// 	url: 'list',
					// })
					uni.showToast({
						title: '设备修改成功',
						icon: 'success'
					});
				}else{
					// 修改失败
					uni.showToast({
						title: "修改失败！",
						icon: 'error'
					})
				}
				
			},
			confirm(d){
				this.equip.positionName = d.value[0].name
				this.equip.positionId = d.value[0].id
				this.showPostion = false;
			},
			confirm2(d){
				this.equip.supplierName = d.value[0].name
				this.equip.supplierId = d.value[0].id
				this.showSupplier = false;
			},
			reset() {
				this.equip = {
					eid: null,
					eno: null,
					name: null,
					outPrice: null,
					positionId: null,
					positionName: null,
					remark: null,
					spec: null,
					supplierId: null,
					supplierName: null,
					warning: null
				}
			},
		}
	}
</script>

<style lang="scss">
	.submit {
		margin: 1vh 2.667vw;
	}

	.add {
		background: #f6f6f6;
		padding: 2.667vw;
		height: 95vh;

		.main {
			background: #ffffff;
			margin-bottom: 2.667vw;
			padding: 2vw;
			border-radius: 8px;

			.u-form-item__body__right {
				padding-left: 20vw;
			}

			.u-button {
				border: none !important;
				padding: 0 !important;
				justify-content: flex-start;
			}
		}
	}
</style>
