<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
                <el-col :span="3">
                    <el-form-item>
                        <el-select v-model="filters.companyId" clearable filterable placeholder="请选择公司">
                            <el-option v-for="item in company" :key="item.id" :label="item.companyName" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item>
                        <el-select v-model="filters.flag" placeholder="请选择查询条件">
                            <el-option label="姓名" value="0"></el-option>
                            <el-option label="用户名" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-form-item>
                    <el-input v-model="filters.keyName" clearable placeholder="请输入姓名或账号进行搜索" style="width:250px;"></el-input>
                </el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getUsers">查询</el-button>
				</el-form-item>
				<el-form-item style="float:right;">
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="list" highlight-current-row :height="tableHeight" v-loading="listLoading" style="width: 100%;">
			<el-table-column type="index" width="60"></el-table-column>
			<el-table-column prop="username" label="姓名" width="150" sortable></el-table-column>
			<el-table-column prop="account" label="用户名" width="150" sortable></el-table-column>
			<el-table-column prop="mobile" label="联系方式" width="150" sortable></el-table-column>
			<el-table-column prop="teamName" label="类型" width="150" sortable></el-table-column>
			<el-table-column prop="companyName" label="公司" sortable></el-table-column>
            <el-table-column prop="roleName" label="角色" sortable></el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope" v-if="user.id == scope.row.parentId">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)" v-if="scope.row.roleName != -1">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)" v-if="scope.row.roleName != -1">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :page-sizes="[20 , 50 , 80 , 100 , 200]"
                :page-size="20"
                layout="total, sizes, prev, pager, next"
                :total="total"
                style="float:right;">
            </el-pagination>
		</el-col>

        <!--新增界面-->
		<el-dialog title="新增人员" v-if="addFormVisible" :visible.sync="addFormVisible" :close-on-click-modal="false" customClass='customWidth'>
			<el-form :model="addForm" label-width="100px" :rules="formRules" ref="addForm" :inline="true" class="demo-form-inline">
				<el-form-item label="姓名" prop="username">
					<el-input v-model="addForm.username" autocomplete="off" placeholder="请输入姓名"></el-input>
				</el-form-item>
				<el-form-item label="用户名" prop="account">
					<el-input v-model="addForm.account" autocomplete="off" placeholder="请输入用户名（登录账号）"></el-input>
				</el-form-item>
				<el-form-item label="联系方式" prop="mobile">
					<el-input v-model="addForm.mobile" autocomplete="off" placeholder="请输入联系方式"></el-input>
				</el-form-item>
                <!-- <el-form-item label="类型" prop="teamName" v-if="user.parentId != 0">
					<el-select v-model="addForm.teamName" clearable filterable placeholder="请选择人员类型" @change="choseComp(0)" style="width:202px">
                        <el-option v-for="item in team" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
				</el-form-item> -->
                <el-form-item label="公司" prop="companyId">
					<el-select v-model="addForm.companyId" clearable filterable placeholder="请选择所属公司" style="width:202px">
                        <el-option v-for="item in company" :key="item.id" :label="item.companyName" :value="item.id">
                        </el-option>
                    </el-select>
				</el-form-item>
                <el-form-item label="角色" prop="roleName">
                    <el-input v-model="addForm.roleName" autocomplete="off" placeholder="请输入角色名称"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
                <span style="color:#f00;float:left;margin-left:60px;">初始密码：000000</span>
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--编辑界面-->
		<el-dialog title="编辑人员" v-if="editFormVisible" :visible.sync="editFormVisible" :close-on-click-modal="false" customClass='customWidth'>
			<el-form :model="editForm" label-width="80px" :rules="formRules" ref="editForm" :inline="true" class="demo-form-inline">
				<el-form-item label="姓名" prop="username">
					<el-input v-model="editForm.username" autocomplete="off" placeholder="请输入姓名"></el-input>
				</el-form-item>
				<el-form-item label="用户名" prop="account">
					<el-input v-model="editForm.account" autocomplete="off" placeholder="请输入用户名（登录账号）"></el-input>
				</el-form-item>
				<el-form-item label="联系方式" prop="mobile">
					<el-input v-model="editForm.mobile" autocomplete="off" placeholder="请输入联系方式"></el-input>
				</el-form-item>
                <!-- <el-form-item label="类型" prop="teamName" v-if="user.parentId != 0">
					<el-select v-model="editForm.teamName" clearable filterable placeholder="请选择人员类型" @change="choseComp(1)" style="width:202px">
                        <el-option v-for="item in team" :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
				</el-form-item> -->
                <el-form-item label="公司" prop="companyId">
					<el-select v-model="editForm.companyId" clearable filterable placeholder="请选择所属公司" style="width:202px">
                        <el-option v-for="item in company" :key="item.id" :label="item.companyName" :value="item.id">
                        </el-option>
                    </el-select>
				</el-form-item>
                <el-form-item label="角色" prop="roleName">
                    <el-input v-model="editForm.roleName" autocomplete="off" placeholder="请输入角色名称"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
                 <span style="color:#f00;float:left;margin-left:60px;">初始密码：000000</span>
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'

	export default {
		data() {
            var checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('请输入联系方式'));
                } else {
                    const reg = /^1[0-9]\d{9}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error('请输入正确的联系方式'));
                    }
                }
            };
			return {
				filters: {
                    keyName: '',
                    roleName: '',
                    flag: '姓名',
                    companyId: ''
                },
                
                user: JSON.parse(sessionStorage.getItem('user')),
                team: [{label:'资产方',value:0},{label:'生产方',value:1}],
                company: [],

				list: [],
				total: 0,
                page: 1,
                size: 20,
                listLoading: false,
                tableHeight: 0,

                formRules: {
                    username: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
                    ],
                    account: [
						{ required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    mobile: [
                        { required: true, validator: checkPhone, trigger: 'blur'}
                    ],
                    teamName: [
						{ required: true, message: '请选择人员类型', trigger: ["blur",'change'] }
					],
                    companyId: [
						{ required: true, message: '请选择所属公司', trigger: ["blur",'change'] }
					],
                    roleName: [
						{ required: true, message: '请输入角色名称', trigger: ["blur",'change'] }
					]
                },

                //新增界面是否显示
                addFormVisible: false,
				addLoading: false,
				//新增界面数据
				addForm: {
					username: '',
                    account: '',
                    mobile: '',
                    //teamName: '',
					companyId: '',
                    roleName: '',
                    flag: 0
				},

                //编辑界面是否显示
				editFormVisible: false,
				editLoading: false,
				//编辑界面数据
				editForm: {
					id: 0,
					username: '',
                    account: '',
                    mobile: '',
                    // teamName: '',
					companyId: '',
                    roleName: '',
                    flag: 1
				}
			}
		},
		methods: {
            // 获取基础数据
            getMsg(){
                this.http.post(this.port.base.companys, {
                    parentId: this.user.parentId,
                    id: this.user.id
                }, res => {
                    if (res.code == "ok") {
                        this.company = res.data;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })
            },

            //  分页
			handleCurrentChange(val) {
				this.page = val;
				this.getUsers();
            },

            handleSizeChange(val) {
                this.size = val;
				this.getUsers();
            },

			//获取用户列表
			getUsers() {
				this.listLoading = true;
                this.http.post(this.port.project.userList, {
                    keyName: this.filters.keyName,
                    currentPage: this.page,
                    pageSize: this.size,
                    companyId: this.filters.companyId==''?-1:this.filters.companyId,
                    flag: this.filters.flag=='姓名'?0:this.filters.flag
                }, res => {
                    this.listLoading = false;
                    if (res.code == "ok") {
                        this.list = res.data.list;
                        this.total = res.data.total;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.listLoading = false;
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })
            },
            
            //显示新增界面
			handleAdd: function () {
                this.addFormVisible = true;
				this.addForm = {
                    parentId: this.user.id,
					username: '',
                    account: '',
                    mobile: '',
                    // teamName: '',
					companyId: '',
                    roleName: '',
                    flag: 0
				};
            },

            //新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.addLoading = true;
                        this.http.post(this.port.project.addUser, this.addForm , res => {
                            this.addLoading = false;
                            this.addFormVisible = false;
                            if (res.code == "ok") {
                                this.$message({
                                    message: '创建成功',
                                    type: 'success'
                                });
                                this.getUsers();
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.addLoading = false;
                            this.addFormVisible = false;
                            this.$message({
                                message: error,
                                type: 'error'
                            });
                        })
					}
				});
			},

			//删除
			handleDel: function (index, row) {
                this.$confirm('确认删除该人员吗?', '提示', {
					type: 'warning'
				}).then(() => {
                    this.http.post(this.port.project.delUser, {
                        id: row.id
                    }, res => {
                        if (res.code == "ok") {
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();
                        } else {
                            this.$message({
                                message: res.msg,
                                type: 'error'
                            });
                        }
                    }, error => {
                        this.$message({
                            message: error,
                            type: 'error'
                        });
                    })
				});
            },
            
			//显示编辑界面
			handleEdit: function (index, row) {
                this.editFormVisible = true;
				this.editForm = {
                    id: row.id,
                    parentId: this.user.id,
					username: row.username,
                    account: row.account,
                    mobile: row.mobile,
                    // teamName: row.teamName,
					companyId: row.companyId,
                    roleName: row.roleName,
                    flag: 1
				};
            },
            
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
                        this.editLoading = true;
                        this.http.post(this.port.project.addUser, this.editForm , res => {
                            this.editLoading = false;
                            this.editFormVisible = false;
                            if (res.code == "ok") {
                                this.$message({
                                    message: '修改成功',
                                    type: 'success'
                                });
                                this.getUsers();
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.editLoading = false;
                            this.editFormVisible = false;
                            this.$message({
                                message: error,
                                type: 'error'
                            });
                        })
					}
				});
			}
        },
        created() {
            let height = window.innerHeight;
            this.tableHeight = height - 210;
            const that = this;
            window.onresize = function temp() {
                that.tableHeight = window.innerHeight - 210;    
            };
        },
		mounted() {
            this.getMsg();
			this.getUsers();
        }
	}
</script>

<style scoped>
    
</style>