<template>
    <div class="user">
        <Card :bordered="false" dis-hover>
            <bingo-list
                :columns="columns"
                :pageSetting="pageSetting"
                :page="true"
                :isAdvanceTable="false"
                :showBtnType="'all'"
                :showBtnOutNum="2"
                :toolbarNew="true"
                ref="bingoList"
                :searchList="searchList"
                :dataApi="Api"
                :queryData="queryData"
                :isSearch="true"
                :isSearchAdvance="true"
                :showSwitchBtn="false"
                :processData="processData"
                @on-query-submit="querySubmit"
                @on-add-new="onAddNew"
                @on-edit-btn="onEditBtn"
                :showNewBtn="auth.add!==0"
                :isEditBtn="auth.edit!==0"
                :isDeleteBtn="auth.delete!==0"
                v-if="auth.table!==0"
            >
            <!-- 高级搜索 -->
                <template slot="queryForm">
                    <bingo-grid-item>
                        <bingo-form-item label="机构名称" prop="nameCn">
                            <Input
                                v-model="queryData.nameCn"
                                placeholder="请输入机构名称"
                            />
                        </bingo-form-item>
                    </bingo-grid-item>
                    <bingo-grid-item>
                        <bingo-form-item label="机构类型" prop="orgType">
                            <Select v-model="queryData.orgType">
                                <Option v-for="item in orgTypeList" :value="item.dictCode" :key="item.dictCode">{{ item.dictValue }}</Option>
                            </Select>
                        </bingo-form-item>
                    </bingo-grid-item>
                </template>
            </bingo-list>
        </Card>
        <Modal v-model="isShowNewForm" :title="isAdd==true?'新建':'修改'" @on-ok="onOk" :width="900">
            <Form ref="formAdd" :model="formAdd" label-position="right" :label-width="90">
                <Row>
                    <Col :span="12">
                        <bingo-form-item label="机构名称">
                            <Input type="text" v-model="formAdd.nameCn" placeholder="请输入机构名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="德文名称">
                            <Input type="text" v-model="formAdd.nameDe" placeholder="请输入德文名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="英文名称">
                            <Input type="text" v-model="formAdd.nameEn" placeholder="请输入英文名称">
                            </Input>
                        </bingo-form-item>
                    </Col>
                    <Col :span="12">
                        <bingo-form-item label="机构类型">
                            <Select v-model="formAdd.orgType">
                                <Option v-for="item in orgTypeList" :value="item.dictCode" :key="item.dictCode">{{ item.dictValue }}</Option>
                            </Select>
                        </bingo-form-item>
                    </Col>
                </Row>
            </Form>
        </Modal>
    </div>
</template>
<script>
    import bingoList from '@/components/bingo-list';
    import Api from '@/api/system/organization';
    import dictApi from '@/api/platform/dict';
    import module from '@/mixins/module';
    import util from '@/utils/tool'
    export default {
        name:'plat-org',
        data () {
            return {
                columns: [
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
                    {
                        title: '机构名称',
                        key: 'nameCn'
                    },
                    {
                        title: '机构类型',
                        key: 'orgType'
                    },
                    {
                        title: '创建人',
                        key: 'creator'
                    },
                    {
                        title: '创建时间',
                        key: 'createTime'
                    },
                    {
                        title: '修改人',
                        key: 'modifier'
                    },
                    {
                        title: '修改时间',
                        key: 'modifyTime'
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        align: 'center',
                        width: 146
                    }
                ],
                Api: Api,
                pageSetting: {
                    pageNum: 1,
                    pageSize: 20,
                    showSizer: true
                },
                // 搜索的字段
                searchList: [
                    {
                        label: '机构名称',
                        value: 'nameCn',
                        type: 'input'
                    },
                    {
                        label: '机构类型',
                        value: 'orgType',
                        type: 'input'
                    }
                ],
                tableData: [],
                queryData: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    orgType: '',
                    sn: ''
                },
                queryObj: {},
                isShowNewForm: false,
                formAdd: {
                    nameCn: '',
                    nameDe: '',
                    nameEn: '',
                    orgType: '',
                    sn: ''
                },
                grid: {
                    xl: 12,
                    lg: 13,
                    md: 13,
                    sm: 24,
                    xs: 24
                },
                isAdd: true,
                orgTypeList: []
            };
        },
        created () {
            // this.getAuth();
            this.auth = JSON.parse(localStorage.getItem('auth'));
        },
        mixins: [module],
        methods: {
            // 获取权限
            // async getAuth () {
            //     let auth = await this.getPermission();
            //     this.auth = JSON.parse(JSON.stringfy(auth));
            // },
            // 机构类型
            getTypeList () {
                let data = {
                    dictGroup: 'platform',
                    dictType: 'org_type'
                }
                return new Promise(function (resolve, reject) {
                    dictApi.tableList(data).then(res => {
                        resolve(res.rows);
                    })
                })
            },
            // 弹出层查询
            querySubmit (obj) {
                if (obj) {
                    this.queryObj = obj;
                }
                this.$refs.bingoList.getTableData(this.queryObj);
            },
            // 新建
            async onAddNew () {
                this.isAdd = true;
                for (let i in this.formAdd) {
                    if (Array.isArray(this.formAdd[i])) {
                        this.formAdd[i] = [];
                    } else {
                        this.formAdd[i] = '';
                    }
                }
                this.formAdd.sn = await this.getSn();
                if (this.orgTypeList.length === 0) {
                    this.orgTypeList = await this.getTypeList();
                }
                this.isShowNewForm = true;
            },
            // 新建确定
            onOk () {
                if (this.isAdd) {
                    Api.addTable(this.formAdd).then(res => {
                        this.$Message.success('新建成功');
                        this.$refs.bingoList.getTableData();
                    })
                } else {
                    Api.editTable(this.formAdd).then(res => {
                        this.$Message.success('修改成功');
                        this.$refs.bingoList.getTableData();
                    })
                }
            },
            // 编辑
            async onEditBtn (id) {
                this.isAdd = false;
                if (this.orgTypeList.length === 0) {
                    this.orgTypeList = await this.getTypeList();
                }
                Api.getTableById(id).then(res => {
                    this.formAdd = res;
                    this.isShowNewForm = true;
                })
            },
            // 处理数据
            processData (data) {
                data.forEach(item => {
                    if (item.createTime) {
                        item.createTime = util.format(new Date(item.createTime), 'yyyy-MM-dd');
                    }
                    if (item.modifyTime) {
                        item.modifyTime = util.format(new Date(item.modifyTime), 'yyyy-MM-dd')
                    }
                });
            }
        },
        components: { bingoList }
    };
</script>
