<template>
<div class='grid' style="height:100%;">
        <el-table v-if="isDisplay === true"
        :default-sort="{ prop: minorSortPSDEF, order: Object.is(minorSortDir, 'ASC') ? 'ascending' : Object.is(minorSortDir, 'DESC') ? 'descending' : '' }"  
        @sort-change="onSortChange($event)"  
        :border="isDragendCol"
        stripe :height="isEnablePagingBar && items.length > 0 ? 'calc(100% - 50px)' : '100%'"  
        @row-click="rowClick($event)"  
        @select-all="selectAll($event)"  
        @select="select($event)"  
        @row-class-name="onRowClassName($event)"  
        @row-dblclick="rowDBLClick($event)"  
        ref='multipleTable' :data="items" :show-header="!isHideHeader">
            <template v-if="!isSingleSelect">
                <el-table-column type='selection' :width="checkboxColWidth"></el-table-column>
            </template>
            <template v-if="getColumnState('xh')">
                <el-table-column show-overflow-tooltip :prop="'xh'" :label="$t('syxtbyd.main_grid.columns.xh')" :width="100" :align="'left'" :sortable="'custom'">
                    <template v-slot="{row,column}">
                        <span>{{row.xh}}</span>
                    </template>
                </el-table-column>
            </template>
            <template v-if="getColumnState('tbydkname')">
                <el-table-column show-overflow-tooltip :prop="'tbydkname'" :label="$t('syxtbyd.main_grid.columns.tbydkname')" :width="200" :align="'left'" :sortable="'custom'">
                    <template v-slot="{row,column}">
                        <template v-if="isOpenEdit">
                            <i-form style="height:100%;" :model="row">
                                <app-form-item :name="column.property" :itemRules="rules[column.property]">
                                    <app-picker  
              :data="row"
              :context="context"
              :viewparams="viewparams"
              :itemParam='{ }' 
              :disabled="row.srfuf === 1 ? (3 & 2) !== 2 : (3 & 1) !== 1" 
              name='tbydkname' 
              deMajorField='tbydkname'
              deKeyField='tbydk'
              :service="service"
              :acParams="{ serviceName: 'TBYDKService', interfaceName: 'FetchSYX'}"
              valueitem='tbydkid' 
              :value="row[column.property]" 
              editortype="dropdown" 
              style="" 
              @formitemvaluechange="($event)=>{onGridItemValueChange(row,$event)}">
            </app-picker>
                                </app-form-item>
                            </i-form>
                        </template>
                        <template v-if="!isOpenEdit">
                                <span>{{row.tbydkname}}</span>
                        </template>
                    </template>
                </el-table-column>
            </template>
            <template v-if="getColumnState('tymc')">
                <el-table-column show-overflow-tooltip :prop="'tymc'" :label="$t('syxtbyd.main_grid.columns.tymc')" :width="300" :align="'left'" :sortable="'custom'">
                    <template v-slot="{row,column}">
                        <template v-if="isOpenEdit">
                            <i-form style="height:100%;" :model="row">
                                <app-form-item :name="column.property" :itemRules="rules[column.property]">
                                    <input-box :disabled="row.srfuf === 1 ? (3 & 2) !== 2 : (3 & 1) !== 1" v-model="row[column.property]" style="" @change="($event)=>{gridEditItemChange(row, column.property, $event)}"></input-box>
                                </app-form-item>
                            </i-form>
                        </template>
                        <template v-if="!isOpenEdit">
                                <span>{{row.tymc}}</span>
                        </template>
                    </template>
                </el-table-column>
            </template>
            <template v-if="getColumnState('tynr')">
                <el-table-column show-overflow-tooltip :prop="'tynr'" :label="$t('syxtbyd.main_grid.columns.tynr')" :align="'left'" :sortable="'custom'">
                    <template v-slot="{row,column}">
                        <template v-if="isOpenEdit">
                            <i-form style="height:100%;" :model="row">
                                <app-form-item :name="column.property" :itemRules="rules[column.property]">
                                    <input-box :disabled="row.srfuf === 1 ? (3 & 2) !== 2 : (3 & 1) !== 1" v-model="row[column.property]" style="" @change="($event)=>{gridEditItemChange(row, column.property, $event)}"></input-box>
                                </app-form-item>
                            </i-form>
                        </template>
                        <template v-if="!isOpenEdit">
                                <span>{{row.tynr}}</span>
                        </template>
                    </template>
                </el-table-column>
            </template>
            <template v-if="getColumnState('cidid')">
                <el-table-column show-overflow-tooltip :prop="'cidid'" :label="$t('syxtbyd.main_grid.columns.cidid')" :width="100" :align="'left'" :sortable="'custom'">
                    <template v-slot="{row,column}">
                        <template v-if="isOpenEdit">
                            <i-form style="height:100%;" :model="row">
                                <app-form-item :name="column.property" :itemRules="rules[column.property]">
                                    <input-box :disabled="row.srfuf === 1 ? (3 & 2) !== 2 : (3 & 1) !== 1" v-model="row[column.property]" style="" @change="($event)=>{gridEditItemChange(row, column.property, $event)}"></input-box>
                                </app-form-item>
                            </i-form>
                        </template>
                        <template v-if="!isOpenEdit">
                                <span>{{row.cidid}}</span>
                        </template>
                    </template>
                </el-table-column>
            </template>
            <template v-if="getColumnState('uagridcolumn1')">
                <el-table-column :column-key="'uagridcolumn1'" :label="$t('syxtbyd.main_grid.columns.uagridcolumn1')" :width="100" :align="'right'">
                    <template slot-scope="scope">
                        <span>
                            
                            <a @click="uiAction(scope.row, 'Detele', $event)">{{$t('syxtbyd.main_grid.uiactions.detele')}}</a>
                        </span>
                    </template>
                </el-table-column>
            </template>
            <template v-if="getColumnState('tbydkid')">
                <el-table-column show-overflow-tooltip :prop="'tbydkid'" :label="$t('syxtbyd.main_grid.columns.tbydkid')" :width="100" :align="'left'" :sortable="'custom'">
                    <template v-slot="{row,column}">
                        <template v-if="isOpenEdit">
                            <i-form style="height:100%;" :model="row">
                                <app-form-item :name="column.property" :itemRules="rules[column.property]">
                                    <input-box :disabled="row.srfuf === 1 ? (3 & 2) !== 2 : (3 & 1) !== 1" v-model="row[column.property]" style="" @change="($event)=>{gridEditItemChange(row, column.property, $event)}"></input-box>
                                </app-form-item>
                            </i-form>
                        </template>
                        <template v-if="!isOpenEdit">
                                <span>{{row.tbydkid}}</span>
                        </template>
                    </template>
                </el-table-column>
            </template>
            <template v-if="adaptiveState">
                <el-table-column></el-table-column>
            </template>
    </el-table>
