<template>
	<lay-container fluid="true" style="padding: 10px">
		<lay-card>
			<lay-form :model="searchObj" class="search-box">
				<lay-row class="search-items">
					<lay-form-item label="PLM内码" prop="FPlmCode">
						<lay-input v-model="searchObj.FPlmCode"></lay-input>
					</lay-form-item>
					<lay-form-item label="版本号" prop="FVersionCode">
						<lay-input v-model="searchObj.FVersionCode"></lay-input>
					</lay-form-item>
					<lay-form-item label="试验号" prop="FTestCode">
						<lay-input v-model="searchObj.FTestCode"></lay-input>
					</lay-form-item>
					<lay-form-item label="配方描述" prop="FName">
						<lay-input v-model="searchObj.FName"></lay-input>
					</lay-form-item>
					<lay-form-item label="配方类型" prop="FType">
						<lay-select v-model="searchObj.FType">
							<lay-select-option v-for="(tIdv,tIdx) in typeList" :key="tIdx" :value="tIdv.FValue"
								:label="tIdv.FName"></lay-select-option>
						</lay-select>
					</lay-form-item>
				</lay-row>
				<lay-form-item class="search-btn">
					<lay-button size="sm" type="primary" @click="_clickSearch">搜索</lay-button>
					<lay-button size="sm" @click="_clickReset">重置</lay-button>
				</lay-form-item>
			</lay-form>
			<lay-table :columns="dataColumn" id="id" :dataSource="dataList" v-model:selectedKeys="selectedKeys"
				:checkbox="checkbox" :default-toolbar="defaultToolbar" :page="dataList.length>0?pageInfo:null"
				@change="changePage">
				<template v-slot:toolbar>
					<lay-button size="sm" @click="_getPageList(false,true)">刷新</lay-button>
					<lay-button size="sm" type="normal" @click="_dockSureBox(1)">SAP配方同步</lay-button>
					<!-- <lay-button size="sm" type="normal" @click="_clickSync()">PLM配方同步</lay-button> -->
					<lay-button size="sm" type="normal">PLM配方导入
						<lay-upload :url="uploadInfo.url" :data="uploadInfo.data" :headers="uploadInfo.headers"
							:acceptMime="uploadInfo.acceptMime" :accept="uploadInfo.accept" :exts="uploadInfo.exts"
							@done="uploadInfo.done" @before="uploadInfo.before" @error="uploadInfo.error"
							:auto-upload="true" :limit="1" :multiple="false"></lay-upload>
					</lay-button>
					<lay-button size="sm" type="primary" @click="_clickDown()">PLM配方模板</lay-button>
				</template>
				<template v-slot:FPlmCode="{ data }">
					{{ data.FPlmCode }}
				</template>
				<template v-slot:FVersionCode="{ data }">
					{{ data.FVersionCode }}
				</template>
				<template v-slot:FTestCode="{ data }">
					{{ data.FTestCode }}
				</template>
				<template v-slot:FName="{ data }">
					{{ data.FName }}
				</template>
				<template v-slot:FTypeName="{ data }">
					{{ data.FTypeName }}
				</template>
				<template v-slot:FProductCode="{ data }">
					{{ data.FProductCode }}
				</template>
				<template v-slot:FFactoryCode="{ data }">
					{{ data.FFactoryCode }}
				</template>
				<template v-slot:FAddDate="{ data }">
					{{ data.FAddDate }}
				</template>
				<template v-slot:FEditDate="{ data }">
					{{ data.FEditDate }}
				</template>
			</lay-table>
		</lay-card>
	</lay-container>
</template>

<style scoped>

</style>

