<template>
    <div class="main">
        <div class="search" v-if="!communitStatus">
            <dmp-newsearch :normal="normal" :query="mixins_query" @search="queryList">
                <template slot="companyOrgId">
                    <select-tree
                        class="new-select-tree"
                        selectTreeTitle="请选择公司"
                        placeholder="请选择公司"
                        :options="companyArray"
                        :props="defaultProps"
                        v-model="mixins_query.companyOrgId"
                    />
                </template>
                <div class="search-icon opt" slot="right-opt">
                    <el-tooltip class="item" effect="light" placement="bottom" content="新增">
                        <i class="iconfont" @click="addOrEdit('add')">&#xe641;</i>
                    </el-tooltip>
                </div>
            </dmp-newsearch>
        </div>
        <zz-table
            v-if="!communitStatus"
            :cols="cols"
            :settings="{ showIndex: true, stripe: true }"
            :loading="mixins_onQuery"
            :data="mixins_list"
            :pageset="mixins_pageset"
            @page-change="pageChange"
        >
            <template slot-scope="scope" slot="opt" class="opt">
                <div class="opt">
                    <el-tooltip effect="light" placement="bottom" content="编辑">
                        <i class="zoniot_font zoniot-icon-bianji" @click="addOrEdit('edit', scope.row)"></i>
                    </el-tooltip>
                    <el-tooltip effect="light" placement="bottom" content="删除">
                        <i class="zoniot_font zoniot-icon-shanchu redText" @click="deleteOne(scope.row.id)"></i>
                    </el-tooltip>
                </div>
            </template>
        </zz-table>

        <add-communit v-else :params="activeData" @clerOwnerStatus="clerOwnerStatus" :isAdd="isAdd"></add-communit>
    </div>
</template>

<script>
import list from '@utils/list.js';
import addCommunit from './pageJump/AddOrEdit.vue';
export default {
    mixins: [list],
    components: { addCommunit },
    data() {
        return {
            cols: [
                {
                    label: '社区名称',
                    prop: 'communityName'
                },
                {
                    label: '所属公司',
                    prop: 'companyName'
                },
                {
                    label: '所在地区',
                    prop: 'mangerName'
                },
                {
                    label: '详细地址',
                    prop: 'address'
                },
                {
                    label: '负责人',
                    prop: 'contactPerson'
                },
                {
                    label: '负责人电话',
                    prop: 'phone'
                },
                {
                    label: '操作',
                    prop: 'opt',
                    slot: 'opt'
                }
            ],
            normal: [
                {
                    prop: 'communityName',
                    placeholder: '社区名称'
                },
                {
                    prop: 'companyOrgId',
                    slot: 'companyOrgId'
                }
            ],
            activeData: {},
            isAdd: true,
            communitStatus: '',
            defaultProps: {
                value: 'id', // 唯一标识
                label: 'orgName', // 标签显示
                children: 'orgs' // 子级
            }
        };
    },
    computed: {
        companyArray() {
            return this.$store.getters['getCompanyArray'];
        }
    },
    methods: {
        queryList(type = 'search') {
            this.mixins_search(type);
        },
        clerOwnerStatus() {
            this.communitStatus = '';
            this.activeData = {};
            this.isAdd = true;
            this.mixins_search();
        },
        addOrEdit(todo, row) {
            if (todo == 'edit') {
                this.activeData = row;
                this.isAdd = false;
            }
            this.communitStatus = todo;
        },

        //单个删除
        deleteOne(ids) {
            this.$msgBox(`刪除社区`, '删除后将无法恢复，请问是否继续?')
                .then(() => {
                    this.$http.get('/assets/community/delete', { id: ids }).then(({ status }) => {
                        if (0 === status) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.mixins_search();
                        } else {
                            this.$message.error('删除失败!');
                        }
                    });
                })
                .catch(() => {});
        },
        getCompany(type) {
            this.$http.postForm('/sc-user-center/org/getOrgTree', { orgType: type }).then(({ status, data, msg }) => {
                if (status === 0 && data) {
                    if (type == 'company') {
                        this.$store.commit('setCompanyArray', data);
                    } else if (type == 'department') {
                        this.$store.commit('setDepartmentArray', data);
                    }
                }
            });
        }
    },
    created() {
        if (this.$store.getters['getCompanyArray'].length === 0) {
            this.getCompany('company');
        }
        if (this.$store.getters['getDepartmentArray'].length === 0) {
            this.getCompany('department');
        }
        this.mixins_post = 'post';
        this.mixins_dataUrl = '/assets/community/page';
        this.mixins_search();
    }
};
</script>

<style lang="scss" scoped>
</style>
