/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2023-03-03 11:30:33
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2023-08-11 09:36:20
 * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/pubDicTypeMana/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */




import BMSPagecontainer from '@/components/BMSPageContainer';
import { BMSTable } from '@/components/BMSTable';

import { ActionType, EditableProTable, ProColumns } from '@ant-design/pro-components';
import {  ProFormDigit } from '@ant-design/pro-form'

import { Input, InputNumber, message } from 'antd';
import { typeOf } from 'mathjs';
import { useEffect, useRef, useState } from 'react';


import {editData, getJobCateTableData, JobCateTableDataType } from './service';

import './style.less';





export default function JobCateBaseSet() {

    const [currentEditRow, set_currentEditRow] = useState<undefined | any>(undefined);

    const [ifEdit,set_ifEdit] = useState(false);

    const tableRef = useRef<ActionType>();

    const columns: ProColumns[] = [

        {
            title: '职类',
            dataIndex: 'name',
            editable:false
        },
        {
            title: '合理编制比例',
            dataIndex: 'staffRate',
            valueType:'digit'
            //render: (_: any, record: any) => <EditableCell _={_} record={record} keyStr='staffRate' />
        },
        {
            title: '合理人均薪酬系数',
            dataIndex: 'averageSalary',
            valueType:'digit'
            //render: (_: any, record: any) => <EditableCell _={_} record={record} keyStr='averageSalary' />
            // render:(_:any)=>_ == 1?'指标':'自定义SQL'
        },
        {
            title: '奖金占比',
            dataIndex: 'percent',
            editable:false
        },
        {
            title: '岗位绩效占比',
            dataIndex: 'ratePercent',
            valueType:'digit'
            //render: (_: any, record: any) => <EditableCell _={_} record={record} keyStr='ratePercent' />
        },
        {
            title: '奖励性绩效占比',
            dataIndex: 'workPercent',
            valueType:'digit'
            //render: (_: any, record: any) => <EditableCell _={_} record={record} keyStr='workPercent' />
        },
        {
            title: '基金提拨',
            dataIndex: 'unitTypeFund',
            valueType:'digit'
            //render: (_: any, record: any) => <EditableCell _={_} record={record} keyStr='workPercent' />
        },
        {
            title: '岗位系数奖金',
            dataIndex: 'defaultPositionRate',
            valueType:'digit'
            //render: (_: any, record: any) => <EditableCell _={_} record={record} keyStr='workPercent' />
        },
        {
            title: '操作',
            key: 'option',
            width: 120,
            valueType: 'option',
            render: (text, record,_,action) => {
                return [
                    // <UpDataActBtn key={'act'} record={record} type='EDIT' />,
                    <div key={'edit'} >
                        {
                            !ifEdit && (
                                <a onClick={() => {
                                    set_currentEditRow(record);
                                    action?.startEditable?.(record.code);
                                }}>编辑</a>
                            )
                        }

                    </div>,

                ]
            },
        },

    ]


    const getTableData = async () => {
        const resp = await getJobCateTableData();
        if(resp){
            return {
                data:resp
            }
        }
        return {
            data:[]
        }
    }



    const updateTable = async (result:any) => {
        // console.log({result});
        const resp = await editData({
            code:result.code,
            name:result.name,
            staffRate:result.staffRate,
            averageSalary:result.averageSalary,
            ratePercent:result.ratePercent,
            workPercent:result.workPercent,
            unitTypeFund:result.unitTypeFund,
            defaultPositionRate:result.defaultPositionRate
        });
        if (resp) {
            message.success('操作成功！');
        }
        tableRef.current?.reload();

    }



    return (
        <BMSPagecontainer className='JobCateBaseSet' title={false}>
            <div className='tableTitle'>职类基础设定</div>
            <div style={{ marginTop: 16 }}>
                <EditableProTable 
                    className='BMS-EditableProTable'
                    columns={columns as ProColumns[]} actionRef={tableRef} 
                    rowKey='code'  pagination={false} request={()=> getTableData()}
                    recordCreatorProps={false}
                    editable={{
                        actionRender:(row, config, defaultDom) => [defaultDom.save, defaultDom.cancel],
                        onSave: async (rowKey, data, row) => {
                            set_ifEdit(false);
                            updateTable(data);   
                        },
                    }}
                    />
            </div>
        </BMSPagecontainer>
    )
}
