<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="请输入资产方名称进行搜索" clearable></el-input>
                </el-form-item>
				<el-form-item>
					<el-button type="primary" @click.native="getComp">查询</el-button>
				</el-form-item>
				<el-form-item style="float:right;">
					<el-button type="primary" @click.native="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="companyName" label="资产方名称" width="300" sortable></el-table-column>
            <!-- <el-table-column prop="companyType" label="资产方类型" width="200" sortable>
                <template slot-scope="scope">
                    {{ scope.row.companyType == 0?"资产方":"生产方" }}
                </template>
            </el-table-column> -->
            <el-table-column prop="companyAddress" label="资产方地址" sortable></el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small" @click.native="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click.native.native="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="100px" :rules="formRules" ref="addForm">
				<el-form-item label="资产方名称" prop="companyName">
					<el-input v-model="addForm.companyName" autocomplete="off" placeholder="请输入资产方地址"></el-input>
				</el-form-item>
                <!-- <el-form-item label="公司类型" prop="companyType">
                    <el-select v-model="addForm.companyType" clearable filterable placeholder="请选择公司类型" style="width:533px">
                        <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="companyAddress">
					<el-input v-model="addForm.companyAddress" autocomplete="off" placeholder="请输入资产方地址"></el-input>
				</el-form-item>
			</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>

		<!--编辑界面-->
		<el-dialog title="编辑资产方" v-if="editFormVisible" :visible.sync="editFormVisible" :close-on-click-modal="false" customClass='customWidth'>
			<el-form :model="editForm" label-width="100px" :rules="formRules" ref="editForm">
				<el-form-item label="资产方名称" prop="companyName">
					<el-input v-model="editForm.companyName" autocomplete="off" placeholder="请输入资产方地址"></el-input>
				</el-form-item>
                <!-- <el-form-item label="公司名称" prop="companyType">
                    <el-select v-model="editForm.companyType" clearable filterable placeholder="请选择公司类型" style="width:533px">
                        <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="companyAddress">
					<el-input v-model="editForm.companyAddress" autocomplete="off" placeholder="请输入资产方地址"></el-input>
				</el-form-item>
			</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: ''
                },
				list: [],
				total: 0,
                page: 1,
                size: 20,
                listLoading: false,
                tableHeight: 0,
                
                formRules: {
					companyName: [
						{ required: true, message: '请输入资产方名称', trigger: 'blur' }
                    ],
                    companyType: [
                        { required: true, message:'请选择资产方类型', trigger: ['blur', 'change'] }
                    ],
                    companyAddress: [
                        { required: true, message: '请输入资产方地址', trigger: 'blur' }
                    ]
                },

                team: [{label:'资产方',value:0},{label:'生产方',value:1}],
                // 新增界面
				addFormVisible: false,
				addLoading: false,
				addForm: {
                    companyName: '',
                    companyType: 0,
                    companyAddress: '',
                    flag: 0
				},
                
                // 编辑界面
				editFormVisible: false,
				editLoading: false,
				editForm: {
					id: 0,
                    companyName: '',
                    companyType: 0,
                    companyAddress: '',
                    flag: 1
				}
			}
		},
		methods: {
            //  分页
			handleCurrentChange(val) {
				this.page = val;
				this.getComp();
            },

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

			//获取用户列表
			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'
                    });
                })
            },

            //显示新增界面
			handleAdd() {
                this.addFormVisible = true;
				this.addForm = {
                    companyName: '',
                    companyType: 0,
                    companyAddress: '',
                    flag: 0
				};
            },
            
            //新增
			addSubmit() {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
                        this.addLoading = true;
                        this.http.post(this.port.base.addCompany, this.addForm , res => {
                            this.addLoading = false;
                            if (res.code == "ok") {
                                this.addFormVisible = false;
                                this.$message({
                                    message: '创建成功',
                                    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('确认删除该资产方吗?', '提示', {
					type: 'warning'
				}).then(() => {
                    this.http.post(this.port.base.delCompany, {
                        id: row.id
                    }, res => {
                        if (res.code == "ok") {
                            this.$message({
                                message: '删除成功',
                                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;
                this.editForm = {
                    id: row.id,
                    companyName: row.companyName,
                    companyType: 0,
                    companyAddress: row.companyAddress,
                    flag: 1
				};
            },
            
			//编辑
			editSubmit() {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.editLoading = true;
                        this.http.post(this.port.base.addCompany, this.editForm , res => {
                            this.editLoading = false;
                            if (res.code == "ok") {
                                this.editFormVisible = false;
                                this.$message({
                                    message: '修改成功',
                                    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.getComp();
        }
	}
</script>

<style scoped>
    
</style>