<template>
    <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增党组织' : '更新党组织'" :close-on-click-modal="false"
               :close-on-press-escape="false">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
                 :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'">
            <!--        <el-form-item label="上级组织" prop="orgPid">-->
            <!--            <el-input v-model="dataForm.orgPid" placeholder="上级组织"></el-input>-->
            <!--        </el-form-item>-->
            <el-form-item prop="orgPidName"
                          label="上级组织"
                          class="dept-list">
                <el-popover v-model="orgListVisible"
                            ref="orgListPopover"
                            placement="bottom-start"
                            trigger="click">
                    <el-tree :data="orgList"
                             :props="{ label: 'partyOrgName', children: 'children' }"
                             node-key="id"
                             ref="deptListTree"
                             :highlight-current="true"
                             :expand-on-click-node="false"
                             accordion
                             @current-change="orgListTreeCurrentChangeHandle">
                    </el-tree>
                </el-popover>
                <el-input v-model="dataForm.orgPidName"
                          v-popover:orgListPopover
                          :readonly="true"
                          placeholder="上级分类">
                    <i v-if="dataForm.orgPid !== '0'"
                       slot="suffix"
                       @click.stop="orgListTreeSetDefaultHandle()"
                       class="el-icon-circle-close el-input__icon">
                    </i>
                </el-input>
            </el-form-item>
<!--            <el-form-item label="组织类型" prop="partyOrgType">-->
<!--                <el-input v-model="dataForm.partyOrgType" placeholder="党组织类型"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item label="组织类型" prop="partyOrgType">
                <el-select v-model="dataForm.partyOrgType" @change="changePartyOrgType" clearable placeholder="组织类型">
                    <el-option v-for="item in partyOrgTypeList" :key="item.value" :label="item.name" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="组织名称" prop="partyOrgName">
                <el-input v-model="dataForm.partyOrgName" placeholder="组织名称"></el-input>
            </el-form-item>
            <el-form-item label="组织编码" prop="partyOrgCode">
                <el-input v-model="dataForm.partyOrgCode" placeholder="组织编码"></el-input>
            </el-form-item>
<!--            <el-form-item label="行政组织" prop="agencyId">-->
<!--                <el-input v-model="dataForm.agencyId" placeholder="行政组织"></el-input>-->
<!--            </el-form-item>-->
            <el-form-item prop="agencyName"
                          label="行政组织"
                          v-if="dataForm.partyOrgType != '5'"
                          class="dept-list">
                <el-popover v-model="agencyListVisible"
                            ref="agencyOrgListPopover"
                            placement="bottom-start"
                            trigger="click">
                    <el-tree :data="agencyOrgList"
                             :props="{ label: 'name', children: 'children' }"
                             node-key="id"
                             ref="agencyListTree"
                             :highlight-current="true"
                             :expand-on-click-node="false"
                             accordion
                             @current-change="agencyListTreeCurrentChangeHandle">
                    </el-tree>
                </el-popover>
                <el-input v-model="dataForm.agencyName"
                          v-popover:agencyOrgListPopover
                          :readonly="true"
                          placeholder="行政组织">
<!--                    <i v-if="dataForm.orgPid !== '0'"-->
<!--                       slot="suffix"-->
<!--                       class="el-icon-circle-close el-input__icon">-->
<!--                    </i>-->
                </el-input>
            </el-form-item>
            <el-form-item label="党组织介绍" prop="introduction">
                <el-input v-model="dataForm.introduction" type="textarea" maxlength="500" placeholder="党组织介绍"></el-input>
            </el-form-item>
        </el-form>
        <template slot="footer">
            <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
            <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
        </template>
    </el-dialog>
</template>

