<template>
  <div>
    <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
    <el-dialog title="复制iscs数据" :visible.sync="dialogVisible" width="400px" center>
      <data-form ref="dataform" :form="form" :form-model="formModel" :rules="rules" />
      <span slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="doSave">{{ $t('global.confirm') }}</el-button>
        <el-button @click="close">{{ $t('global.cancel') }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getIscsDataByPage, copyIscsData, deleteIscs} from '@/api/iscs';
import {getPublishMapListOnline } from '@/api/jmap/map';
export default {
    name: 'PublishISCS',
    data() {
        return {
            mapListMap:{},
            mapList:[],
            dialogVisible:false,
            loading:false,
            formModel:{
                fromMapId: '',
                toMapId: ''
            },
            rules: {
                toMapId: [
                    { required: true, message: this.$t('rules.mapInput'), trigger: 'change' }
                ]
            },
            pagerConfig: {
                pageSize: 'pageSize',
                pageIndex: 'pageNum'
            },
            queryForm: {
                labelWidth: '80px',
                reset: true,
                queryObject: {
                    mapId: {
                        type: 'select',
                        label: '地图名称',
                        config: {
                            data: []
                        }
                    }
                }

            },
            queryList: {
                query: getIscsDataByPage,
                selectCheckShow: false,
                indexShow: true,
                columns: [
                    {
                        title: '所属地图',
                        prop: 'mapId',
                        type: 'tag',
                        columnValue: (row) => { return this.covertMap(row); },
                        tagType: (row) => { return 'success'; }
                    },
                    {
                        title: '父系统',
                        prop: 'totalSystem'
                    },
                    {
                        title: '子系统',
                        prop: 'system'
                    },
                    {
                        title: '子图',
                        prop: 'userInterface'
                    },
                    {
                        type: 'button',
                        title: this.$t('global.operate'),
                        width: '300',
                        buttons: [
                            // {
                            //     name: this.$t('publish.copy'),
                            //     handleClick: this.handleCopy
                            // },
                            {
                                name: this.$t('global.delete'),
                                handleClick: this.handleDelete,
                                type: 'danger',
                                showControl: () => { return this.isShow != -1; }
                            }
                        ]
                    }
                ],
                actions: [
                    { text: '复制', btnCode: 'copy', handler: this.handleCopy }
                ]
            }
        };
    },
    computed: {
        isShow() {
            return this.$store.getters['roles'].indexOf('05');
        },
        form() {
            return {
                labelWidth: '100px',
                items: [
                    { prop: 'fromMapId', label: '数据来源', type: 'select', required: true, options: this.covertOtherData()},
                    { prop: 'toMapId', label: '复制到', type: 'select', required: true, options: this.covertData()}
                ]
            };
        }
    },
    created() {
        this.loadInitData();
    },
    methods:{
        async loadInitData() {
            const res = await getPublishMapListOnline();
            res.data.forEach(elem=>{
                this.mapList.push({label:elem.name, value:parseInt(elem.id)});
                this.queryForm.queryObject.mapId.config.data.push({label:elem.name, value:elem.id});
                this.mapListMap[elem.id] = elem.name;
            });
        },
        covertData() {
            return this.mapList.filter(data=>{ return data.value != this.formModel.fromMapId; });
        },
        covertOtherData() {
            return this.mapList.filter(data=>{ return data.value != this.formModel.toMapId; });
        },
        covertMap(row) {
            return this.mapListMap[row.mapId];
        },
        handleDelete(index, row) {
            this.$confirm('此操作将删除iscs数据，是否继续?', this.$t('global.tips'), {
                confirmButtonText: this.$t('global.confirm'),
                cancelButtonText: this.$t('global.cancel'),
                type: 'warning'
            }).then(() => {
                deleteIscs(row.id).then(res=>{
                    this.$message.success(this.$t('publish.deleteSuccess'));
                    this.reloadTable();
                }).catch(() => {
                    this.reloadTable();
                    this.$messageBox(this.$t('error.deleteFailed'));
                });
            });
        },
        reloadTable() {
            this.queryList.reload();
        },
        handleCopy(index, row) {
            // this.formModel.fromId = row.id;
            this.$nextTick(()=>{
                this.dialogVisible = true;
            });

        },
        doSave() {
            this.$refs.dataform.validateForm(() => {
                this.loading = true;
                copyIscsData(this.formModel).then(res=>{
                    this.loading = false;
                    this.$message.success('复制ISCS数据成功');
                    this.close();
                    this.reloadTable();
                }).catch(() => {
                    this.loading = false;
                    this.reloadTable();
                    this.$messageBox('复制ISCS数据失败');
                });
            });
        },
        close() {
            this.$refs.dataform.resetForm();
            this.dialogVisible = false;
        }
    }
};
</script>
