<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="$t('basic.inputAss')" clearable></el-input>
                </el-form-item>
				<el-form-item>
					<el-button type="primary" @click.native="getComp">{{$t('base.query')}}</el-button>
				</el-form-item>
				<el-form-item style="float:right;">
					<el-button type="primary" @click.native="handleAdd">{{$t('base.add')}}</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="companyName" :label="$t('basic.assName')" width="250" sortable>
            </el-table-column>
            <el-table-column prop="administrator" :label="$t('basic.assManager')" width="150" sortable>
                <template slot-scope="scope">
                    <router-link to="/staff" tag="span" style="cursor: pointer; color: #409eff;">{{scope.row.administrator}}</router-link>
                </template>
            </el-table-column>
            <el-table-column prop="relateCompanyList" :label="$t('basic.assProducer')" sortable>
                <template slot-scope="scope">
                    <span class="info" v-for="(item, index) in scope.row.relateCompanyList">
                        {{item.companyName}}
                        <span v-if="index != scope.row.relateCompanyList.length-1">、</span>
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="companyAddress" :label="$t('basic.assAddress')" sortable></el-table-column>
			<el-table-column :label="$t('base.operate')" width="200">
				<template slot-scope="scope">
					<el-button size="small" @click.native="handleEdit(scope.$index, scope.row)">{{$t('base.edit')}}</el-button>
					<el-button type="danger" size="small" @click.native.native="handleDel(scope.$index, scope.row)">{{$t('el.upload.delete')}}</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="addTitle" v-if="addFormVisible" :visible.sync="addFormVisible" :close-on-click-modal="false" customClass='customWidth'>
			<el-form :model="addForm" :rules="formRules" ref="addForm">
				<el-form-item label-width="150px" :label="$t('basic.assName')" prop="companyName" v-show="addState">
					<el-input v-model="addForm.companyName" autocomplete="off" :placeholder="$t('basic.inputAssName')" style="width:485px"></el-input>
				</el-form-item>
                <el-form-item  label-width="150px" :label="$t('basic.assProducer')" v-show="addState">
                    <el-select v-model="addForm.companyIds" clearable multiple filterable :placeholder="$t('basic.inputAssProducer')" style="width:485px">
                        <el-option v-for="item in companys" :key="item.id" :label="item.companyName" :value="item.id">
                        </el-option>
                    </el-select>
				</el-form-item>
                <el-form-item label-width="150px" :label="$t('basic.assAddress')" prop="companyAddress" v-show="addState">
					<el-input v-model="addForm.companyAddress" autocomplete="off" :placeholder="$t('basic.inputAssAddress')" style="width:485px"></el-input>
				</el-form-item>
                <el-form-item label-width="80px" :label="$t('project.name')" prop="username" v-show="!addState">
					<el-input v-model="addForm.username" autocomplete="off" :placeholder="$t('project.inputName')"></el-input>
				</el-form-item>
				<el-form-item label-width="80px" :label="$t('project.mobile')" prop="account" v-show="!addState">
					<el-input v-model="addForm.account" autocomplete="off" :placeholder="$t('project.inputMobile')"></el-input>
				</el-form-item>
                <el-form-item label-width="80px" :label="$t('project.role')" v-show="!addState">
                    <el-input v-model="addForm.roleName" autocomplete="off" :placeholder="$t('project.inputRole')"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
                <span v-if="!addState" style="color:#f00;float:left;margin-left:60px;">{{$t('project.initial')}}：000000</span>
				<el-button @click.native="addFormVisible = false">{{$t('el.messagebox.cancel')}}</el-button>
                <el-button v-if="addState" type="primary" @click.native="toNext(0)">{{$t('basic.next')}}</el-button>
                <el-button v-if="!addState" type="primary" @click.native="toPart(0)">{{$t('basic.prev')}}</el-button>
				<el-button v-if="!addState" type="primary" @click.native="addSubmit" :loading="addLoading">{{$t('el.messagebox.confirm')}}</el-button>
			</div>
		</el-dialog>

		<!--编辑界面-->
		<el-dialog :title="$t('basic.editAsset')" v-if="editFormVisible" :visible.sync="editFormVisible" :close-on-click-modal="false" customClass='customWidth'>
			<el-form :model="editForm" label-width="150px" :rules="formRules" ref="editForm">
				<el-form-item :label="$t('basic.assName')" prop="companyName">
					<el-input v-model="editForm.companyName" autocomplete="off" :placeholder="$t('basic.inputAssName')" style="width:485px"></el-input>
				</el-form-item>
                <el-form-item :label="$t('basic.assProducer')">
                    <el-select v-model="editForm.companyIds" clearable multiple filterable :placeholder="$t('basic.inputAssProducer')" style="width:485px">
                        <el-option v-for="item in companys" :key="item.id" :label="item.companyName" :value="item.id">
                        </el-option>
                    </el-select>
				</el-form-item>
                <el-form-item :label="$t('basic.assAddress')" prop="companyAddress">
					<el-input v-model="editForm.companyAddress" autocomplete="off" :placeholder="$t('basic.inputAssAddress')" style="width:485px"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">{{$t('el.messagebox.cancel')}}</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">{{$t('el.messagebox.confirm')}}</el-button>
			</div>
		</el-dialog>
	</section>
