<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="产品编码" prop="productCode">
				<el-input
					v-model="queryParams.productCode"
					placeholder="请输入产品编码"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="产品名称" prop="productDescZh">
				<el-input
					v-model="queryParams.productDescZh"
					placeholder="请输入产品名称"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					plain
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
					v-hasPermi="['wms:product:add']"
				>新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					plain
					icon="el-icon-edit"
					size="mini"
					:disabled="single"
					@click="handleUpdate"
					v-hasPermi="['wms:product:edit']"
				>修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					plain
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
					v-hasPermi="['wms:product:remove']"
				>删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="warning"
					plain
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
					v-hasPermi="['wms:product:export']"
				>导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>


		<el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<!-- <el-table-column label="主键ID" align="center" prop="productId" /> -->
			<el-table-column label="产品编码" align="center" prop="productCode" width="200"/>
			<el-table-column label="产品类别" align="center" prop="productGroup">
			  <template slot-scope="scope">
          {{ scope.row.productGroup == "material_type1" ? "产品类别1" : "产品类别2" }}
        </template>
      </el-table-column>
			<!-- 中文描述也就是产品名称 -->
			<el-table-column label="产品名称" align="center" prop="productDescZh" width="400"/>
			<el-table-column label="产品型号" align="center" prop="productModel" />
			<!-- <el-table-column label="英文描述" align="center" prop="productDescEn" />
			<el-table-column label="序列号规则代码" align="center" prop="ruleCode" />
			<el-table-column label="旧产品号" align="center" prop="oldProductCode" />
			<el-table-column label="散件产品号" align="center" prop="partsProductCode" />
			<el-table-column label="69码" align="center" prop="skuBarcode" />
			<el-table-column label="长" align="center" prop="length" />
			<el-table-column label="宽" align="center" prop="width" />
			<el-table-column label="高" align="center" prop="height" /> -->
			<!-- <el-table-column label="毛重" align="center" prop="grossWeight" />
			<el-table-column label="净重" align="center" prop="netWeight" />
			<el-table-column label="皮重" align="center" prop="tareWeight" />
			<el-table-column label="体积" align="center" prop="volume" />
			<el-table-column label="单价" align="center" prop="unitPrice" />
			<el-table-column label="产品组" align="center" prop="productGroup" />
			<el-table-column label="${comment}" align="center" prop="productGroupName" />
			<el-table-column label="用户自定义1" align="center" prop="userDefined1" />
			<el-table-column label="用户自定义2" align="center" prop="userDefined2" />
			<el-table-column label="用户自定义3" align="center" prop="userDefined3" />
			<el-table-column label="${comment}" align="center" prop="userDefined4" />
			<el-table-column label="${comment}" align="center" prop="userDefined5" />
			<el-table-column label="${comment}" align="center" prop="userDefined6" />
			<el-table-column label="工厂编码" align="center" prop="factoryCode" /> -->
			<el-table-column label="是否启用" align="center" prop="activeFlag">
			   <template slot-scope="scope">
           {{ scope.row.activeFlag == 1 ? "是" : "否" }}
         </template>
      </el-table-column>
			<!-- <el-table-column label="${comment}" align="center" prop="syncFlag" />
			<el-table-column label="${comment}" align="center" prop="primaryUom" />
			<el-table-column label="订单单位" align="center" prop="bstme" />
			<el-table-column label="${comment}" align="center" prop="basicOrder" />
			<el-table-column label="${comment}" align="center" prop="convOrder" />
			<el-table-column label="发货单位" align="center" prop="ausme" />
			<el-table-column label="${comment}" align="center" prop="basicIssue" />
			<el-table-column label="${comment}" align="center" prop="convIssue" />
			<el-table-column label="是否补料" align="center" prop="appendFlag" />
			<el-table-column label="补料比例" align="center" prop="appendPercent" /> -->
			<el-table-column label="创建人" align="center" prop="createBy" />
			<el-table-column label="创建时间" align="center" prop="createTime" width="200"/>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						@click="handleUpdate(scope.row)"
						v-hasPermi="['wms:product:edit']"
					>修改</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['wms:product:remove']"
					>删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改产品信息对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="产品编码" prop="productCode">
					<el-input v-model="form.productCode" placeholder="请输入产品编码" style="width:350px"/>
				</el-form-item>
				<el-form-item label="产品名称" prop="productDescZh">
					<el-input v-model="form.productDescZh" placeholder="请输入产品名称" style="width:350px"/>
				</el-form-item>
				<el-form-item label="产品型号" prop="productModel">
        	<el-input v-model="form.productModel" placeholder="请输入产品型号" style="width:350px"/>
        </el-form-item>
				<el-form-item label="产品类别" prop="productGroup">
        	<el-select v-model="form.productGroup" placeholder="请选择产品类别" style="width:350px">
            <el-option
              v-for="dict in dict.type.material_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
				<!-- <el-form-item label="英文描述" prop="productDescEn">
					<el-input v-model="form.productDescEn" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="序列号规则代码" prop="ruleCode">
					<el-input v-model="form.ruleCode" placeholder="请输入序列号规则代码" />
				</el-form-item>
				<el-form-item label="旧产品号" prop="oldProductCode">
					<el-input v-model="form.oldProductCode" placeholder="请输入旧产品号" />
				</el-form-item>
				<el-form-item label="散件产品号" prop="partsProductCode">
					<el-input v-model="form.partsProductCode" placeholder="请输入散件产品号" />
				</el-form-item>
				<el-form-item label="69码" prop="skuBarcode">
					<el-input v-model="form.skuBarcode" placeholder="请输入69码" />
				</el-form-item>
				<el-form-item label="长" prop="length">
					<el-input v-model="form.length" placeholder="请输入长" />
				</el-form-item>
				<el-form-item label="宽" prop="width">
					<el-input v-model="form.width" placeholder="请输入宽" />
				</el-form-item>
				<el-form-item label="高" prop="height">
					<el-input v-model="form.height" placeholder="请输入高" />
				</el-form-item>
				<el-form-item label="毛重" prop="grossWeight">
					<el-input v-model="form.grossWeight" placeholder="请输入毛重" />
				</el-form-item>
				<el-form-item label="净重" prop="netWeight">
					<el-input v-model="form.netWeight" placeholder="请输入净重" />
				</el-form-item>
				<el-form-item label="皮重" prop="tareWeight">
					<el-input v-model="form.tareWeight" placeholder="请输入皮重" />
				</el-form-item>
				<el-form-item label="体积" prop="volume">
					<el-input v-model="form.volume" placeholder="请输入体积" />
				</el-form-item>
				<el-form-item label="单价" prop="unitPrice">
					<el-input v-model="form.unitPrice" placeholder="请输入单价" />
				</el-form-item>
				<el-form-item label="产品组" prop="productGroup">
					<el-input v-model="form.productGroup" placeholder="请输入产品组" />
				</el-form-item>
				<el-form-item label="${comment}" prop="productGroupName">
					<el-input v-model="form.productGroupName" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="用户自定义1" prop="userDefined1">
					<el-input v-model="form.userDefined1" placeholder="请输入用户自定义1" />
				</el-form-item>
				<el-form-item label="用户自定义2" prop="userDefined2">
					<el-input v-model="form.userDefined2" placeholder="请输入用户自定义2" />
				</el-form-item>
				<el-form-item label="用户自定义3" prop="userDefined3">
					<el-input v-model="form.userDefined3" placeholder="请输入用户自定义3" />
				</el-form-item>
				<el-form-item label="${comment}" prop="userDefined4">
					<el-input v-model="form.userDefined4" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="userDefined5">
					<el-input v-model="form.userDefined5" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="userDefined6">
					<el-input v-model="form.userDefined6" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="工厂编码" prop="factoryCode">
					<el-input v-model="form.factoryCode" placeholder="请输入工厂编码" />
				</el-form-item> -->
				<el-form-item label="是否启用" prop="activeFlag">
				  <el-select v-model="form.activeFlag" placeholder="请选择是否启用"  @change="$forceUpdate()" clearable style="width:350px">
            <el-option v-for="item in options" :key="item.activeFlag" :label="item.label" :value="item.activeFlag"></el-option>
          </el-select>
				</el-form-item>
				<!-- <el-form-item label="${comment}" prop="syncFlag">
					<el-input v-model="form.syncFlag" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="primaryUom">
					<el-input v-model="form.primaryUom" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="delFlag">
					<el-input v-model="form.delFlag" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="订单单位" prop="bstme">
					<el-input v-model="form.bstme" placeholder="请输入订单单位" />
				</el-form-item>
				<el-form-item label="${comment}" prop="basicOrder">
					<el-input v-model="form.basicOrder" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="convOrder">
					<el-input v-model="form.convOrder" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="发货单位" prop="ausme">
					<el-input v-model="form.ausme" placeholder="请输入发货单位" />
				</el-form-item>
				<el-form-item label="${comment}" prop="basicIssue">
					<el-input v-model="form.basicIssue" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="convIssue">
					<el-input v-model="form.convIssue" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="是否补料" prop="appendFlag">
					<el-input v-model="form.appendFlag" placeholder="请输入是否补料" />
				</el-form-item>
				<el-form-item label="补料比例" prop="appendPercent">
					<el-input v-model="form.appendPercent" placeholder="请输入补料比例" />
				</el-form-item> -->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/wms/product";

