<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.projectId" clearable filterable placeholder="请选择项目">
                            <el-option v-for="item in projects" :key="item.id" :label="item.projectName" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
				<el-form-item>
					<el-input v-model="filters.keyName" placeholder="请输入姓名进行搜索"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="getPowerList">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="list" highlight-current-row 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="companyName" label="所属公司" sortable>
			</el-table-column>
			<el-table-column prop="projectName" label="项目名称" width="220" sortable>
			</el-table-column>
			<el-table-column prop="addr" label="上传" align="center" width="150">
                <template slot-scope="scope">
                    <el-checkbox :value="scope.row.update==1?true:false"></el-checkbox>
                </template>
			</el-table-column>
            <el-table-column prop="addr" label="下载" align="center" width="150">
                <template slot-scope="scope">
                    <el-checkbox :value="scope.row.download==1?true:false"></el-checkbox>
                </template>
			</el-table-column>
            <el-table-column prop="addr" label="浏览" align="center" width="150">
                <template slot-scope="scope">
                    <el-checkbox :value="scope.row.view==1?true:false"></el-checkbox>
                </template>
			</el-table-column>
            <el-table-column prop="addr" label="审批" align="center" width="150">
                <template slot-scope="scope">
                    <el-checkbox :value="scope.row.approve==1?true:false"></el-checkbox>
                </template>
			</el-table-column>
			<el-table-column label="操作" width="150" align="center">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</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]"
                :page-size="20"
                layout="total, sizes, prev, pager, next"
                :total="total"
                style="float:right;">
            </el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="编辑权限" v-if="editFormVisible" :visible.sync="editFormVisible" :close-on-click-modal="false" customClass='customWidth'>
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-col :span="24">
                    <el-form-item label="权限配置">
                        <el-row>
                            <el-col :span="6" v-for="(item,index) in roleList" :key="item.val" style="text-align:center;">
                                <el-checkbox v-model="editForm[item.label]" :disabled="index==3?true:false" :checked="editForm[item.label]">{{item.name}}</el-checkbox>
                            </el-col>
                        </el-row>
                    </el-form-item>
                </el-col>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<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() {
			return {
				filters: {
                    keyName: '',
                    projectId: '',
                },
                user: JSON.parse(sessionStorage.getItem('user')),

                projects: [],

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

				editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                roleList: [
                    {name:'上传',val:0,label:'uploadPower',uploadPower:false},
                    {name:'下载',val:1,label:'dowloadPower',dowloadPower:false},
                    {name:'浏览',val:2,label:'viewPower',viewPower:false},
                    {name:'审批',val:3,label:'approvalPower',approvalPower:false}
                ],
				//编辑界面数据
				editForm: {
                    id: 0,
                    projectId: 0,
                    uploadPower: false,
                    dowloadPower:false,
                    viewPower:false,
                    approvalPower:false,
				}
			}
		},
		methods: {
            getMsg() {
                this.http.post(this.port.project.projects, {}, res => {
                    if (res.code == "ok") {
                        this.projects = res.data;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })
            },

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

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

			//获取权限列表
			getPowerList() {
				this.listLoading = true;
                this.http.post(this.port.project.powerList, {
                    projectId: this.filters.projectId,
                    keyName: this.filters.keyName,
                    pageNum: this.page,
                    pageSize: this.size
                }, res => {
                    this.listLoading = false;
                    if (res.code == "ok") {
                        var list = res.data.list;
                        for(var i in list){
                            list[i].update = 0;
                            list[i].download = 0;
                            list[i].view = 0;
                            list[i].approve = 0;

                            var powers = list[i].powers;
                            for(var j in powers){
                                if(powers[j].powerType == 0){
                                    list[i].update = 1;
                                } else if(powers[j].powerType == 1){
                                    list[i].download = 1;
                                } else if(powers[j].powerType == 2){
                                    list[i].view = 1;
                                } else if(powers[j].powerType == 3){
                                    list[i].approve = 1;
                                }
                            }
                        }
                        this.list = list;
                        this.total = res.data.total;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.listLoading = false;
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })
            },
            
			//显示编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;
				this.editForm = {
                    id: row.userId,
                    projectId: row.projectId,
                    uploadPower: row.update==1?true:false,
                    dowloadPower: row.download==1?true:false,
                    viewPower: row.view==1?true:false,
                    approvalPower: row.approve==1?true:false
                };
			},
			//编辑
			editSubmit: function () {
                var str = "";

                if(this.editForm.uploadPower) {
                    str += "0,"
                }
                if(this.editForm.dowloadPower) {
                    str += "1,"
                }
                if(this.editForm.viewPower) {
                    str += "2,"
                }
                if(this.editForm.approvalPower) {
                    str += "3,"
                }

                str = str.substring(0,str.length-1)

				this.editLoading = true;
                this.http.post(this.port.project.powerUpdate, {
                    id: this.editForm.id,
                    projectId: this.editForm.projectId,
                    powers: str
                }, res => {
                    this.editLoading = false;
                    this.editFormVisible = false;
                    if (res.code == "ok") {
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                        this.getPowerList();
                    } 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.getPowerList();
		}
	}

</script>

<style>
    .el-table__row .el-checkbox__inner:hover {
        border-color: #DCDFE6;
    }

    .el-table__row .el-checkbox , .el-table__row .el-checkbox__input {
        cursor: inherit;
    }
</style>