<script>
	import {
		ref,
		watch
	} from "vue";

	import {
		getUploadInfo
	} from "/src/api/http";

	import {
		dockSapFormula
	} from "/src/api/api/formula";
	import {
		getBasicList,
		getPageList,
		getTempFile
	} from "/src/api/api/common";
	import '@layui/layui-vue/es/checkbox/index.css';

	export default {
		setup() {
			let dataColumn = [{
					title: "PLM内码",
					key: "FPlmCode"
				}, {
					title: "版本号",
					key: "FVersionCode",
				},
				{
					title: "试验号",
					key: "FTestCode"
				},
				{
					title: "配方描述",
					key: "FName"
				},
				{
					title: "配方类型",
					key: "FTypeName",
					width: "110px"
				},
				{
					title: "生产号",
					key: "FProductCode"
				},
				{
					title: "工厂代码",
					key: "FFactoryCode",
					width: "110px"
				},
				{
					title: "创建时间",
					key: "FAddDate",
					width: "182px",
					hide: true
				},
				{
					title: "最后修改时间",
					key: "FEditDate",
					width: "182px",
					hide: true
				}
			];
			dataColumn.forEach((item) => {
				item.customSlot = item.key;
				item.align = "center";
			});
			const selectedKeys = ref(["1"]);
			const checkbox = ref(false);
			const defaultToolbar = ref(true);
			const showEditBox = ref(false);
			const materialInfoObj = ref({});
			const pageInfo = ref({
				total: 0,
				limit: 10,
				current: 1
			});
			const typeList = ref([]);
			const dataList = ref([]);
			const editBoxTitle = ref("");
			const searchObj = ref({});
			const postData = ref({});
			const uploadInfo = ref({});
			return {
				materialInfoObj,
				editBoxTitle,
				showEditBox,
				selectedKeys,
				checkbox,
				defaultToolbar,
				dataColumn,
				dataList,
				pageInfo,
				searchObj,
				postData,
				typeList,
				uploadInfo
			};
		},
		mounted() {
			this._getTypeList();
			this._getPageList(true);
			this._getUploadInfo();
		},
		methods: {
			changePage(obj) {
				this.pageInfo.current = obj.current;
				this.pageInfo.limit = obj.limit;
				this._getPageList();
			},
			//上传配置
			_getUploadInfo() {
				let $this = this;
				this.uploadInfo = getUploadInfo({
					url: "/api/Formula/UploadTempFile?FType=1&FFuncType=1",
					accept: "file",
					acceptMime: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel",
					exts: "xlsx|xls",
					before: function(obj) {
						layer.msg('文件上传中...', {
							icon: 16,
							shade: 0.01,
							time: 0
						});
					},
					done: function(res) {
						setTimeout(() => {
							layer.closeAll();
							layer.msg("导入成功", {
								time: 1500,
								icon: 1
							});
							$this._getPageList(true);
						}, 300);
					},
					error: function(res) {
						layer.msg('网络异常，请稍后再试!', {
							icon: 2
						});
					}
				});
			},
			//获取种类
			async _getTypeList() {
				this.typeList = await getBasicList(37) || [];
			},
			//获取分页
			async _getPageList(isFirst) {
				//this.dataList = [];
				this.pageInfo.total = 0;
				if (isFirst) {
					this.pageInfo.current = 1;
					this.pageInfo.total = 0;
				}
				this.postData.FPageIndex = this.pageInfo.current;
				this.postData.FPageSize = this.pageInfo.limit;
				let result = await getPageList(this.postData, "Formula");
				result.Data.List = result.Data.List || [];
				result.Data.List.forEach((item) => {
					let type = this.typeList.find(s => s.FValue == item.FType);
					item.FTypeName = type == null ? item.FType : type.FName;
					item.FAddDate = item.FAddDate == null ? "" : this.dateFormat(item.FAddDate);
					item.FEditDate = item.FEditDate == null ? "" : this.dateFormat(item.FEditDate);
				});
				this.pageInfo.total = result.Data.Total || 0;
				this.dataList = result.Data.List;
			},
			//日期处理
			dateFormat(dataStr) {
				return dataStr.replace('T', ' ').split('.')[0];
			},
			//配方同步
			_clickSync() {
				layer.notifiy({
					title: "系统提示",
					content: "开发中，请稍等半月",
					time: 2000
				});
			},
			//模板下载
			async _clickDown() {
				let result = await getTempFile(1);
				window.open(result);
			},
			//检索-检索
			_clickSearch() {
				this.postData = {};
				for (let key in this.searchObj) {
					let val = this.searchObj[key];
					if (val != null && val != "") this.postData[key] = val;
				}
				this._getPageList(true);
			},
			//检索-重置
			_clickReset() {
				this.searchObj = {};
				this._clickSearch();
			},
			//对接-SAP
			async _clickSyncSap() {
				layer.closeAll();
				let idx = layer.load(2);
				try {
					let result = await dockSapFormula() || 0;
					layer.msg("操作成功，本次对接到" + result + "个新配方", {
						time: 2000,
						icon: 1
					});
				} catch (e) {}
				layer.close(idx);
			},
			//对接-执行
			_dockSureNext(type) {
				if (type == 1) this._clickSyncSap();
			},
			//对接-确认
			_dockSureBox(type) {
				let $this = this;
				layer.confirm("同步需要一定时间，您确认需要同步吗？", {
					title: "提示",
					btn: [{
							text: '同步',
							callback: function(id) {
								layer.close(id);
								$this._dockSureNext(type);
							}
						},
						{
							text: '取消',
							callback: function(id) {
								layer.close(id);
							}
						}
					]
				});
			}
		}
	}
</script>
