<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyName" placeholder="请输入项目名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getProject">查询</el-button>
				</el-form-item>
				<el-form-item style="float:right;" v-if="user.parentId == 1 && user.subordinateType == 0">
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="list" highlight-current-row v-loading="listLoading" :height="tableHeight" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="projectName" label="项目名称" width="250" sortable>
                <template slot-scope="scope">
                    <el-link :underline="false" type="primary" @click="toDetail(scope.row)">{{scope.row.projectName}}</el-link>
				</template>
			</el-table-column>
            <el-table-column prop="ownerCompanyName" label="资产方公司" sortable>
			</el-table-column>
            <el-table-column prop="customCompaniesStr" label="生产方公司" width="450">
			</el-table-column>
            <el-table-column prop="manager" label="项目经理" width="100" align="center" sortable>
			</el-table-column>
			<el-table-column prop="indate" label="创建时间" width="250" align="center" sortable>
			</el-table-column>
			<!-- <el-table-column label="操作" width="250" align="center">
				<template slot-scope="scope">
                    <el-button size="small" type="primary" @click="toDetail(scope.row.id)">详情</el-button>
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(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="addFormVisible" :visible.sync="addFormVisible" :close-on-click-modal="false" customClass='customWidth'>
			<el-form :model="addForm" label-width="120px" :rules="formRules" ref="addForm">
                <el-col :span="24">
                    <el-form-item label="项目名称" prop="projectName">
                        <el-input v-model="addForm.projectName" autocomplete="off" placeholder="请输入项目名称" style="width:510px"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="项目模具" prop="mold">
                        <el-select v-model="addForm.mold" @change="companyChange" clearable filterable multiple placeholder="请选择项目模具" value-key='id' style="width:510px">
                            <el-option v-for="item in molds" :key="item.id" :label="item.modelName" :value="item">
                                <span style="float: left">{{ item.modelName }}</span>
                                <span style="float: right; color: #8492a6; font-size: 6px;margin-right:17px;">{{ item.produceCompany }}</span>
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="项目经理" prop="managerId">
                        <el-select v-model="addForm.managerId" clearable filterable placeholder="请选择项目经理" value-key='id' style="width:510px">
                            <!-- <el-option v-for="item in charger" :key="item.id" :label="item.username" :value="item">
                            </el-option> -->
                            <el-option v-for="item in charger" :key="item.id" :label="item.username" :value="item">
                                <span style="float: left">{{ item.username }}</span>
                                <span style="float: right; color: #8492a6; font-size: 13px;">{{ item.companyName }}</span>
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

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

	export default {
		data() {
			return {
				filters: {
					keyName: ''
                },
                user: JSON.parse(sessionStorage.getItem('user')),
                charger: [],
                molds: [],

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

                formRules: {
					projectName: [
						{ required: true, message: '请输入项目名称', trigger: 'blur' }
                    ],
                    mold: [
                        { required: true, message: '请选择项目模具', trigger: ['blur','change'] }
                    ],
                    managerId: [
                        { required: true, message: '请选择项目经理', trigger: ['blur','change'] }
                    ]
                },
                
                addFormVisible: false, //新增界面是否显示
				addLoading: false,
				//新增界面数据
				addForm: {
					projectName: '',
                    mold: [],
                    managerId: ''
				}

			}
		},
		methods: {
            //获取基础数据
            getMsg(){
                this.http.post(this.port.project.getUserById, {
                    companyIds: this.user.companyId,
                    id: this.user.id
                } , res => {
                    if (res.code == "ok") {
                        this.charger = res.data;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })

                //获取模具
                this.getMoulds();
            },

            //获取模具
            getMoulds(){
                this.http.post(this.port.mold.modelList, {
                    parentId: this.user.parentId,
                    id: this.user.id
                }, res => {
                    if (res.code == "ok") {
                        this.molds = res.data;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })
            },

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

            handleSizeChange(val) {
                this.size = val;
				this.getProject();
            },
            
			//获取项目列表
			getProject() {
				this.listLoading = true;
				this.http.post(this.port.project.projectList, {
                    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){
                            var customCompaniesStr = "";
                            for(var j in list[i].customCompanies){
                                if(j == list[i].customCompanies.length -1){
                                    customCompaniesStr += list[i].customCompanies[j].companyName;
                                } else {
                                    customCompaniesStr += list[i].customCompanies[j].companyName + "、";
                                }
                            }
                            list[i].customCompaniesStr = customCompaniesStr;
                        }
                        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'
                    });
                })
            },

            //详情
            toDetail(row) {
                this.$router.push('/project/' + row.id);
            },

            //选择公司切换人员
            companyChange() {
                var param = {} ,
                    str = this.user.companyId;
                for(var i in this.addForm.mold){
                    str += "," + this.addForm.mold[i].produceCompanyId;
                }
                param.companyIds = str;
                param.id = this.user.id
                this.http.post(this.port.project.getUserById, param , res => {
                    if (res.code == "ok") {
                        this.charger = res.data;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })
            },

            //显示新增界面
			handleAdd() {
                //重新获取一下模具
                this.getMoulds();
				this.addFormVisible = true;
				this.addForm = {
					projectName: '',
                    mold: [],
                    managerId: ''
				};
            },

            //新增
			addSubmit() {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
                        var cId = "",
                            cName = "",
                            mId = "";

                        for(var i in this.addForm.mold){
                            if(cId.indexOf(this.addForm.mold[i].produceCompanyId) == -1){
                                cId += this.addForm.mold[i].produceCompanyId + ",";
                                cName += this.addForm.mold[i].produceCompanyName + ",";
                            }
                            mId += this.addForm.mold[i].id + ","
                        }

                        cId = cId.substring(0,cId.length-1);
                        cName = cName.substring(0,cName.length-1);
                        mId = mId.substring(0,mId.length-1);

                        this.addLoading = true;
                        this.http.post(this.port.project.addProject, {
                            projectName: this.addForm.projectName,
                            customerCompanyIds: cId,
                            customerCompanyNames: cName,
                            manager: this.addForm.managerId.username,
                            managerId: this.addForm.managerId.id,
                            modelIds: mId,
                            flag: 0
                        } , res => {
                            this.addLoading = false;
                            if (res.code == "ok") {
                                this.addFormVisible = false;
                                this.$message({
                                    message: '创建成功',
                                    type: 'success'
                                });
                                this.getProject();
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.addLoading = false;
                            this.addFormVisible = 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.getProject();
		}
	}

</script>

<style scoped>

</style>