<template>
    <div>
        <cs-dynamic-table ref="child" :prop="csTable" :parent="this">
            <template slot="form" scope="props">
                <index-form :model="props.obj" :action="props.action" :tableVm="props.tableVm" @close-modal="props.closeModal" :tableInfo="props.tableInfo" ref="form"></index-form>
            </template>
        </cs-dynamic-table>
        <Modal v-model="importFlag" title="指标导入" width="50" :mask-closable="false" @on-cancel="closeModal">
            <index-import @close-modal="closeModal" ref="deploy" @get-data="searchTable"></index-import>
            <div slot="footer"></div>
        </Modal>
    </div>
</template>
<script lang="ts">
    import { Component, Prop, Vue, Emit,Watch } from "vue-property-decorator";
    import IndexForm from "./index-form.vue";
     import IndexImport from "./index-import.vue";

    @Component({
        components: {
            IndexForm,IndexImport
        }
    })
    export default class Index extends Vue {
        importFlag : boolean =false;
        @Prop(String) src! : string;
        @Prop(String) name! : string;
        @Watch('name') 
        srcChange(val){
            this.searchTable()
        }
        private csTable:any = {
            formWidth: 35,
            parentVm: this,
            dataSrc: this.src,
            delSrc: this.$msPrefix.psem+"/api/v1/data/delete/PSEMIndicator/",
            batchDelUri: this.$msPrefix.psem+"/api/v1/data/batch/PSEMIndicator",
            dataProp: "data.content",
            postMethod: true,
            pageableProp: {
                pageSize: "data.size",
                total: "data.totalElements",
                currentPage: "data.number"
            },
            dataFun:this.getIndexData,
            showIndex: true,
            actions: { //表头操作按钮<Icon type="ios-rewind-outline" />
                replace: true, //替换现有菜单
                def: [{
                    name: "返回",
                    icon: "ios-rewind-outline",
                    type: 'info',
                    action: ()=> {
                        this.back();
                    }
                },{
                    name: "新增",
                    icon: "md-add",
                    type: 'primary',
                    action: ()=> {
                        this.add();
                    }
                },{
                    name: "导入",
                    icon: "ios-cloud-upload-outline",
                    type: 'primary',
                    action: ()=> {
                        this.import();
                    }
                }]
            },
            emptyModel:  {
                nameZh: "",
                nameEn: "",
                responseReflectKey: "",
                params: "",
                responseKey: "",
                script: "",
                priority: null,
                show: true,
                description: "",
                valueReflect: "",
                alwaysShow: true,
                typeName:this.name
            },
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center',
                },
                {
                    key: 'responseKey',
                    title: '指标标识',
                    align: 'center',
                },
                {
                    key: 'nameZh',
                    title: '指标中文名',
                    align: 'center',
                },
                {
                    key: 'nameEn',
                    title: '指标英文名',
                    align: 'center'
                },
                

                {
                    key: 'priority',
                    title: '序号',
                    align: 'center',
                },
                {
                    key: 'description',
                    title: '指标描述',
                    align: 'center',
                },
                {

                    title: '操作',
                    key: 'action',
                    minWidth:200,
                    align: 'center',
                    render: (h, params) => {
                        let menus = [{
                            icon: 'ios-eye',
                            action: 'detail',
                            label: '详情',
                            type:'info'
                        }, {
                            icon: 'ios-create-outline',
                            action: 'update',
                            label: '编辑',
                            type:'primary'
                        }, {
                            icon: 'md-trash',
                            action: 'delete',
                            label: '删除',
                            type:'error'

                        }];
                        let clickEvent = this.clickEvent;
                        return h('div', [
                            h('CsButton', {
                                props: {
                                    actions: menus,
                                    click: clickEvent,
                                    data: params.row,
                                    size:"small",
                                    styleCss:"marginRight:5px",
                                    disabled:params.row._disabled
                                },
                            })
                        ]);
                    }
                }]
        }
        @Emit("back") goBack(){}
        back(){
            this.goBack()
        }
        getIndexData(){

        }
        
        add(){
            (this.$refs.child as any).formInfo.title = "新增";
            (this.$refs.child as any).formInfo.action = "save";
            (this.$refs.child as any).formInfo.model = _.cloneDeep(this.csTable.emptyModel);
            (this.$refs.child as any).dialogVisible = true;
        }
        clickEvent(name:string, obj:any) {
            if(name == 'delete') {
                (this.$refs.child as any).handleDelete(obj.id)
            }else {
                (this.$refs.child as any).formInfo.title = name == 'update' ? "编辑" : "详情";
                (this.$refs.child as any).formInfo.action = name == 'update' ? 'update' : 'detail';
                (this.$refs.child as any).formInfo.model = _.cloneDeep(obj);
                (this.$refs.child as any).dialogVisible = true;
            }

        }
        searchTable(){
            this.csTable.dataSrc=this.src;
            this.csTable.dataFun=!1;
            this.csTable.postData={typeName:[""+this.name]};
            this.csTable.emptyModel.typeName=this.name;
            (this.$refs.child as any).getData();
        }
         import(){
            this.importFlag=!0;
        }
         closeModal(){
            this.importFlag=!1;
        }
        mounted(){
           this.csTable.emptyModel.typeName=this.name;
        }

    }
</script>