</div>
</template>
<script lang='ts'>
import { Vue, Component, Prop, Provide, Emit, Watch, Model } from 'vue-property-decorator';
import { CreateElement } from 'vue';
import { Subject, Subscription } from 'rxjs';
import { ControlInterface } from '@/interface/control';
import { UICounter,UIActionTool } from '@/utils';
import SYXTBYDService from '@/service/syxtbyd/syxtbyd-service';
import MainService from './main-grid-service';

import SYXTBYDUIService from '@/uiservice/syxtbyd/syxtbyd-ui-service';


@Component({
    components: {
    }
})
export default class MainBase extends Vue implements ControlInterface {

    /**
     * 名称
     *
     * @type {string}
     * @memberof Main
     */
    @Prop() protected name?: string;

    /**
     * 视图通讯对象
     *
     * @type {Subject<ViewState>}
     * @memberof Main
     */
    @Prop() protected viewState!: Subject<ViewState>;

    /**
     * 应用上下文
     *
     * @type {*}
     * @memberof Main
     */
    @Prop() protected context: any;

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof Main
     */
    @Prop() protected viewparams: any;

    /**
     * 视图状态事件
     *
     * @protected
     * @type {(Subscription | undefined)}
     * @memberof Main
     */
    protected viewStateEvent: Subscription | undefined;