export default {
	name: "Product",
	dicts: ['sys_normal_disable', 'material_type'],
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 产品信息表格数据
			productList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				productCode: null,
				productDescZh: null,
				productModel:null,
				productDescEn: null,
				ruleCode: null,
				oldProductCode: null,
				partsProductCode: null,
				skuBarcode: null,
				length: null,
				width: null,
				height: null,
				grossWeight: null,
				netWeight: null,
				tareWeight: null,
				volume: null,
				unitPrice: null,
				productGroup: null,
				productGroupName: null,
				userDefined1: null,
				userDefined2: null,
				userDefined3: null,
				userDefined4: null,
				userDefined5: null,
				userDefined6: null,
				factoryCode: null,
				activeFlag: null,
				syncFlag: null,
				primaryUom: null,
				bstme: null,
				basicOrder: null,
				convOrder: null,
				ausme: null,
				basicIssue: null,
				convIssue: null,
				appendFlag: null,
				appendPercent: null
			},
				options: [{
        activeFlag: "1",
        label: '是'
        }, {
        activeFlag: "0",
        label: '否'
        }],
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				productCode: [
					{ required: true, message: "产品代码不能为空", trigger: "blur" }
				],
			}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		// 生成表头序号
		indexMethod(index) {
        return index + 1;
        },
		/** 查询产品信息列表 */
		getList() {
			this.loading = true;
			listProduct(this.queryParams).then(response => {
				this.productList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				productId: null,
				productCode: null,
				productDescZh: null,
				productModel:null,
				productDescEn: null,
				ruleCode: null,
				oldProductCode: null,
				partsProductCode: null,
				skuBarcode: null,
				length: null,
				width: null,
				height: null,
				grossWeight: null,
				netWeight: null,
				tareWeight: null,
				volume: null,
				unitPrice: null,
				productGroup: null,
				productGroupName: null,
				userDefined1: null,
				userDefined2: null,
				userDefined3: null,
				userDefined4: null,
				userDefined5: null,
				userDefined6: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				factoryCode: null,
				activeFlag: null,
				syncFlag: null,
				primaryUom: null,
				delFlag: null,
				bstme: null,
				basicOrder: null,
				convOrder: null,
				ausme: null,
				basicIssue: null,
				convIssue: null,
				appendFlag: null,
				appendPercent: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.productId)
			this.single = selection.length!==1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加产品信息";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const productId = row.productId || this.ids
			getProduct(productId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改产品信息";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.productId != null) {
						updateProduct(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addProduct(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const productIds = row.productId || this.ids;
			this.$modal.confirm('是否确认删除产品信息编号为"' + productIds + '"的数据项？').then(function() {
				return delProduct(productIds);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download('wms/product/export', {
				...this.queryParams
			}, `product_${new Date().getTime()}.xlsx`)
		}
	}
};
</script>
