/*
 * @Author: your name
 * @Date: 2021-07-30 18:09:25
 * @LastEditTime: 2021-08-01 14:07:20
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /TracerMethodology_PC/src/pages/costAllocationParamsConnect/updateForm_drawer.js
 */

import React, { useState, useEffect } from 'react';
import { Cascader, Form } from 'antd';
import ProForm, {
  ProFormSelect,
  ProFormText,
  ModalForm,
  ProFormDigit,
} from '@ant-design/pro-form';
import { useIntl, FormattedMessage } from 'umi';

import {
    getCostshareparamConnectList, editCostshareparamConnectList, delCostshareparamConnect,
    addCostshareparamConnect, getResponsibilityCenterSelecterList, getAccountingSubjectForSelecter,
    getHasSetParamsList, getParamsSelectableList,saveParamsList
  } from './service';

const UpdateFormDrawer = (props) => {
  const {
    values: record,
    drawerUpdateModalVisible,
    drawerUpdateModalVisibleChange,
    currentResponsibilityRow,
    onSubmit,
  } = props;
  const { accountingId, responsibilityId } = record;
  const [ifDisable, setIfDisable] = useState(false);
  const [selectedParam, setSelectedParam] = useState(null);
//   console.log({record})

//   useEffect(async () => {
//     const resp = await getResponsibilityCenterSelecterList();
//     const { data, msg, status } = resp;
//     if (status == 200) {
//       const selected = data.filter((item) => {
//         return item.responsibilityId == responsibilityId;
//       });
//     //   console.log({ selected, data });
//       if (selected && selected.length > 0 && selected[0].calcType == 0) {
//         setIfDisable(true);
//       } else {
//         setIfDisable(false);
//       }
//     }
//   }, [record]);

  return (
    <>
      {JSON.stringify(record) !== '{}'&&record.shareParamPopout && (
        <ModalForm
          title="编辑成分摊参数"
          width="800px"
          initialValues={{ ...record }}
          labelCol={{ span: 5, offset: 3 }}
          layout={'horizontal'}
          visible={drawerUpdateModalVisible}
          onVisibleChange={(visible) => drawerUpdateModalVisibleChange(visible)}
          onFinish={(value) => onSubmit({ ...record, ...value })}
        >
          <ProFormSelect
            rules={[
              {
                required: true,
                message: (
                  <FormattedMessage
                    id="pages.searchTable.accountingType"
                    defaultMessage=""
                  />
                ),
              },
            ]}
            fieldProps={{
              //这里使用了select的onChange方法，必须使用这样的写法来进行调用onChange方法
              onChange: async (val) => {
                // setSelectedParam(val);
                const { id } = currentResponsibilityRow;
                const resp = await getParamsSelectableList({ id });
                const { data, status } = resp;
                if (status == 200) {
                  const selected = data.list.filter((item) => item.id == val);
                  console.log({ selected });
                  setSelectedParam(selected[0]);
                }
              },
            }}
            request={async () => {
              
              const { id } = currentResponsibilityRow;
              const resp = await getParamsSelectableList({ id });
              const { data, status } = resp;
        
              if (status == 200) {
                const temp = data.list.map((item) => ({
                  label: item.shareParamName,
                  value: item.id,
                }));
                return temp;
              }
            }}
            width="sm"
            name="id"
            label="请选择分摊参数"
          />

          <ProFormDigit width={100} label="分摊比例" name="shareParamPopout" min={0} max={101} />
        </ModalForm>
      )}
    </>
  );
};

export default UpdateFormDrawer;