    /**
     * 获取部件类型
     *
     * @returns {string}
     * @memberof Main
     */
    protected getControlType(): string {
        return 'GRID'
    }

    /**
     * 建构部件服务对象
     *
     * @type {MainService}
     * @memberof Main
     */
    protected service: MainService = new MainService({ $store: this.$store });

    /**
     * 实体服务对象
     *
     * @type {SYXTBYDService}
     * @memberof Main
     */
    protected appEntityService: SYXTBYDService = new SYXTBYDService({ $store: this.$store });
    

    /**
     * 逻辑事件
     *
     * @param {*} [params={}]
     * @param {*} [tag]
     * @param {*} [$event]
     * @memberof 
     */
    protected grid_uagridcolumn1_u89889ad_click(params: any = {}, tag?: any, $event?: any) {

        // 取数
        let datas: any[] = [];
        let xData: any = null;
        // _this 指向容器对象
        const _this: any = this;
        let paramJO:any = {};
        
        let contextJO:any = {};
        xData = this;
        if (_this.getDatas && _this.getDatas instanceof Function) {
            datas = [..._this.getDatas()];
        }
        // 界面行为
        const curUIService:SYXTBYDUIService  = new SYXTBYDUIService();
         curUIService.SYXTBYD_Detele(datas,contextJO, paramJO,  $event, xData,this);
    }


