/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2023-03-03 11:30:33
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2024-04-25 16:52:23
 * @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 { getComputeDate } from '@/pages/Home/service';
import { getDataByKeyFromDic, getPubDicData } from '@/services/getDic';
import { createFromIconfontCN } from '@ant-design/icons';

import { ActionType, ProFormDependency, ProFormRadio } from '@ant-design/pro-components';
import { ModalForm, ProFormCascader, ProFormDigit, ProFormSelect, ProFormText, ProFormTextArea } from '@ant-design/pro-form'
import { ProColumns } from '@ant-design/pro-table';
import { Form, Input, InputNumber, message, Popconfirm } from 'antd';
import { useEffect, useRef, useState } from 'react'
import { getSecondaryProjectDistriData } from '../secondaryProjectDistribute/service';


import { addData, delData, editData, getProjectTableData } from './service';

import './style.less';

import '../../../../utils/zhongtaiB';
import '../../../../utils/zhongtaiA'

const IconFont = createFromIconfontCN({
    scriptUrl: '',
});

const AddIconFont = createFromIconfontCN({
    scriptUrl: '',
});



export default function RetentionAssessmentProjectMana() {

    const [tableDataFilterParams, set_tableDataFilterParams] = useState<any | undefined>();
    const [tableDataSearchKeywords, set_tableDataSearchKeywords] = useState<string>('');
    const tableRef = useRef<ActionType>();




    const columns: ProColumns[] = [

        {
            title: '项目代码',
            dataIndex: 'code',
        },
        {
            title: '项目名称',
            dataIndex: 'name',
        },
        {
            title: '项目类型',
            dataIndex: 'typeName',
        },
        {
            title: '说明',
            dataIndex: 'comment',
        },
        {
            title: '操作',
            key: 'option',
            width: 200,
            valueType: 'option',
            render: (_: any, record: any) => {
                return [
                    <UpDataActBtn key={'act'} record={record} type='EDIT' />,
                    <Popconfirm
                        title="是否确认删除？"
                        key="del"
                        onConfirm={() => delTableData(record)}
                    >
                        <a>删除</a>
                    </Popconfirm>
                ]
            },
        },

    ]


    const getTableData = async (params: any) => {
        const resp = await getProjectTableData(params);
        if (resp) {
            return {
                data: resp.list,
                success: true,
                total: resp.totalCount,
                pageSize: resp.pageSize,
                totalPage: resp.totalPage,
            }
        }
        return []
    }

    const delTableData = async (record: any) => {
        const resp = await delData(record.id);
        if (resp) {
            message.success('操作成功！');
            tableRef.current?.reload();
            // message.success('操作成功!');
        }
    }

    const updateTable = async (formVal: any, type: 'EDIT' | "ADD") => {

        const result = {
            name: formVal.name,
            type: formVal.type,
            comment: formVal.comment,
        }

        let flag = true;

        if (type == 'ADD') {
            const resp = await addData({ ...result });
            if (resp) {
                tableRef.current?.reload();
                message.success('操作成功！');
            }
        }
        if (type == 'EDIT') {
            const resp = await editData({ id: formVal.id, ...result });
            if (resp) {
                tableRef.current?.reload();
                message.success('操作成功！');
            }
        }

        return flag;
    }

    const UpDataActBtn = ({ record, type }: { record: any, type: 'EDIT' | 'ADD'}) => {

        return (
            <ModalForm
                title={`${type == 'EDIT' ? '编辑' : '新增'}`}
                width={400}
                initialValues={type == 'EDIT' ? { ...record } : {type:1}}
                trigger={
                    type == 'EDIT' ? <a key="edit" >编辑</a> : <span className='add'>新增</span>
                }
                onFinish={(val) => {
                    return updateTable(type == 'EDIT' ? { ...record, ...val } : { ...val }, type);
                }}
                modalProps={{ destroyOnClose: true }}
            >
                <ProFormText
                    name="name"
                    label="项目名称："
                    placeholder="请输入"
                    rules={[{ required: true, message: '名称不能为空!' }]}
                />
                <ProFormRadio.Group
                    name="type"
                    label="项目类型"
                    options={[
                        {
                            label: '系数',
                            value: 1,
                        },
                        {
                            label: '数值',
                            value: 2,
                        }
                    ]}
                    rules={[{ required: true, message: '项目类型不能为空!' }]}
                />
                <ProFormTextArea
                    name="comment"
                    label="说明："
                    placeholder="请输入"
                />
            </ModalForm>
        )
    }

    const tableDataSearchHandle = (paramName: string) => {

        set_tableDataFilterParams({
            ...tableDataFilterParams,
            [`${paramName}`]: tableDataSearchKeywords
        })
    }

    return (
        <BMSPagecontainer className='RetentionAssessmentProjectMana' title={false}>
            <div className='toolBar'>
                <div className='filter'>
                    <div className='filterItem'>
                        <span className='label' style={{ whiteSpace: 'nowrap' }}> 检索：</span>
                        <Input placeholder={'请输入项目名称'} allowClear
                            suffix={
                                <IconFont type="iconsousuo" onClick={() => tableDataSearchHandle('filter')} />
                            }
                            onChange={(e) => {
                                set_tableDataSearchKeywords(e.target.value);
                                if (e.target.value.length == 0) {
                                    set_tableDataFilterParams({
                                        ...tableDataFilterParams,
                                        filter: ''
                                    });
                                }
                            }}
                            onPressEnter={(e) => {

                                set_tableDataFilterParams({
                                    ...tableDataFilterParams,
                                    filter: (e.target as HTMLInputElement).value
                                });
                            }}

                        />
                    </div>
                </div>
                <div className='btnGroup'>
                    {/* <span className='getDataBtn' onClick={() => getDataBySQL()}>获取</span> */}
                    <UpDataActBtn record type='ADD' />
                </div>
            </div>
            <div style={{ marginTop: 16 }}>
                <BMSTable columns={columns as ProColumns[]} actionRef={tableRef} rowKey='id' params={tableDataFilterParams} request={(params) => getTableData(params)} />
            </div>
        </BMSPagecontainer>
    )
}