</template>

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

	export default {
		data() {
            const checkPhone = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error(this.$t('project.inputMobile')));
                } else {
                    const reg = /^1[0-9]\d{9}$/
                    if (reg.test(value)) {
                        callback();
                    } else {
                        return callback(new Error(this.$t('project.inputTrueMobile')));
                    }
                }
            };
            const msg = (rule, value, callback) => {
                if (!value) {
                    switch(rule.field) {
                        case "companyName":
                            return callback(new Error(this.$t('basic.inputAssName')));
                            break;
                        case "companyAddress":
                            return callback(new Error(this.$t('basic.inputAssAddress')));
                            break;
                        case "username":
                            return callback(new Error(this.$t('project.inputName')));
                            break;
                        default:
                            callback();
                    } 
                } else {
                    callback();
                }
            };
			return {
				filters: {
                    keyName: ''
                },

                user: JSON.parse(sessionStorage.getItem('user')),
                companys:[],
				list: [],
				total: 0,
                page: 1,
                size: 20,
                listLoading: false,
                tableHeight: 0,
                
                formRules: {
					companyName: [
						{ required: true, validator: msg , trigger: 'blur' }
                    ],
                    companyAddress: [
                        { required: true, validator: msg , trigger: 'blur' }
                    ],
                    username: [
						{ required: true, validator: msg , trigger: 'blur' }
                    ],
                    account: [
                        { required: true, validator: checkPhone, trigger: 'blur'}
                    ]
                },

                team: [{label:this.$t('basic.asset'),value:0},{label:this.$t('basic.producer'),value:1}],

                // 新增界面
				addFormVisible: false,
                addLoading: false,
                addState: true,
                addTitle: this.$t('basic.addAsset'),
				addForm: {
                    companyName: '',
                    companyAddress: '',
                    companyIds: [],
                    username: '',
                    account: '',
                    companyId: '',
                    roleName: this.$t('basic.system'),
                    flag: 0
				},
                
                // 编辑界面
				editFormVisible: false,
                editLoading: false,
				editForm: {
					id: 0,
                    companyName: '',
                    companyType: 0,
                    companyAddress: '',
                    companyIds: [],
                    flag: 1
				}
			}
		},
		methods: {
            //  分页
			handleCurrentChange(val) {
				this.page = val;
				this.getComp();
            },

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

            //获取基础数据
            getMsg() {
                this.http.post(this.port.base.relationList, {
                    companyType: 1
                }, res => {
                    if (res.code == "ok") {
                        this.companys = res.data;
                    } else {
                        this.$message({
                            message: res.msg,
                            type: 'error'
                        });
                    }
                }, error => {
                    this.$message({
                        message: error,
                        type: 'error'
                    });
                })
            },

			//获取资产方列表
			getComp() {
				this.listLoading = true;
                this.http.post(this.port.base.companyList, {
                    keyName: this.filters.keyName,
                    pageNum: this.page,
                    pageSize: this.size,
                    companyType: 0
                }, 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'
                    });
                })
            }, 

            toNext(i) {
                if(i == 0) {
                    this.addState = false;
                    this.addTitle = this.$t('basic.addSystem');
                }
            },

            toPart(i) {
                if(i == 0) {
                    this.addState = true;
                    this.addTitle = this.$t('basic.addAsset');
                }
            },

            //显示新增界面
			handleAdd() {
                this.addFormVisible = true;
                this.addState = true;
                this.addTitle = this.$t('basic.addAsset');
				this.addForm = {
                    companyName: '',
                    companyAddress: '',
                    companyIds: [],
                    username: '',
                    account: '',
                    companyId: '',
                    roleName: this.$t('basic.system'),
                    flag: 0
				};
            },
            
            //新增
			addSubmit() {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
                        this.addLoading = true;
                        var str = "";
                        for(var i in this.addForm.companyIds){
                            if(i == this.addForm.companyIds.length-1){
                                str += this.addForm.companyIds
                            } else {
                                str += this.addForm.companyIds + ","
                            }
                        }
                        this.http.post(this.port.base.addCompany, {
                            companyName: this.addForm.companyName,
                            companyType: 0,
                            companyAddress: this.addForm.companyAddress,
                            companyIds: str,
                            flag: 0
                        } , res => {
                            if (res.code == "ok") {
                                this.addPeo(res.data.id);
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.addLoading = false;
                            this.addFormVisible = false;
                            this.$message({
                                message: error,
                                type: 'error'
                            });
                        })
					}
				});
            },

            //添加人员
            addPeo(companyId) {
                this.$refs.addForm.validate((valid) => {
					if (valid) {
                        this.http.post(this.port.project.addUser, {
                            username: this.addForm.username,
                            account: this.addForm.account,
                            companyId: companyId,
                            roleName: this.addForm.roleName,
                            parentId: this.user.id,
                            addType: 0,
                            flag: 0
                        } , res => {
                            this.addLoading = false;
                            if (res.code == "ok") {
                                this.addFormVisible = false;
                                this.$message({
                                    message: this.$t('prompt.success2'),
                                    type: 'success'
                                });
                                this.getComp();
                            } else {
                                this.$message({
                                    message: res.msg,
                                    type: 'error'
                                });
                            }
                        }, error => {
                            this.addLoading = false;
                            this.addFormVisible = false;
                            this.$message({
                                message: error,
                                type: 'error'
                            });
                        })
					}
				});
            },
            
			//删除
			handleDel(index, row) {
				this.$confirm(this.$t('basic.delAss'), this.$t('el.messagebox.title'), {
					confirmButtonText: this.$t('el.messagebox.confirm'),
                    cancelButtonText: this.$t('el.messagebox.cancel'),
                    type: 'warning'
				}).then(() => {
                    this.http.post(this.port.base.delCompany, {
                        id: row.id
                    }, res => {
                        if (res.code == "ok") {
                            this.$message({
                                message: this.$t('prompt.success3'),
                                type: 'success'
                            });
                            this.getComp();
                        } else {
                            this.$message({
                                message: res.msg,
                                type: 'error'
                            });
                        }
                    }, error => {
                        this.$message({
                            message: error,
                            type: 'error'
                        });
                    })
				});
            },
            
			//显示编辑界面
			handleEdit(index, row) {
                this.editFormVisible = true;
                var array = [];
                for(var i in row.relateCompanyList) {
                    array.push(row.relateCompanyList[i].id)
                }
                this.editForm = {
                    id: row.id,
                    companyName: row.companyName,
                    companyType: 0,
                    companyAddress: row.companyAddress,
                    companyIds: array,
                    flag: 1
				};
            },
            
			//编辑
			editSubmit() {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
                        this.editLoading = true;
                        var str = "";
                        for(var i in this.editForm.companyIds){
                            if(i == this.editForm.companyIds.length-1){
                                str += this.editForm.companyIds
                            } else {
                                str += this.editForm.companyIds + ","
                            }
                        }
                        this.http.post(this.port.base.addCompany, {
                            id: this.editForm.id,
                            companyName: this.editForm.companyName,
                            companyType: 0,
                            companyAddress: this.editForm.companyAddress,
                            companyIds: str,
                            flag: 1
                        } , res => {
                            this.editLoading = false;
                            if (res.code == "ok") {
                                this.editFormVisible = false;
                                this.$message({
                                    message: this.$t("prompt.success"),
                                    type: 'success'
                                });
                                this.getComp();
                            } 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.getComp();
        }
	}
</script>

<style scoped>
    
</style>