<script>
    import debounce from 'lodash/debounce'

    export default {
        data() {
            return {
                visible: false,
                dataForm: {
                    id: '',
                    customerId: '',
                    staffId:'',
                    orgPid: '',
                    orgPids: '',
                    orgPidName: '',
                    agencyId: '',
                    agencyName:'',
                    agencyPids: '',
                    partyOrgType: '',
                    partyOrgName: '',
                    partyOrgCode: '',
                    longitude: '1',
                    latitude: '1',
                    address: '',
                    introduction: '',
                    delFlag: '',
                    revision: '',
                    createdBy: '',
                    createdTime: '',
                    updatedBy: '',
                    updatedTime: ''
                },
                orgListVisible: false,
                agencyListVisible:false,
                orgList: [],
                agencyId: '',
                partyOrgTypeList: [],
                agencyOrgList:[]
            }
        },
        computed: {
            dataRule() {
                return {
                    customerId: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    orgPid: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    orgPids: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    agencyId: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    agencyPids: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    partyOrgType: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    partyOrgName: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    longitude: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    latitude: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    address: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    delFlag: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    revision: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    createdBy: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    createdTime: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    updatedBy: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    updatedTime: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    orgPidName: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ],
                    agencyName: [
                        {required: true, message: this.$t('validate.required'), trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            changePartyOrgType(value){
                if(value != '5'){
                    this.dataForm.agencyId = ''
                    this.dataForm.agencyName = ''
                }
                this.$http.get('/gov/org/customeragency/getOrgTreeByUserAndType',
                    {params:
                            {agencyId: this.agencyId,
                            orgType:value}
                    }).then(({data: res}) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    }
                    this.agencyOrgList = res.data
                }).catch(() => {
                })
            },
            //设置组织类型
            setPartyOrgType() {
                /**
                 * 社区级：community，
                 街道级:street,
                 区县级: district,
                 市级: city
                 省级:province
                 *
                 */
                this.level = localStorage.getItem('level')
                /**
                 * 党组织类型 0省委,1市委,2区委,3党工委,4党委,5支部
                 */
                if (this.level == 'province') {
                    this.partyOrgTypeList = [
                        {value: '0', name: '省委'},
                        {value: '1', name: '市委'},
                        {value: '2', name: '区委'},
                        {value: '3', name: '党工委'},
                        {value: '4', name: '党委'},
                        {value: '5', name: '支部'}
                    ]
                }else if(this.level == 'city'){
                    this.partyOrgTypeList = [
                        {value: '1', name: '市委'},
                        {value: '2', name: '区委'},
                        {value: '3', name: '党工委'},
                        {value: '4', name: '党委'},
                        {value: '5', name: '支部'}
                    ]
                }else if(this.level == 'district'){
                    this.partyOrgTypeList = [
                        {value: '2', name: '区委'},
                        {value: '3', name: '党工委'},
                        {value: '4', name: '党委'},
                        {value: '5', name: '支部'}
                    ]
                }else if(this.level == 'street'){
                    this.partyOrgTypeList = [
                        {value: '3', name: '党工委'},
                        {value: '4', name: '党委'},
                        {value: '5', name: '支部'}
                    ]
                }else if(this.level == 'community'){
                    this.partyOrgTypeList = [
                        {value: '4', name: '党委'},
                        {value: '5', name: '支部'}
                    ]
                }
            },
            // 获取党组织列表
            getOrgList() {
                return this.$http.get('/resi/partymember/icPartyOrg/getTreelist', {params: {agencyId: this.agencyId}}).then(({data: res}) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    }
                    this.orgList = res.data
                }).catch(() => {
                })
            },
            // 上级党组织树, 设置默认值
            orgListTreeSetDefaultHandle() {
                this.dataForm.orgPid = '0'
                this.dataForm.orgPidName = '一级组织'
            },
            // 上级党组织树, 选中
            orgListTreeCurrentChangeHandle(data) {
                this.dataForm.orgPid = data.id
                this.dataForm.orgPidName = data.partyOrgName
                this.dataForm.orgPids = data.orgPids+data.id+","
                //默认设置行政组织id为父节点的行政组织id
                this.dataForm.agencyId = data.agencyId
                this.orgListVisible = false
            },
            // 上级行政组织树, 选中
            agencyListTreeCurrentChangeHandle(data) {
                this.dataForm.agencyId = data.id
                this.dataForm.agencyName = data.name
                this.agencyListVisible = false
            },
            init() {
                this.visible = true
                this.agencyId = localStorage.getItem('agencyId')
                this.dataForm.customerId = localStorage.getItem('customerId')
                this.dataForm.staffId = localStorage.getItem('staffId')
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    //设置党组织类型
                    this.setPartyOrgType()
                    this.getOrgList().then(() => {
                        if (this.dataForm.id) {
                            this.getInfo()
                        } else {
                            this.orgListTreeSetDefaultHandle()
                        }
                    })
                })
            },
            // 获取信息
            getInfo() {
                this.$http.get(`/resi/partymember/icPartyOrg/${this.dataForm.id}`).then(({data: res}) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    }
                    this.dataForm = {
                        ...this.dataForm,
                        ...res.data
                    }
                }).catch(() => {
                })

                //根据组织类型获取行政组织
                this.getInfoAgencyLisy()
            },
            getInfoAgencyLisy(){
                this.$http.get('/gov/org/customeragency/getOrgTreeByUserAndType',
                    {params:
                            {agencyId: this.agencyId,
                                orgType:this.dataForm.partyOrgType}
                    }).then(({data: res}) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    }
                    this.agencyOrgList = res.data
                }).catch(() => {
                })
            },
            // 表单提交
            dataFormSubmitHandle: debounce(function () {
                this.$refs['dataForm'].validate((valid) => {
                    if (!valid) {
                        return false
                    }
                    this.$http[!this.dataForm.id ? 'post' : 'put']('/resi/partymember/icPartyOrg/', this.dataForm).then(({data: res}) => {
                        if (res.code !== 0) {
                            return this.$message.error(res.msg)
                        }
                        this.$message({
                            message: this.$t('prompt.success'),
                            type: 'success',
                            duration: 500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })
                    }).catch(() => {
                    })
                })
            }, 1000, {'leading': true, 'trailing': false})
        }
    }
</script>
<style lang="scss">
    .mod-sys__dept {
        .dept-list {
            .el-input__inner,
            .el-input__suffix {
                cursor: pointer;
            }
        }
    }

    .el-popover {
        height: 320px;
        overflow-y: scroll;
    }

    .el-dialog__footer {
        margin-top: 100px;
    }

    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }

    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }

    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>
