<template>
    <Modal
        :loading="loading"
        :styles="styles"
        :title="title"
        :fullscreen="false"
        class-name="modal-table"
        ref="modal"
        :value="modalSwitch"
    >
        <Grid
            :autoInit="false"
            :gridGuid="gridGuid"
            :showActionPane="false"
            :canPagerSelect="true"
            :height="284"
            :rowData="rowData"
            style="height: 400px;"
            ref="grid"
            :defaultPagerSelectedItems="defaultPagerSelectedItems"
        />
        <div slot="footer">
            <div class="all-choose" v-show="isShowAll">
                <i-button type="primary" @click="allChoose" :loading="AllLoading">全部挂接</i-button>
            </div>
            <div>
                <i-button @click="modalSwitch = false">取消</i-button>
                <i-button type="primary" @click="onOk">确定</i-button>
            </div>
        </div>
    </Modal>
</template>
<script>
    import DataApi from '../../../../../../data/common-allocation'
    export default {
        name: 'CommonAllocationTable',
        data() {
            return {
                gridGuid: '',
                loading: true,
                defaultPagerSelectedItems: [],
                modalSwitch: true,
                AllLoading: false
            }
        },

        props: {
            title: {
                type: String,
                default: '分配'
            },

            styles: {
                type: Object,
                default() {
                    return {
                        width: '1000px',
                        top: '20px'
                    }
                }
            },

            guid: {
                type: String
            },
            isShowAll: {
                type: Boolean
            },
            rowData: Object
        },

        created() {
            this.loadConfGrid()
        },

        methods: {
            onOk() {
                let params = {
                    conf_guid: this.guid
                }
                let json = {
                    selectData: this.$refs.grid.getPagerSelectedItems(),
                    rowData: this.rowData
                }
                DataApi.saveDataGrid(params, json).then(res => {
                    this.$Message.success('保存成功')
                    this.loading = false
                    this.$nextTick(() => {
                        this.AllLoading = false
                        this.loading = true
                    })
                    this.modalSwitch = false
                }).catch(err => {
                    this.AllLoading = false
                    this.loading = false
                    this.$nextTick(() => {
                        this.loading = true
                    })
                })
            },
            loadConfGrid() {
                let params = {
                    conf_guid: this.guid,
                    data_guid: this.rowData.guid
                }
                DataApi.loadConfGrid(params, this.rowData).then(res => {
                    this.gridGuid = res.conf.gridGuid
                    this.defaultPagerSelectedItems = res.selected
                    this.$nextTick(() => {
                        this.$refs.grid.init()
                    })
                })
            },
            // 全部挂载
            allChoose() {
                this.AllLoading = true
                this.$refs.grid.fetchAllDataGuids(this.onOk)
            }
        }
    }
</script>
<style lang="less">
    .all-choose{
        float: left;
    }
</style>