    /**
     * 关闭视图
     *
     * @param {any[]} args
     * @memberof Main
     */
    protected closeView(args: any[]): void {
        let _this: any = this;
        _this.$emit('closeview', args);
    }


    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof Main
     */
    public getDatas(): any[] {
        return this.selections;
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof Main
     */
    public getData(): any {
        return this.selections[0];
    }

    /**
     * 显示处理提示
     *
     * @type {boolean}
     * @memberof Main
     */
    @Prop({ default: true }) protected showBusyIndicator?: boolean;

    /**
     * 部件行为--update
     *
     * @type {string}
     * @memberof Main
     */
    @Prop() protected updateAction!: string;
    
    /**
     * 部件行为--fetch
     *
     * @type {string}
     * @memberof Main
     */
    @Prop() protected fetchAction!: string;
    
    /**
     * 部件行为--remove
     *
     * @type {string}
     * @memberof Main
     */
    @Prop() protected removeAction!: string;
    
    /**
     * 部件行为--load
     *
     * @type {string}
     * @memberof Main
     */
    @Prop() protected loadAction!: string;
    
    /**
     * 部件行为--loaddraft
     *
     * @type {string}
     * @memberof Main
     */
    @Prop() protected loaddraftAction!: string;
    
    /**
     * 部件行为--create
     *
     * @type {string}
     * @memberof Main
     */
    @Prop() protected createAction!: string;

    /**
     * 当前页
     *
     * @type {number}
     * @memberof Main
     */
    protected curPage: number = 1;

    /**
     * 数据
     *
     * @type {any[]}
     * @memberof Main
     */
    protected items: any[] = [];

    /**
     * 是否支持分页
     *
     * @type {boolean}
     * @memberof Main
     */
    protected isEnablePagingBar: boolean = false;

    /**
     * 是否禁用排序
     *
     * @type {boolean}
     * @memberof Main
     */
    protected isNoSort: boolean = false;

    /**
     * 排序方向
     *
     * @type {string}
     * @memberof Main
     */
    protected minorSortDir: string = '';

    /**
     * 排序字段
     *
     * @type {string}
     * @memberof Main
     */
    protected minorSortPSDEF: string = '';

    /**
     * 分页条数
     *
     * @type {number}
     * @memberof Main
     */
    protected limit: number = 20;

    /**
     * 是否显示标题
     *
     * @type {boolean}
     * @memberof Main
     */
    protected isHideHeader: boolean = false;

    /**
     * 是否单选
     *
     * @type {boolean}
     * @memberof Main
     */
    @Prop() protected isSingleSelect?: boolean;

    /**
     * 是否开启行编辑
     *
     * @type {boolean}
     * @memberof Main
     */
    @Prop({default: false}) protected isOpenEdit!: boolean;

    /**
     * 总条数
     *
     * @type {number}
     * @memberof Main
     */
    protected totalrow: number = 0;

    /**
     * 选中行数据
     *
     * @type {any[]}
     * @memberof Main
     */
    protected selections: any[] = [];

    /**
     * 拦截行选中
     *
     * @type {boolean}
     * @memberof Main
     */
    protected stopRowClick: boolean = false;


    /**
     * 表格是否显示
     *
     * @type {boolean}
     * @memberof Main
     */
    protected isDisplay:boolean = true;

    /**
     * 部件刷新
     *
     * @param {any[]} args
     * @memberof Main
     */
    protected refresh(args: any[]): void {
        this.load();
    }

	/**
	 * 选项框列宽
	 *
	 * @type {number}
	 * @memberof AppIndex
	 */
	protected checkboxColWidth: number = 80;

    /**
     * 是否允许拖动列宽
     *
     * @type {boolean}
     * @memberof AppEmbedPicker
     */
    protected isDragendCol: boolean = false;

    /**
     * 所有列成员
     *
     * @type {any[]}
     * @memberof Main
     */
    protected allColumns: any[] = [
        {
            name: 'xh',
            label: '商业险',
            langtag: 'syxtbyd.main_grid.columns.xh',
            show: true,
            util: 'PX'
        },
        {
            name: 'tbydkname',
            label: '特约代码',
            langtag: 'syxtbyd.main_grid.columns.tbydkname',
            show: true,
            util: 'PX'
        },
        {
            name: 'tymc',
            label: '特约名称',
            langtag: 'syxtbyd.main_grid.columns.tymc',
            show: true,
            util: 'PX'
        },
        {
            name: 'tynr',
            label: '特约内容',
            langtag: 'syxtbyd.main_grid.columns.tynr',
            show: true,
            util: 'STAR'
        },
        {
            name: 'cidid',
            label: '车险单标识',
            langtag: 'syxtbyd.main_grid.columns.cidid',
            show: false,
            util: 'PX'
        },
        {
            name: 'uagridcolumn1',
            label: '操作',
            langtag: 'syxtbyd.main_grid.columns.uagridcolumn1',
            show: true,
            util: 'PX'
        },
        {
            name: 'tbydkid',
            label: '特别约定库标识',
            langtag: 'syxtbyd.main_grid.columns.tbydkid',
            show: false,
            util: 'PX'
        },
    ]

    /**
     * 属性值规则
     *
     * @type {*}
     * @memberof Main
     */
    protected rules: any = {
        tymc: [
             { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特约名称 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特约名称 值不能为空', trigger: 'blur' },
        ],
        tbydkid: [
             { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特别约定库标识 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特别约定库标识 值不能为空', trigger: 'blur' },
        ],
        tbydkname: [
             { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特约代码 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特约代码 值不能为空', trigger: 'blur' },
        ],
        cidid: [
             { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '车险单标识 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '车险单标识 值不能为空', trigger: 'blur' },
        ],
        tynr: [
             { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特约内容 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '特约内容 值不能为空', trigger: 'blur' },
        ],
        srfkey: [
             { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '商业险特别约定标识 值不能为空', trigger: 'change' },
            { required: false, validator: (rule:any, value:any, callback:any) => { return (rule.required && (value === null || value === undefined || value === "")) ? false : true;}, message: '商业险特别约定标识 值不能为空', trigger: 'blur' },
        ],
    }

    /**
     * 表格数据加载
     *
     * @param {*} [arg={}]
     * @memberof Main
     */
    protected load(opt: any = {}): void {
        if(!this.fetchAction){
            this.$Notice.error({ title: '错误', desc: 'SYXTBYDGridView9视图表格fetchAction参数未配置' });
            return;
        }
        const arg: any = {...opt};

        const page: any = {};
        if (this.isEnablePagingBar) {
            Object.assign(page, { page: this.curPage-1, size: this.limit });
        }
        // 设置排序
        if (!this.isNoSort && !Object.is(this.minorSortDir, '') && !Object.is(this.minorSortPSDEF, '')) {
            const sort: string = this.minorSortPSDEF+","+this.minorSortDir;
            Object.assign(page, { sort: sort });
        }
        Object.assign(arg, page);

        const parentdata: any = {};
        this.$emit('beforeload', parentdata);
        Object.assign(arg, parentdata);
        if(this.context.srfparentdename && !this.context[this.context.srfparentdename.toLowerCase()]){
            return;
        }
        const post: Promise<any> = this.service.search(this.fetchAction,this.context, arg, this.showBusyIndicator);
        post.then((response: any) => {
            if (!response.status || response.status !== 200) {
                if (response.errorMessage) {
                    this.$Notice.error({ title: '错误', desc: response.errorMessage });
                }
                return;
            }
            const data: any = response.data;
            if (Object.keys(data).length > 0) {
                this.items = JSON.parse(JSON.stringify(data.records));
                this.totalrow = data.total;
            }
            // 清空selections
            this.selections = [];
            this.$emit('load', this.items);
        }).catch((response: any) => {
            if (response && response.status === 401) {
                return;
            }
            this.$Notice.error({ title: '错误', desc: response.errorMessage });
        });
    }

    /**
     * 删除
     *
     * @param {any[]} datas
     * @returns {Promise<any>}
     * @memberof Main
     */
    protected async remove(datas: any[]): Promise<any> {
        if(!this.removeAction){
            this.$Notice.error({ title: '错误', desc: 'SYXTBYDGridView9视图表格removeAction参数未配置' });
            return;
        }
        let _datas:any[] = [];
        datas.forEach((record: any, index: number) => {
            if (!record.srfkey) {
                this.items.some((val: any, num: number) =>{
                    if(JSON.stringify(val) == JSON.stringify(record)){
                        this.items.splice(num,1);
                        return true;
                    }
                }); 
            }else{
               _datas.push(datas[index]);
            }
        });
        if (_datas.length === 0) {
            return;
        }
        let dataInfo = '';
        _datas.forEach((record: any, index: number) => {
            let srfmajortext = record.srfmajortext;
            if (index < 5) {
                if (!Object.is(dataInfo, '')) {
                    dataInfo += '、';
                }
                dataInfo += srfmajortext;
            } else {
                return false;
            }
        });

        if (_datas.length < 5) {
            dataInfo = dataInfo + ' 共' + _datas.length + '条数据';
        } else {
            dataInfo = dataInfo + '...' + ' 共' + _datas.length + '条数据';
        }

        const removeData = () => {
            let keys: any[] = [];
            _datas.forEach((data: any) => {
                keys.push(data.srfkey);
            });
            const context:any = JSON.parse(JSON.stringify(this.context));
            const post: Promise<any> = this.service.delete(this.removeAction,Object.assign(context,{ syxtbyd: keys.join(';') }),{ syxtbydid: keys.join(';') }, this.showBusyIndicator);
            return new Promise((resolve: any, reject: any) => {
                post.then((response: any) => {
                    if (!response || response.status !== 200) {
                        this.$Notice.error({ title: '', desc: '删除数据失败,' + response.info });
                        return;
                    } else {
                        this.$Notice.success({ title: '', desc: '删除成功!' });
                    }
                    this.load({});
                    this.$emit('remove', null);
                    this.selections = [];
                    resolve(response);
                }).catch((response: any) => {
                    if (response && response.status === 401) {
                        return;
                    }
                    if (!response || !response.status || !response.data) {
                        this.$Notice.error({ title: '错误', desc: '系统异常' });
                        reject(response);
                        return;
                    }

                    const { data: _data } = response;
                    this.$Notice.error({ title: _data.title, desc: _data.message });
                    reject(response);
                });
            });
        }

        dataInfo = dataInfo.replace(/[null]/g, '').replace(/[undefined]/g, '');
        this.$Modal.confirm({
            title: '警告',
            content: '确认要删除 ' + dataInfo + '，删除操作将不可恢复？',
            onOk: () => {
                removeData();
            },
            onCancel: () => { }
        });
        return removeData;
    }


    /**
     * 批量添加
     *
     * @param {*} [arg={}]
     * @memberof Main
     */
    protected addBatch(arg: any = {}): void {
        if(!this.fetchAction){
            this.$Notice.error({ title: '错误', desc: 'SYXTBYDGridView9视图表格fetchAction参数未配置' });
            return;
        }
        if(!arg){
            arg = {};
        }
        console.error("批量添加未实现");
    }

    /**
     * 数据导出
     *
     * @param {*} data
     * @memberof Main
     */
    protected exportExcel(data: any = {}): void {
        const arg: any = {};

        const page: any = {};
        if (!this.isNoSort && !Object.is(this.minorSortDir, '') && !Object.is(this.minorSortPSDEF, '')) {
            const sort: string[] = [this.minorSortPSDEF];
            if (Object.is(this.minorSortDir, 'ASC')) {
                Object.assign(page, { asc: sort });
            } else {
                Object.assign(page, { desc: sort });
            }
        }

        if (Object.is(data.type, 'maxRowCount')) {
            Object.assign(page, { page: 0, size: data.maxRowCount });
        } else if (Object.is(data.type, 'activatedPage')) {
            Object.assign(page, { page: this.curPage-1, size: this.limit });
        }
        Object.assign(arg, { page: page });

        const parentdata: any = {};
        this.$emit('beforeload', parentdata);
        Object.assign(arg, parentdata);
        console.error("数据导出未实现");
    }

    /**
     * 生命周期
     *
     * @memberof Main
     */
    protected created(): void {
        this.afterCreated();
    }

    /**
     * 执行created后的逻辑
     *
     *  @memberof Main
     */    
    protected afterCreated(){
        this.setColState();
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                if (Object.is('load', action)) {
                    this.load(data);
                }
                if (Object.is('remove', action)) {
                    this.remove(data);
                }
                if (Object.is('save', action)) {
                    this.save(data);
                }
            });
        }
    }

    /**
     * vue 生命周期
     *
     * @memberof Main
     */
    protected destroyed() {
        this.afterDestroy();
    }

    /**
     * 执行destroyed后的逻辑
     *
     * @memberof Main
     */
    protected afterDestroy() {
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
    }

    /**
     * 获取选中行胡数据
     *
     * @returns {any[]}
     * @memberof Main
     */
    protected getSelection(): any[] {
        return this.selections;
    }

    /**
     * 行双击事件
     *
     * @param {*} $event
     * @returns {void}
     * @memberof Main
     */
    protected rowDBLClick($event: any): void {
        if (!$event || this.isOpenEdit) {
            return;
        }
        this.selections = [];
        this.selections.push(JSON.parse(JSON.stringify($event)));

        const refs: any = this.$refs;
        if (refs.multipleTable) {
            refs.multipleTable.clearSelection();
            refs.multipleTable.toggleRowSelection($event);
        }

        this.$emit('rowdblclick', this.selections);
        this.$emit('selectionchange', this.selections);
    }

    /**
     * 复选框数据选中
     *
     * @param {*} $event
     * @returns {void}
     * @memberof  Main
     */
    protected select($event: any): void {
        if (!$event) {
            return;
        }
        this.selections = [];
        this.selections = [...JSON.parse(JSON.stringify($event))];
        this.$emit('selectionchange', this.selections);
    }

    /**
     * 复选框数据全部选中
     *
     * @param {*} $event
     * @memberof  Main
     */
    protected selectAll($event: any): void {
        if (!$event) {
            return;
        }
        this.selections = [];
        this.selections = [...JSON.parse(JSON.stringify($event))];
        this.$emit('selectionchange', this.selections);
    }

    
    /**
     * 行单击选中
     *
     * @param {*} $event
     * @returns {void}
     * @memberof Main
     */
    protected rowClick($event: any, ifAlways: boolean = false): void {
        if (!ifAlways && (!$event || this.isOpenEdit)) {
            return;
        }
        if(this.stopRowClick) {
            this.stopRowClick = false;
            return;
        }
        if(this.isSingleSelect){
            this.selections = [];
        }
        this.selections.push(JSON.parse(JSON.stringify($event)));

        const refs: any = this.$refs;
        if (refs.multipleTable) {
            if(this.isSingleSelect){
                refs.multipleTable.clearSelection();
            }
            refs.multipleTable.toggleRowSelection($event);
        }

        this.$emit('selectionchange', this.selections);
    }


    /**
     * 页面变化
     *
     * @param {*} $event
     * @returns {void}
     * @memberof Main
     */
    protected pageOnChange($event: any): void {
        if (!$event) {
            return;
        }
        if ($event === this.curPage) {
            return;
        }
        this.curPage = $event;
        this.load({});
    }

    /**
     * 分页条数变化
     *
     * @param {*} $event
     * @returns {void}
     * @memberof Main
     */
    protected onPageSizeChange($event: any): void {
        if (!$event) {
            return;
        }
        if ($event === this.limit) {
            return;
        }
        this.limit = $event;
        if (this.curPage === 1) {
            this.load({});
        }
    }

    /**
     * 分页刷新
     *
     * @memberof Main
     */
    protected pageRefresh(): void {
        this.load({});
    }

    /**
     * 排序变化
     *
     * @param {{ column: any, prop: any, order: any }} { column, prop, order }
     * @memberof Main
     */
    protected onSortChange({ column, prop, order }: { column: any, prop: any, order: any }): void {
        const dir = Object.is(order, 'ascending') ? 'asc' : Object.is(order, 'descending') ? 'desc' : '';
        if (Object.is(dir, this.minorSortDir) && Object.is(this.minorSortPSDEF, prop)) {
            return;
        }
        this.minorSortDir = dir;
        this.minorSortPSDEF = prop ? prop : '';
        this.load({});
    }

    /**
     * 表格行选中样式
     *
     * @param {{ row: any, rowIndex: any }} { row, rowIndex }
     * @returns {string}
     * @memberof Main
     */
    protected onRowClassName({ row, rowIndex }: { row: any, rowIndex: any }): string {
        const index = this.selections.findIndex((select: any) => Object.is(select.srfkey, row.srfkey));
        return index !== -1 ? 'grid-row-select' : '';
    }



    /**
     * 界面行为
     *
     * @param {*} row
     * @param {*} tag
     * @param {*} $event
     * @memberof Main
     */
	protected uiAction(row: any, tag: any, $event: any) {
        this.rowClick(row, true);
        if(Object.is('Detele', tag)) {
            this.grid_uagridcolumn1_u89889ad_click(row, tag, $event);
        }
    }

    /**
     * 设置列状态
     *
     * @memberof Main
     */
    protected setColState() {
		const _data: any = localStorage.getItem('syxtbyd_main_grid');
		if (_data) {
			let columns = JSON.parse(_data);
			columns.forEach((col: any) => {
				let column = this.allColumns.find((item) => Object.is(col.name, item.name));
				if (column) {
					Object.assign(column, col);
				}
			});
		}
    }

    /**
     * 列变化
     *
     * @memberof Main
     */
    protected onColChange() {
        localStorage.setItem('syxtbyd_main_grid', JSON.stringify(this.allColumns));
    }

    /**
     * 获取列状态
     *
     * @param {string} name
     * @returns {boolean}
     * @memberof Main
     */
    protected getColumnState(name: string): boolean {
        let column = this.allColumns.find((col: any) =>
            Object.is(name, col.name)
        );
        return column.show ? true : false;
    }

    /**
     * 表格列是否自适应布局
     *
     * @readonly
     * @type {boolean}
     * @memberof Main
     */
    get adaptiveState(): boolean {
        return !this.allColumns.find((column: any) => column.show && Object.is(column.util, 'STAR'));
    }

    /**
     * 保存
     *
     * @param {*} $event
     * @returns {void}
     * @memberof Main
     */
    protected save(args: any[], params?: any, $event?: any, xData?: any): void {
        let _this = this;
        let promises:any = [];
        _this.items.forEach((item:any)=>{
            if(Object.is(item.srfuf, '0')){
                if(!this.createAction){
                    this.$Notice.error({ title: '错误', desc: 'SYXTBYDGridView9视图表格createAction参数未配置' });
                    return;
                }
                Object.assign(item,this.viewparams);
                promises.push(this.service.add(this.createAction, this.context,item, this.showBusyIndicator));
            }else{
                if(!this.updateAction){
                    this.$Notice.error({ title: '错误', desc: 'SYXTBYDGridView9视图表格updateAction参数未配置' });
                    return;
                }
                Object.assign(item,this.viewparams);
                if(item.syxtbyd){
                    Object.assign(this.context,{syxtbyd:item.syxtbyd})
                }
                promises.push(this.service.add(this.updateAction,this.context,item, this.showBusyIndicator));
            }
        });
        Promise.all(promises).then((response: any) => {
            this.$emit('save', response);
        }).catch((response: any) => {
            this.$Notice.error({ title: '错误', desc: '系统异常' });
        });
    }

    /**
     * 新建行
     *
     * @param {*} $event
     * @returns {void}
     * @memberof Main
     */
    protected newRow(args: any[], params?: any, $event?: any, xData?: any): void {
        if(!this.loaddraftAction){
            this.$Notice.error({ title: '错误', desc: 'SYXTBYDGridView9视图表格loaddraftAction参数未配置' });
            return;
        }
        let _this = this;
        Object.assign(args[0],this.viewparams);
        let post: Promise<any> = this.service.loadDraft(this.loaddraftAction, this.context, args[0], this.showBusyIndicator);
        post.then((response: any) => {
            if (!response.status || response.status !== 200) {
                if (response.errorMessage) {
                    this.$Notice.error({ title: '错误', desc: response.errorMessage });
                }
                return;
            }
            const data = response.data.records;
            _this.items.push(data);
        }).catch((response: any) => {
            if (response && response.status === 401) {
                return;
            }
            if (!response || !response.status || !response.data) {
                this.$Notice.error({ title: '错误', desc: '系统异常' });
                return;
            }
            const { data: _data } = response;
            this.$Notice.error({ title: _data.title, desc: _data.message });
        });
    }

    /**
     * 表格编辑项值变更
     *  
     * @param row 行数据
     * @param {{ name: string, value: any }} $event
     * @returns {void}
     * @memberof Main
     */
    protected onGridItemValueChange(row: any,$event: { name: string, value: any }): void {
        if (!$event) {
            return;
        }
        if (!$event.name || Object.is($event.name, '') || !row.hasOwnProperty($event.name)) {
            return;
        }
        row[$event.name] = $event.value;
        this.gridEditItemChange(row, $event.name, $event.value);
    }

    /**
     * 表格编辑项值变化
     *
     * @private
     * @param row 行数据
     * @param property 列编辑项名
     * @param row 列编辑项值
     * @returns {void}
     * @memberof Main
     */
    protected gridEditItemChange(row: any, property: string, value: any){
        if(Object.is(property, 'tbydkname')){
            const details: string[] = ['tymc', 'tynr'];
            this.updateGridEditItem('GetTBYDKXX', row, details, true);
        }
    }

    /**
     * 表格编辑项更新
     *
     * @param {string} mode 界面行为名称
     * @param {*} [data={}] 请求数据
     * @param {string[]} updateDetails 更新项
     * @param {boolean} [showloading] 是否显示加载状态
     * @returns {void}
     * @memberof Main
     */
    protected updateGridEditItem(mode: string, data: any = {}, updateDetails: string[], showloading?: boolean): void {
        if (!mode || (mode && Object.is(mode, ''))) {
            return;
        }
        const arg: any = JSON.parse(JSON.stringify(data));
        const post: Promise<any> = this.service.frontLogic(mode,this.context,arg, showloading);
        post.then((response: any) => {
            if (!response || response.status !== 200) {
                this.$Notice.error({ title: '错误', desc: '表单项更新失败' });
                return;
            }
            const _data: any = response.data.records;
            if(!_data){
                return;
            }
            updateDetails.forEach((name: string) => {
                if (!_data.hasOwnProperty(name)) {
                    return;
                }
                data[name] = _data[name];
            });
        }).catch((response: any) => {
            if (response && response.status === 401) {
                return;
            }
            if (!response || !response.status || !response.data) {
                this.$Notice.error({ title: '错误', desc: '系统异常' });
                return;
            }
        });
    }

}
</script>

<style lang='less'>
@import './main-grid.less';
</style>