<template>
	<view class="page">
		<!-- 反馈内容 -->
		<view class="feedback-data">
			<view class="titleall-box">
				<view class="titleall-left">
					<view class="titleall-left-line"></view>企业基础情况填报
				</view>
			</view>
			<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" style="width: 96%;margin: 0 auto;">
				<uni-forms-item label="公司名称" required name="enterpriseName">
					<uni-easyinput v-model="valiFormData.enterpriseName" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="行业类型" required name="industry">
					<uni-easyinput v-model="valiFormData.industry" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="公司法人" required name="legalPerson">
					<uni-easyinput v-model="valiFormData.legalPerson" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="公司联系人" required name="contacts">
					<uni-easyinput v-model="valiFormData.contacts" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="联系人电话" required name="contactsPhone">
					<uni-easyinput v-model="valiFormData.contactsPhone" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="从业人数" required name="employees">
					<uni-easyinput v-model="valiFormData.employees" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="总产值" required name="totalOutputValue">
					<uni-easyinput v-model="valiFormData.totalOutputValue" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="销售收入" required name="salesRevenue">
					<uni-easyinput v-model="valiFormData.salesRevenue" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="固投" required name="fixedInvestment">
					<uni-easyinput v-model="valiFormData.fixedInvestment" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="税收" required name="taxation">
					<uni-easyinput v-model="valiFormData.taxation" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="利润" required name="profit">
					<uni-easyinput v-model="valiFormData.profit" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="进口总额" name="imports">
					<uni-easyinput v-model="valiFormData.imports" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="出口总额" name="totalExportValue">
					<uni-easyinput v-model="valiFormData.totalExportValue" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="主要产品" required name="mainProducts">
					<uni-easyinput v-model="valiFormData.mainProducts" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="创建用户企业" required name="creatorUserEnterprise">
					<uni-easyinput v-model="valiFormData.creatorUserEnterprise" placeholder="请输入" />
				</uni-forms-item>
				<uni-forms-item label="企业规模" required>
					<uni-data-checkbox v-model="valiFormData.sex" :localdata="sexs" />
				</uni-forms-item>
				<!-- <uni-forms-item label="党的建设详细内容">
					<uni-easyinput type="textarea" v-model="valiFormData.bodyContent" placeholder="请输入" />
				</uni-forms-item> -->
			</uni-forms>
			<!-- 选择反馈类型 -->
			<!-- <view class="feedback-type">
				<view class="feedback-type-left">企业规模</view>
				<view class="feedback-type-right">
					<view class="title">
						<text>{{TypeArray[TypeIndex]}}</text>
					</view>
					<view class="picker">
						<picker @change="FeedbackTypeCh" :value="TypeIndex" :range="TypeArray">
								<view class="uni-input">{{TypeArray[TypeIndex]}}</view>
						</picker>
					</view>
					<view class="more">
						<image src="../../static/down1.png"></image>
					</view>
				</view>
			</view> -->
			<!-- <view class="example-body">
				<uni-file-picker limit="9" @select="select" title="信息图片"></uni-file-picker>
			</view> -->
		</view>

		<!-- 提交 -->
		<view v-if="have" class="submit-btn" @click="submitContent('valiForm')">
			<text>发布</text>
		</view>
		<view v-else class="submit-btn" @click="updateEvent('valiForm')">
			<text>保存</text>
		</view>
	</view>
</template>

