<template>
	<lay-form :model="dataInfo" class="dataInfo">
		<lay-form-item label="选择模式" prop="FCreateFactoryID" required label-width="120" style="margin-bottom:30px;">
			<lay-select v-model="dataInfo.FCreateFactoryID" placeholder="请选择">
				<lay-select-option v-for="(fIdv, fIdx) in factoryLists" key="fIdx" :value="fIdv.FID" :label="fIdv.FName"></lay-select-option>
			</lay-select>
			<p class="note">注：模式必选，通过模式确认视图类型</p>
		</lay-form-item>
		<lay-form-item label="请选择物料名称" prop="FSearchName" label-width="120" class="FSearchName">
			<lay-input v-model="dataInfo.FSearchName"></lay-input>
			<lay-button type="primary" @click="_searchPageList" class="search-btn2">检索</lay-button>
			<p class="note">注：检索该模式下的所有物料</p>
		</lay-form-item>
		<lay-form-item v-if="dataList.length > 0">
			<lay-table height="200px" :columns="dataColumn" v-model:selected-key="selectedKey" id="FID"
				:dataSource="dataList" :page="dataList.length > 0 ? pageInfo : null" @change="changePage">
			</lay-table>
		</lay-form-item>
	</lay-form>
	<div style="text-align: center;">
		<lay-form-item>
			<lay-button type="normal" @click="submitClick(true)">确认</lay-button>
			<lay-button @click="cancelClick">取消</lay-button>
		</lay-form-item>
	</div>
</template>

<style scoped>
.dataInfo {
	max-width: 90%;
	height: 600px;
	margin: 30px auto;
	padding-right: 30px;
	overflow-y: auto;
}

.search-btn2 {
	position: absolute;
	right: 0;
	top: 0;
}

.note2 {
	display: inline-block;
	margin-left: 16px;
}

.note3 {
	display: inline-flex;
	line-height: 38px;
	margin-left: 12px;
}

.note2,
.note {
	color: #999;
	font-size: 12px;
}

.note {
	margin-top: 4px;
	position: absolute;
	left: 0;
}

.note .layui-form-radio,
.note .layui-form-radio i {
	margin: 0 1px 0 0;
	padding: 0;
	font-size: 14px;
	margin-top: -2px;
	line-height: 0;
}

.FSearchName {
	color: var(--global-primary-color)
}
</style>
<script>
import {
	ref,
	watch
} from 'vue';
import {
	searchMaterialsByFactory,
	modifyMaterial
} from "/src/api/api/materialEdit"
import {
	getDataCode,
	getPageList,
	getBasicList,
	insertDataInfo,
	updateDataInfo,
	getDataModel
} from "/src/api/api/common";
import {
	getFactoryList
} from "/src/api/api/factory";
import { layer } from '@layui/layer-vue';

