<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="teamCode">
				<el-input
					v-model="queryParams.teamCode"
					placeholder="请输入班组编码"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="班组名称" prop="teamDescGlobal">
				<el-input
					v-model="queryParams.teamDescGlobal"
					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:team: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:team: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:team: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:team:export']"
				>导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="teamList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			 <!-- 序号 -->
      <el-table-column
            type="index"
            width="90"
            align="center"
            :index="indexMethod"
            label="序号">
      </el-table-column>
			<el-table-column label="班组编码" align="center" prop="teamCode" />
			<el-table-column label="班组名称" align="center" prop="teamDescGlobal" />
			<el-table-column prop="teamType" label="班组类别" align="center" >
        <template slot-scope="scope">
            {{ scope.row.teamType == "team_type1" ? "生产班组" : "检验班组" }}
        </template>
      </el-table-column>
			<el-table-column label="所属产线" align="center" prop="productionLineCode" />
			<el-table-column label="创建人" align="center" prop="createBy" />
			<el-table-column label="创建时间" align="center" prop="createDate" />
			<el-table-column prop="active" label="是否启用" align="center">
        <template slot-scope="scope">
          {{ scope.row.active == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
			<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:team:edit']"
					>修改</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
						v-hasPermi="['wms:team: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="110px">
				<el-form-item label="班组编码" prop="teamCode">
					<el-input v-model="form.teamCode" placeholder="请输入班组编码" />
				</el-form-item>
				<el-form-item label="班组简称" prop="teamDesc">
					<el-input v-model="form.teamDesc" placeholder="请输入班组简称" />
				</el-form-item>
				<el-form-item label="班组名称" prop="teamDescGlobal">
					<el-input v-model="form.teamDescGlobal" placeholder="请输入班组通用名称" />
				</el-form-item>
				</el-form-item>
        <el-form-item label="班组类型" prop="teamType">
        	<el-select v-model="form.teamType" placeholder="请选择班组">
            <el-option
              v-for="dict in dict.type.team_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
				<el-form-item label="班组扩展名称" prop="teamDescExtended">
					<el-input v-model="form.teamDescExtended" placeholder="请输入班组扩展名称" />
				</el-form-item>
				<el-form-item label="是否启用" prop="active">
					<el-input v-model="form.active" placeholder="请输入是否启用" />
				</el-form-item>
				<el-form-item label="企业编码" prop="enterpriseCode">
					<el-input v-model="form.enterpriseCode" placeholder="请输入企业编码" />
				</el-form-item>
				<el-form-item label="工厂编码" prop="siteCode">
					<el-input v-model="form.siteCode" placeholder="请输入工厂编码" />
				</el-form-item>
				<el-form-item label="所属产线" prop="productionLineCode">
					<el-input v-model="form.productionLineCode" placeholder="请输入线体编码" />
				</el-form-item>
				<el-form-item label="班组长编码" prop="teamLeaderCode">
					<el-input v-model="form.teamLeaderCode" placeholder="请输入班组长编码" />
				</el-form-item>
				<el-form-item label="定编岗位数量" prop="planPostQuantity">
					<el-input v-model="form.planPostQuantity" placeholder="请输入定编岗位数量" />
				</el-form-item>
				<el-form-item label="已分配岗位数量" prop="actualPostQuantity">
					<el-input v-model="form.actualPostQuantity" placeholder="请输入已分配岗位数量" />
				</el-form-item>
				<el-form-item label="班组属性" prop="teamProperties">
					<el-input v-model="form.teamProperties" placeholder="请输入班组属性" />
				</el-form-item>
				<el-form-item label="应出勤人数" prop="attendanceQuantity">
					<el-input v-model="form.attendanceQuantity" placeholder="请输入应出勤人数" />
				</el-form-item>
				<el-form-item label="已分配人数" prop="assignedQuantity">
					<el-input v-model="form.assignedQuantity" 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 { listTeam, getTeam, delTeam, addTeam, updateTeam } from "@/api/wms/team";

export default {
	name: "Team",
	dicts: ['sys_normal_disable', 'team_type'],
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 班组表格数据
			teamList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				orgId: null,
				teamCode: null,
				teamDesc: null,
				teamDescGlobal: null,
				teamDescExtended: null,
				createBy: null,
				createDate: null,
				lastUpdateBy: null,
				lastUpdateDate: null,
				Active: null,
				enterpriseId: null,
				enterpriseCode: null,
				siteId: null,
				siteCode: null,
				productionLineCode: null,
				teamLeaderCode: null,
				planPostQuantity: null,
				actualPostQuantity: null,
				salaryType: null,
				teamProperties: null,
				attendanceQuantity: null,
				assignedQuantity: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
      	teamCode: [
      	  { required: true, message: "班组编码不能为空", trigger: "blur" }
      	],
      	teamDescGlobal: [
      		{ required: true, message: "班组名称不能为空", trigger: "blur" }
      	],
      	productionLineCode: [
      		{ required: true, message: "所属产线不能为空", trigger: "blur" }
      	],
      	active: [
      		{ required: true, message: "是否启用不能为空", trigger: "blur" }
      	],
      }
		};
	},
	created() {
		this.getList();
	},
	methods: {
	  // 生成表头序号
    indexMethod(index) {
      return index + 1;
    },
		/** 查询班组列表 */
		getList() {
			this.loading = true;
			listTeam(this.queryParams).then(response => {
				this.teamList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				teamId: null,
				orgId: null,
				teamCode: null,
				teamDesc: null,
				teamDescGlobal: null,
				teamDescExtended: null,
				createBy: null,
				createDate: null,
				lastUpdateBy: null,
				lastUpdateDate: null,
				Active: null,
				enterpriseId: null,
				enterpriseCode: null,
				siteId: null,
				siteCode: null,
				productionLineCode: null,
				teamLeaderCode: null,
				planPostQuantity: null,
				actualPostQuantity: null,
				salaryType: null,
				teamProperties: null,
				attendanceQuantity: null,
				assignedQuantity: 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.teamId)
			this.single = selection.length!==1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加班组";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const teamId = row.teamId || this.ids
			getTeam(teamId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改班组";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.teamId != null) {
						updateTeam(this.form).then(response => {
							this.$modal.msgSuccess("修改成功");
							this.open = false;
							this.getList();
						});
					} else {
						addTeam(this.form).then(response => {
							this.$modal.msgSuccess("新增成功");
							this.open = false;
							this.getList();
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const teamIds = row.teamId || this.ids;
			this.$modal.confirm('是否确认删除班组编号为"' + teamIds + '"的数据项？').then(function() {
				return delTeam(teamIds);
			}).then(() => {
				this.getList();
				this.$modal.msgSuccess("删除成功");
			}).catch(() => {});
		},
		/** 导出按钮操作 */
		handleExport() {
			this.download('wms/team/export', {
				...this.queryParams
			}, `team_${new Date().getTime()}.xlsx`)
		}
	}
};
</script>