<script>
	import {
		data
	} from '../../uview-ui/libs/mixin/mixin';
	import request from '@/utils/request.js'
	import utils from '../../service/utils';
	export default {
		data() {
			return {
				userCode: {},
				// 选择图片路径
				filePath: [],
				uploadPath: [],
				type: "",
				lastModifyUserId: '',
				// 用户公司ID
				companyId: '',
				// 校验表单数据
				valiFormData: {
					enterpriseName: '',
					industry: '',
					legalPerson: '',
					contacts: '',
					contactsPhone: "",
					employees: '',
					totalOutputValue: '',
					salesRevenue: '',
					fixedInvestment: '',
					taxation: '',
					profit: '',
					imports: '',
					totalExportValue: '',
					mainProducts: '',
					creatorUserEnterprise: '',
					sex:1,
				},
				creater: '',
				rules: {
					enterpriseName: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					industry: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					legalPerson: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					contacts: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					contactsPhone: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					employees: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					salesRevenue: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					fixedInvestment: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					taxation: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					profit: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					mainProducts: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					creatorUserEnterprise: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					totalOutputValue: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					imports: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
					totalExportValue: {
						rules: [{
							required: true,
							errorMessage: '不能为空'
						}]
					},
				},
				baseUrl: "https://www.dygxq-es.cn",
				// 判断是否传了id
				isId: '',
				have: false,
				detailData: [],
				TypeArray: ['规上', '规下'],
				TypeIndex: 0,
				sexs: [{
					text: '规上',
					value: 0
				}, {
					text: '规下',
					value: 1
				}],
			};

		},
		// onShow() {
		// 	this.getType()
		// },
		onLoad(options) {
			console.log("options", options)

			if (JSON.stringify(options) == "{}") {
				this.have = true
			} else {
				this.isId = JSON.parse(options.data)
				console.log("Id", this.isId)
				this.getDetail(this.isId)
			}
			this.getUser()
			let user = uni.getStorageSync('user')
			this.lastModifyUserId = user.userInfo.userId

		},
		methods: {
			/**
			 * 反馈类型
			 * @param {Object} val
			 */
			FeedbackTypeCh(val) {
				console.log(val);
				this.TypeIndex = val.detail.value;
			},
			// 获取上传状态
			select(e) {
				console.log('选择文件：', e)
				this.filePath = e.tempFilePaths
			},
			// 获取用户信息
			getUser() {
				if (uni.getStorageSync('user')) {
					this.userCode = uni.getStorageSync('user').userInfo
					this.companyId = this.userCode.organizeId
					this.valiFormData.creatorUserEnterprise = this.userCode.organizeName
					// this.userInfo = this.userCode.userAccount
					// this.userInfo = '123456'
					console.log('用户已登录！', this.userCode)
				} else {
					uni.showToast({
						title: '请登录',
						icon: 'none'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/login/index'
						})
					})
				}
			},
			// 提交内容
			submitContent(ref) {
				let that = this
				this.$refs[ref].validate().then(res => {
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})
					this.uploadPath = []
					setTimeout(function() {
						that.postAll()
					}, 1500)
				})
			},

			// 上传所有
			postAll() {
				console.log("进入POST")
				let that = this
				let enterpriseScale = this.TypeArray[this.TypeIndex]
				let sex = this.sexs[this.valiFormData.sex].text
				if (that.uploadPath) {
					console.log('jinru')
					// console.log(uni.getStorageSync('token'))
					var info = {
						enterpriseName: that.valiFormData.enterpriseName,
						industry: that.valiFormData.industry,
						legalPerson: that.valiFormData.legalPerson,
						contacts: that.valiFormData.contacts,
						contactsPhone: that.valiFormData.contactsPhone,
						employees: that.valiFormData.employees,
						totalOutputValue: that.valiFormData.totalOutputValue,
						salesRevenue: that.valiFormData.salesRevenue,
						fixedInvestment: that.valiFormData.fixedInvestment,
						taxation: that.valiFormData.taxation,
						profit: that.valiFormData.profit,
						imports: that.valiFormData.imports,
						totalExportValue: that.valiFormData.totalExportValue,
						mainProducts: that.valiFormData.mainProducts,
						creatorUserEnterprise: that.valiFormData.creatorUserEnterprise,
						enterpriseScale: sex,
						
					}
					console.log(info)
					this.API.postCompanyInfo(info).then(res => {
						console.log("1111", res);
						console.log("res", res);
						if (res.code == 200) {
							console.log("上传后的值", res)
							uni.showToast({
								icon: "success",
								title: "发布成功！",
								duration: 1500
							}).then(() => {
								uni.navigateTo({
									url: '/pages/datain/datain'
								})
							})
						}
					}).catch((err) => {
						console.log(err)
					})

					// 	request({
					// 		url: '/api/SubDev/basepartybuilding',
					// 		method: 'POST',
					// 		// header: {
					// 		// 	"Content-Type": "application/json;charset:utf-8"
					// 		// },
					// 		data: {
					// 			title: that.valiFormData.title,
					// 			content: that.valiFormData.bodyContent,
					// 			creatorUserId: that.userCode.userId,
					// 			summary: that.valiFormData.subTitle,
					// 			image: that.uploadPath,
					// 		}
					// 	}).then(res => {
					// 		console.log("res",res);
					// 		if (res.code == 200) {
					// 			console.log("上传后的值", res)
					// 			uni.showToast({
					// 				icon: "success",
					// 				title: "发布成功"
					// 			})
					// 		}
					// 	})

					// } else {
					// 	uni.showToast({
					// 		icon: "error",
					// 		title: "请选择图片"
					// 	})
					// }
					// setTimeout(function(){
					// 	console.log('需要', that.uploadPath)

					// },1000)
				}
			},
			// /**
			//  * 反馈类型
			//  * @param {Object} 
			//  */
			// FeedbackTypeCh(val) {
			// 	console.log(val);
			// 	this.TypeIndex = val.detail.value;
			// }

			// 获取详细信息
			getDetail(id) {
				let that = this
				request({
					url: `/api/SubDev/baseenterprisemanager/${id}`,
					method: 'get',
					data: {}
				}).then(res => {
					console.log(res)
					if (res.code === 200) {
						this.info = res.data
						that.valiFormData.enterpriseName = this.info.enterpriseName
						that.valiFormData.industry = this.info.industry
						that.valiFormData.legalPerson = this.info.legalPerson
						that.valiFormData.contacts = this.info.contacts
						that.valiFormData.contactsPhone = this.info.contactsPhone
						that.valiFormData.employees = this.info.employees
						that.valiFormData.totalOutputValue = this.info.totalOutputValue
						that.valiFormData.salesRevenue = this.info.salesRevenue
						that.valiFormData.fixedInvestment = this.info.fixedInvestment
						that.valiFormData.taxation = this.info.taxation
						that.valiFormData.profit = this.info.profit
						that.valiFormData.imports = this.info.imports
						that.valiFormData.totalExportValue = this.info.totalExportValue
						that.valiFormData.mainProducts = this.info.mainProducts
					}
				})
			},

			// 信息更新
			updateEvent(ref) {
				let that = this
				this.$refs[ref].validate().then(res => {
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})

					this.uploadPath = []
					console.log('需要', this.uploadPath)
					setTimeout(function() {
						that.updateData()
					}, 1500)
				})
			},
			// 更新接口
			updateData() {
				let that = this
				let enterpriseScale = this.TypeArray[this.TypeIndex]
				if (that.uploadPath) {
					console.log('jinru')
					// console.log(uni.getStorageSync('token'))
					console.log("id", this.isId)
					var info = {
						enterpriseName: that.valiFormData.enterpriseName,
						industry: that.valiFormData.industry,
						legalPerson: that.valiFormData.legalPerson,
						contacts: that.valiFormData.contacts,
						contactsPhone: that.valiFormData.contactsPhone,
						employees: that.valiFormData.employees,
						totalOutputValue: that.valiFormData.totalOutputValue,
						salesRevenue: that.valiFormData.salesRevenue,
						fixedInvestment: that.valiFormData.fixedInvestment,
						taxation: that.valiFormData.taxation,
						profit: that.valiFormData.profit,
						imports: that.valiFormData.imports,
						totalExportValue: that.valiFormData.totalExportValue,
						mainProducts: that.valiFormData.mainProducts,
						creatorUserEnterprise: that.companyId,
						enterpriseScale: enterpriseScale,
						id: this.isId
					}
					console.log(info)
					this.API.updateInfo(info).then(res => {
						console.log("1111", res);
						console.log("res", res);
						if (res.code == 200) {
							console.log("上传后的值", res)
							uni.showToast({
								icon: "success",
								title: "更新成功！"
							})
						}
					}).catch((err) => {
						console.log(err)
					})
				}
			}
		},
	}
</script>

<style scoped lang="scss">
	@import 'Feedback.scss';
</style>