export default {
	setup() {
		const dataInfo = ref({
			FMaterialType: "ZMAT",
			FWeightUnit: "KG"
		});
		const formulaList = ref([]);
		const seletList = ref([
			[],
			[]
		]);
		const typeList = ref([]);

		let dataColumn = [{
			type: "radio"
		}, {
			title: "物料号",
			key: "FCode",
			width: "120px"
		},
		{
			title: "试验号",
			key: "FTestCode",
			width: "120px"
		},
		{
			title: "物料描述",
			key: "FName",
			width: "280px"
		},
		{
			title: "产品分类",
			key: "FTypeName",
			width: "200px"
		}
		];
		dataColumn.forEach((item) => {
			item.align = "center";
		});
		const pageInfo = ref({
			total: 0,
			limit: 5,
			current: 1
		});
		const dataList = ref([]);
		const selectedKey = ref("");
		watch(selectedKey, function () {
			const chose = dataList.value.find(s => s.FID == selectedKey.value) || null;
			if (chose != null) {
				dataInfo.value.FID = chose.FID;
			}
		});

		const showEditBox = ref(false);
		const showButton = ref(true);
		const saleList = ref([]);
		const factoryLists = ref([]);
		const factoryValue=ref(null);
		return {
			dataInfo,
			formulaList,
			seletList,
			typeList,
			dataColumn,
			dataList,
			pageInfo,
			selectedKey,
			showEditBox,
			showButton,
			saleList,
			factoryLists,
			factoryValue
		}
	},
	props: {
		dataInfoObj: {
			type: Object,
			default: () => { },
		}
	},
	mounted() {
		//其他地方过来获取数据
		if (this.dataInfoObj && this.dataInfoObj.FTeamID) {
			this._getTeamData(this.dataInfoObj.FTeamID);
			this.showButton = this.dataInfoObj.FCanEdit != 2
		}
		this._getSelectList();
		this._getFactoryList();
	},
	methods: {
		cancelClick(isFirst) {
			this.$emit('cancelClick', isFirst ? isFirst : false);
		},
		async _getFactoryList() {
			this.factoryLists = await getFactoryList() || [];
		},
		async _getTeamData(dataId) {
			let result = await getDataModel({
				FID: dataId,
				FKey: "FID,FTestCode,FWeightUnit,FSaleCode,FMaterialType,FMaterialGroup,FFormulaTestCode,FFormulaName,FFormulaID,FPackCode,FCreateFactoryID"
			}, "Teamwork");
			this.dataInfo = result || this.dataInfo;
		},
		async _getSelectList() {
			let result = await getDataCode({
				FType: '1,2'
			});
			this.seletList[0] = result.FType1 || [];
			this.seletList[1] = result.FType2 || [];
			this.typeList = await getBasicList(37) || [];
			this._getPageList();
		},
		async _inputSaleCode(val) {
			let pData = {
				FPageIndex: 1,
				FPageSize: 10,
				FName: val || ""
			};
			this.saleList = [];
			if (pData.FName != "") {
				let result = await getPageList(pData, "Material");
				this.saleList = result.Data.List || [];
			}
		},
		//配方检索
		_searchPageList(value) {
			this._getPageList(true);
		},
		//配方翻页
		changePage(obj) {
			this.pageInfo.current = obj.current;
			this.pageInfo.limit = obj.limit;
			this._getPageList();
		},
		//配方接口
		async _getPageList(isFirst) {
			if (isFirst) {
				this.pageInfo.current = 1;
				this.pageInfo.total = 0;
			}
			let postData = {
				FPageIndex: this.pageInfo.current,
				FPageSize: this.pageInfo.limit,
				FSearchName: this.dataInfo.FSearchName ? this.dataInfo.FSearchName : ""
			}

			let result = await searchMaterialsByFactory(postData, "MaterialTeamwork");
			console.log('result', result);
			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;
			});
			this.pageInfo.total = result.Data.Total || 0;
			this.dataList = result.Data.List;
		},
		//数据-验证
		async submitClick(isCreate) {
			let postData = {
				FMaterialId: this.dataInfo.FID,
				FCreateFactoryID: this.dataInfo.FCreateFactoryID
			};

			if (postData.FCreateFactoryID && postData.FMaterialId) {
				this._postTermData(postData);
			} else if(!postData.FCreateFactoryID) {
				layer.msg("选择模式不能为空", { icon : 2, time: 1000})
			} else {
				layer.msg("必须选择物料", { icon : 2, time: 1000})
			}
			
		},
		//数据-提交
		async _postTermData(postData) {
			let result = 0;
			let idx = layer.load(2);
			result = await modifyMaterial(postData, "MaterialTeamwork") || 0;			
			layer.close(idx);

			if (result.Data.TaskId > 0) {
				this.cancelClick(true);
			} else if (result.Data.TaskId == -9) {
				layer.msg('提交失败，没有相应权限的用户');
			} else {
				layer.msg('提交失败');
			}
		}
	}
}
</script>
