<template>
	<div class="my-container">
		<transition-group name="fade" style="position: relative;width: 100%;display: flex;">
			<div class="bill-left" v-show="isShow==='home'" key="item">
				<div class="bill-tab">
					<div class="bill-title">导航</div>
					<side-tree :propConfig="treeConfig" @onChange="onChangeTree" ref="sideTree" />
				</div>
			</div>
			<div class="bill-main" v-show="isShow==='home'" key="item2">
				<module-view :propConfig="config" ref="moduleView" @pagination="getDataList" @search="getDataList"
					@resert="getDataList" @clickHandle="clickHandle" @detail="getDetail" @onRefresh="onRefresh" />
			</div>
			<!-- 新增/编辑 -->
			<div class="detil" v-show="isShow==='add'" key="item3">
				<by-tool :propConfig="toolConfig" @clickHandle="insideTools" ref="byTool" />
				<div class="box-shadow">
					<by-form :propConfig="formConfig" ref="form">
						<template v-slot:description>
							<el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="textarea">
							</el-input>
						</template>
						<template v-slot:tags>
							<el-tag style="margin-right: 16px;" :disable-transitions="true" v-for="(item,index) of tagsData"
								:key="index" closable @close="handleClose2(index)">
								{{item}}
							</el-tag>
							<by-input class="input-new-tag" ref="tagsValue" style="width: 150px;"></by-input>
							<el-button class="button-new-tag" @click="getTagsValue">添加</el-button>
						</template>
						<template v-slot:status>
							<el-radio v-model="radioStatus" :label="1">启用</el-radio>
							<el-radio v-model="radioStatus" :label="2">备用</el-radio>
							<el-radio v-model="radioStatus" :label="3">禁用</el-radio>
						</template>
						<template v-slot:ismakeup>
							<el-radio v-model="ismakeup" :label="1">是</el-radio>
							<el-radio v-model="ismakeup" :label="2">否</el-radio>
						</template>
						<template v-slot:issued>
							<el-radio v-model="issued" :label="1">是</el-radio>
							<el-radio v-model="issued" :label="2">否</el-radio>
						</template>
						<template v-slot:materialSpec>
							<el-radio v-model="materialSpec" :label="0" @input="changeSingle">单规格</el-radio>
							<el-radio v-model="materialSpec" :label="1" @input="changeSingle">多规格</el-radio>
						</template>
						<template v-slot:attributeName>
							<div v-if="isSingle" key="item4">
								<by-table :propConfig="singleTable" @onChangeRow="getInputValue" ref="singeTable">
									<template v-slot:stock="{row}">
										<vxe-input v-if="!isEdit" v-model="row.stock" size="mini" type="integer"></vxe-input>
										<span v-else>{{ row.stock }}</span>
									</template>
								</by-table>
							</div>
							<div v-if="!isSingle" key="item5">
								<el-select :value="selectValue" clearable placeholder="请选择" style="width: 19.5%;"
									@input="getSelectValue">
									<el-option v-for="item in selectList" :key="item.value" :label="item.name" :value="item.id">
									</el-option>
								</el-select>
								<div class="by-tab" key="item6">
									<div v-for="(item,index) of attributeList" :key="index">
										<div class="tab-title">{{item.title}} <i class="el-icon-circle-close"
												@click="closeGroup(index)"></i>
										</div>
										<el-tag class="item-tab" :disable-transitions="true" :key="i" v-for="(tag,i) of item.list" closable
											@close="handleClose(tag,index)">
											{{tag}}
										</el-tag>
										<by-input class="input-new-tag" :ref="'getValue_'+index"></by-input>
										<el-button class="button-new-tag" @click="showInput(index)">添加</el-button>
									</div>

									<div style="margin: 20px 0;" v-if="!isAdd" key="item7">
										<div class="el-button mr15 el-button--primary el-button--small" @click="isAdd=true">添加新规格</div>
									</div>
									<div style="display: flex;margin: 20px 0;" v-else key='item8'>
										<div class="flex-item">
											<label>规格：</label>
											<div class="el-input el-input--small" style="width: 250px;">
												<input type="text" v-model="norms" placeholder="请输入规格" class="el-input__inner">
											</div>
										</div>
										<div class="flex-item">
											<label>规格值：</label>
											<div class="el-input el-input--small" style="width: 250px;">
												<input type="text" v-model="normsValue" placeholder="请输入规格值" class="el-input__inner">
											</div>
										</div>
										<div @click="doComfirm" class="el-button el-button--primary el-button--small">确定</div>
										<div @click="isAdd=false" class="el-button el-button--danger el-button--small">取消</div>
									</div>
									<div>
										<by-table :propConfig="tableConfig" @onChangeRow="getInputValue" ref="table">
											<template v-slot:stock="{row}">
												<vxe-input v-if="!isEdit" v-model="row.stock" size="mini" type="integer"></vxe-input>
												<span v-else>{{ row.stock }}</span>
											</template>
										</by-table>
									</div>
								</div>
							</div>
						</template>
					</by-form>
				</div>
			</div>
		</transition-group>
	</div>
