import { updateBase } from '@/services/SmartOps';
import { useRequest } from '@umijs/max';
import { message } from 'antd';
import { useEffect, useState } from 'react';
import BasicParamsForm from './components/BasicParamsForm';
import ControlParamsForm from './components/ControlParamsForm';
import TargetList from './components/TargetList';
import WorkConditionAssessment from './components/WorkConditionAssessment';
import styles from './index.less';

/**
 *
 * @param {number}
 * @returns
 */
const View = (props) => {
  const {
    listRes,
    onEdit,
    current,
    setCurrent,
    process,
    setProcess,
    refresh,
    projectId,
  } = props;
  const sectionId = 7;
  const [edit, setEdit] = useState(false);

  const updateRes = useRequest(updateBase, {
    manual: true,
    onSuccess() {
      refresh();
      message.success('删除成功');
    },
  });
  useEffect(() => {
    if (!current || Object.keys(current).length == 0) return;
    process.control.forEach((item) => {
      item.value = current.control?.[item.name];
    });
    process.base.forEach((item) => {
      item.value = current.base[item.name];
    });
    setProcess({ ...process });
  }, [current]);

  return (
    <>
      <div className={styles.Row} style={{ marginBottom: 20 }}>
        <div className={styles.Col}>
          <WorkConditionAssessment process={process} projectId={projectId} />
        </div>
        <div className={styles.Col}>
          <TargetList
            list={listRes.data}
            loading={listRes.loading}
            current={current}
            onEdit={onEdit}
            setCurrent={setCurrent}
            onDelete={(id) => updateRes.run({ id, is_delete: 1 })}
          />
        </div>
      </div>
      <div className={styles.Row}>
        <div className={styles.Col}>
          <BasicParamsForm current={current} process={process} />
        </div>
        <div className={styles.Col}>
          <ControlParamsForm current={current} process={process} />
        </div>
      </div>
    </>
  );
};

export default View;