</template>
<script lang="ts">
	import { Component, Prop, Vue, Watch } from "vue-property-decorator";
	import api from '@/api/currency'
	import other from '@/api/other'
	import ForTab from "@/components/ForTab/index.vue";
	@Component({ components: { ForTab } })
	export default class productManagement extends Vue {
		loading : any = null
		tagsData : any = [] // 标签值
		isShow = 'home'
		isEdit = true
		editInfo : any = ''
		isSingle : any = true
		timeNum = 0;
		timer : any = null
		data : any = []
		expandedKeys : any = []
		materialSpec : any = 0
		radioStatus : any = 1
		ismakeup : any = 1
		issued : any = 1
		textarea : any = ''
		categoryId : any = ''
		selectList = []
		selectValue = ''
		// tab
		singeTable : any = ''
		setTableConfig : any = ''
		towTable : any = ''
		attributeList : any = [] // 物料属性规格
		inputValue : any = ''
		norms : any = ''
		normsValue : any = ''
		isAdd : any = false
		skuRuleList : any = ''
		tableList : any = ''
		attributeId : any = ''
		intercept : any = ''
		// tab
		toolConfig = {
			tools: {
				return: true,
				save: true,
			}
		}
		treeConfig = {
			attr: {
				retConfig: {
					id: 'id',
					name: 'name'
				},
				defaultExpandAll: true,
				label: 'name',
				resType: 'data'
			},
			request: {
				url: '/maindata/maindataMaterialCategory/treeList',
				method: 'GET'
			}
		}
		singleTable : any = {
			attr: {
				size: 'mini',
				align: 'center',
			},
			columns: [
				{
					width: '160px',
					title: '标题',
					field: 'skuTitle',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' '
						},
					}
				},
				{
					width: '150px',
					title: '副标题',
					field: 'skuSubtitle',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' '
						},
					}
				},
				{
					width: '150px',
					title: '物料编码',
					field: 'skuCode',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' '
						},
					}
				},
				{
					width: '110px',
					title: '价格',
					field: 'price',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
							defaultValue: 0
						},
					}
				},
				{
					width: '110px',
					title: '单位',
					field: 'unit',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
						},
					}
				},
				{
					width: '110px',
					title: '库存',
					field: 'stock',
					slot: true,
				},
				{
					width: '120px',
					title: '保质期（天）',
					field: 'shelfLife',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "integer",
						},
					}
				},
				{
					width: '110px',
					title: '重量/g',
					field: 'weight',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '110px',
					title: '长/cm',
					field: 'length',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '110px',
					title: '宽/cm',
					field: 'width',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '110px',
					title: '高/cm',
					field: 'height',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '110px',
					title: '体积/cm³',
					field: 'volume',
				},
			]
		}
		tableConfig : any = {
			attr: {
				size: 'mini',
				height: 486,
				align: 'center',
			},
			columns: [
				{
					width: '170px',
					title: '标题',
					field: 'skuTitle',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' '
						},
					}
				},
				{
					width: '120px',
					title: '副标题',
					field: 'skuSubtitle',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' '
						},
					}
				},
				{
					width: '140px',
					title: '物料编码',
					field: 'skuCode',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' '
						},
					}
				},
				{
					width: '80px',
					title: '价格',
					field: 'price',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '80px',
					title: '单位',
					field: 'unit',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
						},
					}
				},
				{
					width: '80px',
					title: '库存',
					field: 'stock',
					slot: true,
				},
				{
					width: '100px',
					title: '保质期（天）',
					field: 'shelfLife',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '80px',
					title: '重量/g',
					field: 'weight',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '80px',
					title: '长/cm',
					field: 'length',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '80px',
					title: '宽/cm',
					field: 'width',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '80px',
					title: '高/cm',
					field: 'height',
					component: 'by-input',
					compConfig: {
						attr: {
							size: 'mini',
							placeholder: ' ',
							type: "number",
						},
					}
				},
				{
					width: '80px',
					title: '体积/cm³',
					field: 'volume',
				},
				{
					width: '80px',
					title: '操作',
					action: true,
					plugins: [{
						icon: 'el-icon-delete',
						name: '删除',
						audit: '',
						event: {
							click: (item : any) => {
								(this as any).doDeleteTable(item)
							}
						}
					}]
				}
			]
		}
		formConfig = {
			attr: {
				size: 'small',
				rules: {
					name: [{
						required: true, message: '请输入名称', trigger: 'blur'
					}],
					basePrice: [{
						required: true, message: '请输入基本售价', trigger: 'blur'
					}],
					categoryName: [{
						required: true, message: '请输入物料分类', trigger: 'change'
					}],
				}
			},
			columns: [
				[
					{
						span: 6,
						label: '名称',
						prop: 'name',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入名称',
								clearable: true
							}
						}
					},
					{
						span: 6,
						label: '简称',
						prop: 'shortName',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入简称',
								clearable: true
							}
						}
					},
					{
						span: 6,
						label: '物料编码',
						prop: 'materialCode',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入物料编码',
							}
						}
					},
					{
						span: 6,
						label: '货号',
						prop: 'itemNumber',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入货号',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 6,
						label: '基本售价',
						prop: 'basePrice',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入基本售价',
								type: 'number'
							}
						}
					},
					{
						span: 6,
						label: '市场价格',
						prop: 'markPrice',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入市场价格',
								type: 'number'
							}
						}
					},
					{
						span: 6,
						label: '财务编号',
						prop: 'accountingCode',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入财务编号',
								clearable: true
							}
						}
					},
					{
						span: 6,
						label: '国际码',
						prop: 'nsCode',
						component: 'by-input',
						compConfig: {
							attr: {
								placeholder: '请输入国际码',
								clearable: true
							}
						}
					},
				],
				[
					{
						span: 6,
						label: '物料分类',
						prop: 'categoryName',
						component: 'select-tree',
						compConfig: {
							attr: {
								label: 'name',
								clearable: true,
								retConfig: {
									categoryName: 'name',
									categoryId: 'id'
								},
								defaultExpandAll: true
							},
							request: {
								url: '/maindata/maindataMaterialCategory/treeList'
							}
						}
					},
					{
						span: 6,
						label: '虚拟分类',
						prop: 'vmCategoryName',
						component: 'select-tree',
						compConfig: {
							attr: {
								label: 'name',
								clearable: true,
								retConfig: {
									vmCategoryName: 'name',
									vmCategoryId: 'id'
								},
								defaultExpandAll: true
							},
							request: {
								url: '/maindata/maindataMaterialVmcategory/treeList'
							}
						}
					},
					{
						labelWidth: '116px',
						span: 6,
						label: '机构商品分类',
						prop: 'organizationCategoryName',
						component: 'select-tree',
						compConfig: {
							attr: {
								label: 'name',
								clearable: true,
								retConfig: {
									organizationCategoryName: 'name',
									organizationCategoryId: 'id'
								},
								defaultExpandAll: true
							},
							request: {
								url: '/maindata/maindataMaterialOrganizationCategory/treeList'
							}
						}
					},
				],
				[
					{
						span: 30,
						label: '描述',
						slot: true,
						prop: 'description',
					}
				],
				[
					{
						span: 20,
						label: '标签',
						slot: true,
						prop: 'tags',
					}
				],
				[
					{
						span: 20,
						label: '物料状态',
						slot: true,
						prop: 'status',
					}
				],
				// [
				// 	{
				// 		span: 20,
				// 		label: '是否组合商品',
				// 		slot: true,
				// 		prop: 'ismakeup',
				// 	}
				// ],
				[
					{
						span: 20,
						label: '是否公布',
						slot: true,
						prop: 'issued',
					}
				],
				[
					{
						span: 20,
						label: '商品规格',
						slot: true,
						prop: 'materialSpec',
					}
				],
				[
					{
						span: 30,
						label: '物料属性',
						slot: true,
						prop: 'attributeName',
					}
				],
			],

		}
		config : any = {
			search: {
				attr: {
					size: 'mini'
				},
				columns: [
					[
						{
							span: 6,
							label: '名称',
							prop: 'name',
							component: 'by-input',
							labelWidth: '70px',
							compConfig: {
								attr: {
									placeholder: '请输入名称',
									clearable: true
								}
							},
						}, {
							span: 6,
							label: '简称',
							prop: 'shortName',
							component: 'by-input',
							labelWidth: '70px',
							compConfig: {
								attr: {
									placeholder: '请输入简称',
									clearable: true
								}
							}
						},
						{
							span: 6,
							label: '货号',
							prop: 'itemNumber',
							component: 'by-input',
							labelWidth: '70px',
							compConfig: {
								attr: {
									placeholder: '请输入货号',
									clearable: true
								}
							}
						},
						{
							span: 6,
							label: '基本售价',
							prop: 'basePrice',
							component: 'by-input',
							labelWidth: '70px',
							compConfig: {
								attr: {
									placeholder: '请输入基本售价',
									clearable: true
								}
							},
						},
					],
					[
						{
							span: 6,
							label: '财务编号',
							prop: 'accountingCode',
							component: 'by-input',
							labelWidth: '70px',
							compConfig: {
								attr: {
									placeholder: '请输入财务编号',
									clearable: true
								}
							}
						},
					]
				]
			},
			tool: {
				tools: {
					add: true,
					delete: true,
					search: true,
					refresh: true
				}
			},
			table: {
				attr: {
					size: 'mini',
					seq: true,
					align: 'center',
					checkbox: true,
					height: 560,
				},
				columns: [{
					width: 250,
					title: '名称',
					field: 'name',
					isDetail: true,
				}, {
					title: '简称',
					field: 'shortName'
				}, {
					title: '货号',
					field: 'itemNumber'
				}, {
					// width: 120,
					title: '财务编号',
					field: 'accountingCode'
				}, {
					// width: 120,
					title: '国际码',
					field: 'nsCode'
				}, {
					width: 80,
					title: '基本售价',
					field: 'basePrice'
				}, {
					width: 80,
					title: '市场价格', // （市场价格统一售价）
					field: 'markPrice'
				},
				{
					width: 120,
					title: '操作',
					action: true,
					plugins: [{
						icon: 'el-icon-edit',
						name: '编辑',
						audit: '',
						event: {
							click: (item : any) => {
								(this as any).getDetail(item)
							}
						}
					}, {
						name: '删除',
						event: {
							click: (item : any) => (this as any).doDelete2(item)
						}
					}]
				}]
			},
		}
		mounted() {
			this.getAttribute()
			this.timer = setInterval(() => {
				this.getDataList()
			}, 500)
		}
		//点击树获取数据
		onChangeTree(e : any) {
			this.categoryId = e.id;
			api.ByCategoryPage({ categoryId: e.id },'maindataMaterialCategory').then((res : any) => {
				if (res.code === 200) {
					(this.$refs.moduleView as any).setTableValue(res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.moduleView as any).setPage(page)
				}
			})
		}
		// 保存编辑/新增
		doFormData() {
			(this as any).$refs.form.validate().then(() => {
				let query = (this as any).$refs.form.getValue();
				// console.log('物料信息 ==> ',query);
				query.materialSpec = this.materialSpec; // 商品规格
				query.attributeList = JSON.stringify(this.attributeList) // 物料属性
				query.attributeId = this.attributeId // 物料属性ID
				query.status = this.radioStatus; // 物料状态
				query.tags = this.tagsData.join(","); // 标签
				// query.ismakeup = this.ismakeup; // 商品组合
				query.issued = this.issued; // 是否公布
				query.description = this.textarea; // 备注
				let data : any = {}
				// 多规格
				if (query.materialSpec === 1) {
					if (this.tableList.length <= 0 || this.attributeList.length <= 0) return (this as any).$message.warning('多规格商品，物料属性不能为空！');

					console.log('多规格表格数据 ==> ', this.tableList);
					for (let v of this.tableList) {
						if (!v.price) return this.$message.warning('物料价格必填');
						if (parseInt(v.price) <= 0) return this.$message.warning('物料价格不能小于1');
						if (!v.unit) return this.$message.warning('物料单位必填');
						if (!v.stock) return this.$message.warning('物料库存必填');
						if (parseInt(v.stock) <= 0) return this.$message.warning('物料库存必须为自然数');
						if (!v.shelfLife) return this.$message.warning('物料保质期必填');
						if (parseInt(v.shelfLife) <= 0) return this.$message.warning('物料保质期必须为自然数');
						if (!v.weight) return this.$message.warning('物料重量必填');
						if (parseInt(v.weight) <= 0) return this.$message.warning('物料重量不能小于1');
						if (!v.length) return this.$message.warning('物料长必填');
						if (parseInt(v.length) <= 0) return this.$message.warning('物料长不能小于1');
						if (!v.width) return this.$message.warning('物料宽必填');
						if (parseInt(v.width) <= 0) return this.$message.warning('物料宽不能小于1');
						if (!v.height) return this.$message.warning('物料高必填');
						if (parseInt(v.height) <= 0) return this.$message.warning('物料高不能小于1');
					};
					data = { maindataMaterial: query, maindataMaterialSkus: this.tableList }
					let newArray : any = []
					let materialSpecData : any = []
					let attributeLength = this.attributeList.length
					this.tableList.map((v : any) => {
						for (let kyes in v) {
							if (kyes.indexOf('_field_name') != -1) {
								let key = kyes.split('_field_name')[0]
								newArray.push({ [key]: v[kyes] })
							}
						}
					})
					// console.log('newArray ==> ',newArray);
					for (var i = 0; i < newArray.length; i += attributeLength) {
						materialSpecData.push(newArray.slice(i, i + attributeLength));
					}
					// console.log('materialSpecData ==> ', materialSpecData);
					data.maindataMaterialSkus.map((v : any, i : any) => {
						if (materialSpecData[i]) v.materialSpec = JSON.stringify(materialSpecData[i])
					})
				} else { // 单规格
					data = { maindataMaterial: query, maindataMaterialSkus: (this.$refs.singeTable as any).getValue() }
					console.log('单规格表格数据 ==> ', data.maindataMaterialSkus);
					for (let v of data.maindataMaterialSkus) {
						if (!v.price) return this.$message.warning('物料价格必填');
						if (parseInt(v.price) <= 0) return this.$message.warning('物料价格不能小于1');
						if (!v.unit) return this.$message.warning('物料单位必填');
						if (!v.stock) return this.$message.warning('物料库存必填');
						if (parseInt(v.stock) <= 0) return this.$message.warning('物料库存必须为自然数');
						if (!v.shelfLife) return this.$message.warning('物料保质期必填');
						if (parseInt(v.shelfLife) <= 0) return this.$message.warning('物料保质期必须为自然数');
						if (!v.weight) return this.$message.warning('物料重量必填');
						if (parseInt(v.weight) <= 0) return this.$message.warning('物料重量不能小于1');
						if (!v.length) return this.$message.warning('物料长必填');
						if (parseInt(v.length) <= 0) return this.$message.warning('物料长不能小于1');
						if (!v.width) return this.$message.warning('物料宽必填');
						if (parseInt(v.width) <= 0) return this.$message.warning('物料宽不能小于1');
						if (!v.height) return this.$message.warning('物料高必填');
						if (parseInt(v.height) <= 0) return this.$message.warning('物料高不能小于1');
					};
					data.maindataMaterialSkus[0].isDeleted = 0;
					data.maindataMaterial.attributeList = ""
				}
				// console.log('data ==> ', data);
				if (this.isEdit) { // 编辑
					other.updateSpuAndSku(data, 'maindataMaterial').then((res : any) => {
						if (res.code === 200) {
							this.isShow = 'home';
							(this as any).$refs.sideTree.request();
							this.getDataList();
							this.$message.success('保存成功!');
						}
					})
					// 新增
				} else {
					other.saveSpuAndSku(data, 'maindataMaterial').then((res : any) => {
						if (res.code === 200) {
							this.isShow = 'home';
							(this as any).$refs.sideTree.request();
							this.getDataList();
							this.$message.success('保存成功!');
						}
					})
				}

			});
		}
		// 新增
		onAdd() {
			this.isShow = 'add'; // 展示类型
			this.selectValue = '' // 物料属性名称
			this.towTable = '' // 表格数据
			this.textarea = '' // 描述
			this.tagsData = [] // 标签
			this.attributeList = [] // 属性规格
			this.tableList = [] // 表格数据
			this.materialSpec = 0; // 单规格
			this.isSingle = true; // 显示单表格
			this.isEdit = false; // 是否编辑
			(this as any).$refs.byTool.setTool({ return: true, save: true });
			setTimeout(() => {
				(this as any).$refs.form.setValue(); // 设置表单数据
				(this as any).$refs.singeTable.setValue([{}]) // 设置单表格数据
			}, 0)
		}
		// 编辑
		getDetail(e : any) {
			console.log('编辑 ==> ', e);
			(this as any).$refs.byTool.setTool({ return: true, save: true, delete: true })
			this.loading = this.$loading({ target: '.my-container' });
			api.single({ id: e.vmCategoryId }, 'maindataMaterialVmcategory').then((res : any) => {
				if (res.code === 200) {
					if (res.data) e.vmCategoryName = res.data.name;
				}
			});
			api.single({ id: e.organizationCategoryId }, 'maindataMaterialOrganizationCategory').then((res : any) => {
				if (res.code === 200) {
					if (res.data) e.organizationCategoryName = res.data.name;
				}
			});
			api.single({ id: e.categoryId }, 'maindataMaterialCategory').then((res : any) => {
				if (res.code === 200) e.categoryName = res.data.name;
				if (e.attributeId) {
					api.single({ id: e.attributeId }, 'maindataMaterialAttribute').then((res : any) => {
						if (res.code === 200) {
							if (res.data) this.selectValue = res.data.name
						}
					})
				}
				this.intercept = e.attributeId // 拦截物料属性判断
				this.isEdit = true;
				this.editInfo = e;
				this.isShow = 'add';
				// 标签
				if (e.tags) {
					this.tagsData = e.tags.split(",")
				} else this.tagsData = [];
				this.materialSpec = e.materialSpec
				this.isSingle = e.materialSpec === 0 ? true : false;
				if (e.attributeList) this.attributeList = JSON.parse(e.attributeList);
				other.selectByMaterialId({ id: e.id }, 'maindataMaterialSku').then((res : any) => {
					if (res.code === 200) {
						if (this.isSingle) {
							this.selectValue = '' // 物料属性名称
							this.towTable = '' // 表格数据
							this.attributeList = []; // 属性规格
							setTimeout(() => {
								(this as any).$refs.singeTable.setValue(res.data);
							}, 0)
						} else {
							this.forFun(this.attributeList[0], 0) // 生成sku
							let tableConfig = (this as any).$lodash.cloneDeep(this.tableConfig);
							let skuRuleList = (this as any).$lodash.cloneDeep(this.skuRuleList);
							let newColumns = (this as any).$lodash.cloneDeep(this.attributeList).reverse();
							res.data.map((v : any, i : any) => {
								v = Object.assign(v, skuRuleList[i])
								v.dataIndex = i
							});
							newColumns.map((v : any) => {
								tableConfig.columns.unshift({
									width: '80px',
									title: v.title,
									field: v.field,
								})
							});
							this.tableList = (this as any).$lodash.cloneDeep(res.data);
							// console.log('表格配置 ==> ', tableConfig);
							console.log('skuRuleList ==> ', skuRuleList);
							console.log('表格数据 ==> ', this.tableList);
							this.setTableConfig = tableConfig;
							setTimeout(() => {
								(this as any).$refs.table.setConfig(tableConfig); // 设置表格配置
								(this as any).$refs.table.setValue(this.tableList) // 设置表格数据
							}, 300)
						}
					}
					(this as any).$refs.form.setValue(e)
					this.loading.close()
				})
			})
		}
		// 立即生成
		generateNow() {
			this.forFun(this.attributeList[0], 0) // 生成sku
			let newArray : any = []
			let materialSpecData : any = []
			let attributeLength = this.attributeList.length
			let tableConfig = (this as any).$lodash.cloneDeep(this.tableConfig);
			let skuRuleList = (this as any).$lodash.cloneDeep(this.skuRuleList);
			let newColumns = (this as any).$lodash.cloneDeep(this.attributeList).reverse();
			let tableList = (this as any).$refs.table.getValue();
			tableList.map((v : any) => {
				for (let key in v) {
					if (key.indexOf('_field_name') != -1) {
						delete v[key]
					}
				}
			})
			skuRuleList.map((v : any) => {
				for (let kyes in v) {
					if (kyes.indexOf('_field_name') != -1) {
						let key = kyes.split('_field_name')[0]
						newArray.push({ [key]: v[kyes] })
					}
				}
			})
			// console.log('skuRuleList ==> ', skuRuleList);
			for (var i = 0; i < newArray.length; i += attributeLength) {
				materialSpecData.push(newArray.slice(i, i + attributeLength));
			}
			// console.log('materialSpecData ==> ', materialSpecData);
			skuRuleList.map((v : any, i : any) => {
				v.materialSpec = JSON.stringify(materialSpecData[i]);
				v.dataIndex = i;
				v.isDeleted = 0;
				if (tableList[i] && v.materialSpec.indexOf(tableList[i].materialSpec != -1)) {
					v = Object.assign(v, tableList[i])
				};
			})
			newColumns.map((v : any) => {
				tableConfig.columns.unshift({
					width: '80px',
					title: v.title,
					field: v.field,
				})
			});
			console.log('旧表格数据 ==> ', tableList);
			console.log('新表格数据 ==> ', skuRuleList);
			this.tableList = skuRuleList;
			this.setTableConfig = tableConfig;
			setTimeout(() => {
				(this as any).$refs.table.setConfig(tableConfig); // 设置表格配置
				(this as any).$refs.table.setValue(this.tableList); // 设置表格数据
			}, 0)
		}
		// 更换物料属性数据
		getSelectValue(e : any) {
			if (this.intercept === e) return console.log('拦截!');
			this.$confirm('确定更换物料属性吗？确定之后原表格数据会被清除', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				api.single({ id: e }, 'maindataMaterialAttribute').then((res : any) => {
					if (res.code === 200) {
						this.selectList.map((v : any) => {
							if (v.id === e) {
								v.id = e
								this.selectValue = v.name
							}
						});
						this.intercept = e
						this.attributeList = JSON.parse(res.data.attributeValue);
						this.attributeId = e
						this.forFun(this.attributeList[0], 0) // 生成sku
						let tableConfig = (this as any).$lodash.cloneDeep(this.tableConfig);
						let skuRuleList = (this as any).$lodash.cloneDeep(this.skuRuleList);
						let newColumns = (this as any).$lodash.cloneDeep(this.attributeList).reverse();
						let giveInfo = (this as any).$refs.form.getValue();
						skuRuleList.map((v : any, i : any) => {
							v.dataIndex = i;
							v.isDeleted = 0;
							v.skuTitle = giveInfo.name;
							v.skuSubtitle = giveInfo.shortName;
							v.price = giveInfo.basePrice
						})
						newColumns.map((v : any) => {
							tableConfig.columns.unshift({
								width: '80px',
								title: v.title,
								field: v.field,
							})
						});
						console.log('表格配置 ==> ', tableConfig);
						console.log('表格数据 ==> ', skuRuleList);
						this.setTableConfig = tableConfig;
						this.tableList = skuRuleList;
						setTimeout(() => {
							(this as any).$refs.table.setConfig(tableConfig); // 设置表格配置
							(this as any).$refs.table.setValue(skuRuleList); // 设置表格数据
						}, 0)
					}
				})
			}).catch(() => {});
		}
		// 切换商品规格
		changeSingle() {
			if (this.isSingle) {
				if ((this as any).$refs.singeTable) this.singeTable = (this as any).$refs.singeTable.getValue()
				this.isSingle = false
				setTimeout(() => {
					// 设置表格数据
					if (this.towTable) {
						if ((this as any).$refs.table) {
							this.tableList = this.towTable;
							(this as any).$refs.table.setConfig(this.setTableConfig); // 设置表格配置
							(this as any).$refs.table.setValue(this.towTable); // 设置表格数据
						}
					}
				}, 0)
			} else {
				if ((this as any).$refs.table) this.towTable = (this as any).$refs.table.getValue()
				this.isSingle = true;
				setTimeout(() => {
					// 设置单表格数据
					if (this.singeTable) {
						console.log('单表格数据 ==> ', this.singeTable);
						(this as any).$refs.singeTable.setValue(this.singeTable);
					} else (this as any).$refs.singeTable.setValue([{}]);
				}, 0)
			}
		}
		// 添加规格
		showInput(index : any) {
			this.$confirm('确定添加吗！确定后将可能影响原表格数据', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let inputValue = (this as any).$refs['getValue_' + index][0].getValue();
				if (!inputValue) return (this as any).$message.warning('请输入内容');
				if (!/^\S*$/.test(inputValue)) return (this as any).$message.warning('输入内容不能有空格');
				if (this.attributeList[index].list.indexOf(inputValue) !== -1) return (this as any).$refs['getValue_' + index][0].clearValue();
				if (inputValue) {
					this.attributeList[index].list.push(inputValue);
				}
				(this as any).$refs['getValue_' + index][0].clearValue();
				this.generateNow();
				this.$message.success('添加成功!');
			}).catch(() => this.$message.info('已取消添加'));
		}
		// 添加sku规格
		doComfirm() {
			if (!this.normsValue || !this.norms) return (this as any).$message.warning('请添加完整的规格');
			if (!/^\S*$/.test(this.normsValue) || !/^\S*$/.test(this.norms)) return this.$message.warning('输入内容不能有空格');
			this.$confirm('确定添加吗！确定后将可能影响原表格数据', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				let obj : any = {
					title: this.norms,
					field: this.norms + '_field_name',
					list: [this.normsValue]
				};
				this.attributeList.push(obj);
				this.norms = '';
				this.normsValue = '';
				this.isAdd = false;
				this.generateNow();
				this.$message.success('添加成功!');
			}).catch(() => this.$message.info('已取消添加'));
		}
		// 监听表格数据
		getInputValue(e : any) {
			// console.log('监听表格 ==> ', e);
			if (e.length && e.width && e.height) {
				e.volume = (e.length * e.width * e.height).toFixed(2);
			}
		}
		// 排序生成
		forFun(data : any, i : number, v ?: any) {
			if (data.list) {
				let d : Array<any> = [];
				data.list.forEach((item : any) => {
					if (i == 0) { // 第一次循环
						let obj : any = {}
						obj[data.field] = item
						d.push(obj);
					} else {
						v.forEach((ele : any) => {
							let obj : any = {};
							obj = (this as any).$lodash.clone(ele);
							obj[data.field] = item
							d.push(obj);
						});
					}
				});
				i++
				if (this.attributeList[i]) {
					this.forFun(this.attributeList[i], i, d);
				}
				else {
					this.skuRuleList = d;
				}
			}
		}
		// 添加标签
		getTagsValue() {
			let value = (this as any).$refs.tagsValue.getValue().trim();
			if (!value) return this.$message.warning('请输入标签值');
			this.tagsData.push(value);
			(this as any).$refs.tagsValue.clearValue();
		}
		// 删除标签
		handleClose2(index : any) {
			this.$confirm('确定删除吗！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				this.tagsData.splice(index, 1)
				this.$message.success('删除成功!');
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 删除标签
		handleClose(tag : any, index : any) {
			this.$confirm('确定删除吗！确定后将可能影响原表格数据', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				this.attributeList[index].list.splice(this.attributeList[index].list.indexOf(tag), 1);
				this.generateNow();
				this.$message.success('删除成功!');
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 删除数组
		closeGroup(index : any) {
			this.$confirm('确定删除吗！确定后将可能影响原表格数据', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				this.attributeList.splice(index, 1)
				this.generateNow();
				this.$message.success('删除成功!');
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 删除表格数据
		doDeleteTable(item : any) {
			this.$confirm('确定删除吗！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				console.log(this.tableList);
				(this as any).tableList.map((v : any) => {
					if (v.dataIndex === item.dataIndex) {
						v.isDeleted = 1
					}
				})
				let filterList = this.tableList.filter((v : any) => v.isDeleted !== 1);
				(this as any).$refs.table.setValue(filterList); // 设置表格数据
				this.$message.success('删除成功!');
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 获取物料属性
		getAttribute() {
			api.pageList({}, 'maindataMaterialAttribute').then((res : any) => {
				if (res.code === 200) {
					this.selectList = res.data.records
				} else this.$message.error(res.msg);
			})
		}
		// 获取列表数据
		getDataList() {
			if (!this.$refs.moduleView) {
				if (this.timeNum > 5) {
					clearInterval(this.timer)
				}
				this.timeNum++;
				return
			}
			clearInterval(this.timer)
			let query = (this.$refs.moduleView as any).getQuery();
			query.categoryId = this.categoryId;
			let newData : any = {}
			for (let key in query) {
				if (query[key].toString()) {
					newData[key] = query[key]
				}
			}
			// console.log('表单字段 ==> ', newData);
			api.pageList(newData, 'maindataMaterial').then((res : any) => {
				if (res.code === 200) {
					(this.$refs.moduleView as any).setTableValue(res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.moduleView as any).setPage(page)
				} else this.$message.error(res.msg);
			})
		}
		// 工具栏方法
		clickHandle(e : any) {
			if (e === 'onAdd') this.onAdd();
			if (e === 'onDelete') this.doDelete();
			if (e === 'onExport') this.onExport();
		}
		// 内页工具栏方法
		insideTools(e : any) {
			console.log(e);
			if (e === 'onReturn') this.isShow = 'home';
			if (e === 'onSave') this.doFormData();
			if (e === 'onDelete') this.pageDelete(this.editInfo);
		}
		// 内页删除
		pageDelete(item : any) {
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				other.deleteSpuAndSku({ ids: item.id }, 'maindataMaterial').then((res : any) => {
					if (res.code === 200) {
						this.$message.success('删除成功!');
						this.isShow = 'home';
						(this as any).$refs.sideTree.request();
						this.getDataList();
					} else this.$message.error(res.msg);
				})
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 工具栏删除
		doDelete() {
			let selectData = (this.$refs.moduleView as any).getSelectData()
			let ids = '';
			if (selectData.length > 0) {
				selectData.map((v : any) => {
					ids += v.id + ','
				})
			} else return this.$message({ type: 'warning', message: '请选择删除数据' })
			ids = ids.slice(0, ids.length - 1);
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				other.deleteSpuAndSku({ ids: ids }, 'maindataMaterial').then((res : any) => {
					if (res.code === 200) {
						(this as any).$refs.sideTree.request();
						this.getDataList();
						this.$message.success('删除成功!');
					} else this.$message.error(res.msg);
				})
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 操作删除
		doDelete2(item : any) {
			this.$confirm('确定删除吗,此操作不能撤销！', '注意', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				center: true
			}).then(() => {
				other.deleteSpuAndSku({ ids: item.id }, 'maindataMaterial').then((res : any) => {
					if (res.code === 200) {
						(this as any).$refs.sideTree.request();
						this.getDataList();
						this.$message.success('删除成功!');
					} else this.$message.error(res.msg);
				})
			}).catch(() => this.$message.info('已取消删除'));
		}
		// 刷新
		onRefresh() {
			this.loading = this.$loading({ target: '.main-container' });
			(this as any).$refs.moduleView.clearSearch();
			api.pageList({}, 'maindataMaterialSku').then((res : any) => {
				if (res.code === 200) {
					(this.$refs.moduleView as any).setTableValue(res.data.records);
					let page = {
						pageNo: res.data.current, //当前页
						pageSize: res.data.size, //每页条数
						total: res.data.total //总条数
					};
					(this.$refs.moduleView as any).setPage(page);
					this.loading.close();
				} else this.$message.error(res.msg);
			})
		}
		//导出
		onExport() {
			let urlArr = '/maindata/maindataMaterial';
			let query = (this.$refs.moduleView as any).getQuery();
			(this as any).$download(urlArr + '/export', {
				...query
			}, urlArr[urlArr.length - 1] + `_${new Date().getTime()}.xlsx`)
		}
	}
</script>
<style lang="scss" scoped>
	.my-container {
		width: 100%;
		box-sizing: border-box;
		display: flex;
		padding: 16px;

		.by-tab {
			.tab-title {
				padding: 10px 0 0;
			}

			.item-tab {
				margin-right: 16px;
			}

			.input-new-tag {
				width: 130px;
			}


			.flex-item {
				margin-right: 20px;
			}
		}

		.search-btn {
			width: 100%;
			display: flex;
			justify-content: flex-end;
			margin-bottom: 20px;
		}

		.bill-left {
			position: relative;
			border-right: solid #EEE 1px;
			padding-right: 16px;
			flex-shrink: 0;
			height: 740px;

			// box-sizing: border-box;
			.bill-tab {
				width: 150px;
				height: 100%;
				transition: all .5s;
				overflow: hidden;
			}

			.bill-title {
				font-size: 16px;
				padding-bottom: 16px;
				width: 200px;
			}

			.bill-nav {
				font-size: 14px;
				height: 30px;
				line-height: 30px;
				width: 200px;
				box-sizing: border-box;
				padding: 0 8px;
				cursor: pointer;
				margin-bottom: 2px;
				border-radius: 5px;
			}

			.onBill {
				background-color: #bde3f7;
			}

			.bill-nav:hover {
				background-color: #bde3f7;
			}

			.close {
				height: 22px;
				width: 22px;
				border-radius: 50%;
				border: solid #EEE 1px;
				position: absolute;
				top: 30px;
				right: -11px;
				background-color: #FFF;
				display: flex;
				justify-content: center;
				align-items: center;
				cursor: pointer;
			}
		}

		.bill-main {
			width: calc(100% - 16px);
			box-sizing: border-box;
			margin-left: 16px;
			position: relative;

			.bill-box {
				width: 100%;
				position: absolute;
				left: 0;
				top: 0;
				opacity: 0;
				z-index: -1;
				transition: all .5s;

				.bill-tool,
				.table-tool {
					width: 100%;
					padding-bottom: 16px;
				}

				.form {
					margin-bottom: 8px;
				}
			}

			.on-show {
				opacity: 1;
				z-index: 1;
			}
		}

		.detil {
			width: 100%;

			.box-shadow {
				box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
				padding: 20px;
				border-radius: 4px;
				border: 1px solid #e6ebf5;
				background-color: #fff;
				overflow: hidden;
				color: #303133;
				-webkit-transition: .3s;
				transition: .3s;
				margin-top: 20px;
			}
		}

		@media screen and (min-width: 1700px) {
			.bill-main {
				min-width: 1450px;
			}
		}
	}
</style>