index.jsx 15.5 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import AutoTable from "@/components/AutoTable";
import DraggableDialog from "@/components/DraggableDialog";
import InitForm from "@/components/InitForm";
import PremButton from "@/components/PremButton";
import TreeRender from "@/components/TreeRender/sxtree";
import { doFetch } from "@/utils/doFetch";
import editorIsNull from "@/utils/editorIsNull";
import { Box, Container, Grid, Link, Stack, Typography } from "@mui/material";
import { useParams } from "@umijs/max";
import { useRequest } from "ahooks";
import { Badge, message, Tabs } from "antd";
import { useEffect, useMemo, useRef, useState } from "react";
import { history } from "umi";
import "./index.less";

function Dolessons() {
  const params = useParams();
  const formRef = useRef(),
    cksyactionRef = useRef(),
    cjwtactionRef = useRef();
  const [lessonDetail, setlessonDetail] = useState(null),
    [courseContent, setCourseContent] = useState({}),
    [drawer, setDrawer] = useState({
      open: false,
    }),
    [active, setactive] = useState("1");

  const { runAsync, loading } = useRequest(doFetch, {
    manual: true,
    onSuccess: (res, parames) => {
      if (res?.code == "0000") {
        handleClose();
        message.success("操作成功");
        if (active === "2") {
          cksyactionRef?.current?.reload();
        } else if (active === "3") {
          cjwtactionRef?.current?.reload();
        }
      }
    },
  });

  useEffect(() => {
    doFetch({ url: "/busTrain/detail", params: { id: params?.id } }).then(
      (res) => {
        if (res.code === "0000") {
          setlessonDetail(res?.data?.data);
        }
      }
    );
  }, []);

wuhao's avatar
wuhao committed
53 54 55 56 57
  const ifs = useMemo(() => {
    let { type } = lessonDetail ?? {};
    return !(type === 1 || type === 3);
  }, [lessonDetail]);

wuhao's avatar
wuhao committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  const checkCourse = (val) => {
    if (val.length) {
      doFetch({
        url: "/busTrainCatalogue/getCatalogueContent",
        params: { id: val[0] },
      }).then((res) => {
        setCourseContent(res?.data?.data);
        formRef.current.setFieldValue(
          "trainContent",
          res?.data?.data?.trainContent
        );
      });
    }
  };

  const edit = (text, row, _, action) => {
    return (
      <PremButton
        btn={{
          size: "small",
wuhao's avatar
wuhao committed
78
          disabled: ifs,
wuhao's avatar
wuhao committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
          variant: "text",
          onClick: () => {
            setDrawer((v) => ({
              open: true,
              defaultFormValue: { ...row },
              title: "编辑",
            }));
          },
        }}
      >
        编辑
      </PremButton>
    );
  };

  const remove = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
wuhao's avatar
wuhao committed
98
          disabled: ifs,
wuhao's avatar
wuhao committed
99 100 101 102 103 104 105 106 107 108 109
          title: "是否删除该实验?",
          okText: "确认",
          cancelText: "取消",
          onConfirm: async () => {
            await runAsync({
              url: "/busTrainExperiment/remove",
              params: { id: row?.id },
            });
          },
        }}
        btn={{
wuhao's avatar
wuhao committed
110
          disabled: ifs,
wuhao's avatar
wuhao committed
111 112 113 114 115 116 117 118 119 120 121 122
          size: "small",
          color: "error",
        }}
      >
        删除
      </PremButton>
    );
  };
  const removeq = (text, row, _, action) => {
    return (
      <PremButton
        pop={{
wuhao's avatar
wuhao committed
123
          disabled: ifs,
wuhao's avatar
wuhao committed
124 125 126 127 128
          title: "是否删除该实验?",
          okText: "确认",
          cancelText: "取消",
          onConfirm: async () => {
            await runAsync({
wuhao's avatar
wuhao committed
129
              url: "/busTrainQuestion/delete",
wuhao's avatar
wuhao committed
130 131 132 133 134 135 136
              params: { id: row?.id },
            });
          },
        }}
        btn={{
          size: "small",
          color: "error",
wuhao's avatar
wuhao committed
137
          disabled: ifs,
wuhao's avatar
wuhao committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        }}
      >
        删除
      </PremButton>
    );
  };

  const questionColumns = useMemo(() => {
    let col = [
      {
        title: "问题内容",
        dataIndex: "question",
        key: "question",
        width: 400,
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
      {
        title: "参考答案",
        dataIndex: "answer",
        key: "answer",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
    ];
    return col;
  }, []);

  const experimentColumns = useMemo(() => {
    let col = [
      {
        title: "实验名称",
        dataIndex: "experimentName",
        key: "experimentName",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
        render: (_, row) => {
          return (
            <Link
              underline="hover"
              href={row?.url}
              target="_blank"
              rel="noopener"
            >
              {row?.experimentName}
            </Link>
          );
        },
        search: false,
      },
      {
        title: "考试时间(分钟)",
        search: false,
        dataIndex: "testTime",
        key: "testTime",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
      {
        title: "权重(%)",
        search: false,
        dataIndex: "weight",
        key: "weight",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
      {
        search: false,
        title: "创建人",
        dataIndex: "updateUserName",
        key: "updateUserName",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
      {
        title: "创建时间",
        dataIndex: "updateTime",
        search: false,
        key: "updateTime",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
      {
        title: "截止时间",
        dataIndex: "deadline",
        search: false,
        key: "deadline",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
      {
        title: "实验完成率",
        dataIndex: "finishPer",
        search: false,
        key: "finishPer",
        formItemProps: { rules: [{ required: true, message: "此项为必填项" }] },
      },
    ];
    return col;
  }, []);

  const items = [
    {
      key: "1",
      label: `课程目录`,
      children: (
        <Grid container spacing={2}>
          <Grid item width={340}>
            <Box boxShadow={"0 0 18px #f0f0f0"} borderRadius={2} padding={2}>
              <TreeRender
                onselected={checkCourse}
                maxWidth={170}
                url="/busTrainCatalogue/queryCatalogueTree"
                saveurl="/busTrainCatalogue/saveOrUpdateCatalogue"
                deleteurl="/busTrainCatalogue/deleteCatalogue"
                submitKey="catalogue"
wuhao's avatar
wuhao committed
246
                disabled={ifs}
wuhao's avatar
wuhao committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
                params={{
                  trainId: params?.id,
                }}
              />
            </Box>
          </Grid>
          <Grid item flex={1}>
            <Box boxShadow={"0 0 18px #f0f0f0"} borderRadius={2} padding={2}>
              <InitForm
                formRef={formRef}
                fields={[
                  {
                    key: "trainContent",
                    dataIndex: "trainContent",
                    valueType: "Editor",
                    colProps: {
                      span: 24,
                    },
                  },
                ]}
                onFinish={(val) => {
                  const data = { ...val };
                  if (editorIsNull(data?.trainContent)) {
                    return message.warning("备课内容不能为空!", 2);
                  }
                  doFetch({
                    url: "/busTrainCatalogue/saveCatalogueContent",
                    params: {
                      id: courseContent?.id,
                      trainContent: data?.trainContent,
                    },
                  }).then((res) => {
                    if (res.code === "0000") {
                      message.success("操作成功!");
                    }
                  });
                }}
wuhao's avatar
wuhao committed
284
                disabled={ifs}
wuhao's avatar
wuhao committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
              />
            </Box>
          </Grid>
        </Grid>
      ),
    },
    {
      key: "2",
      label: (
        <Badge count={lessonDetail?.experimentNum} size="small" offset={[6, 0]}>
          查看实验
        </Badge>
      ),
      children: (
        <Box boxShadow={"0 0 18px #f0f0f0"} borderRadius={2}>
          <AutoTable
            rerendered={false}
            actionRef={cksyactionRef}
            columns={[
              ...experimentColumns,
              {
                title: "操作",
                valueType: "option",
                width: 180,
                render: (text, row, _, action) => [
                  edit(text, row, _, action),
                  remove(text, row, _, action),
                ],
              },
            ]}
            path="/busTrainExperiment/page"
            extraparams={{
              trainId: params?.id,
            }}
          />
        </Box>
      ),
    },
    {
      key: "3",
      label: "常见问题",
      children: (
        <Box boxShadow={"0 0 18px #f0f0f0"} borderRadius={2}>
          <AutoTable
            rerendered={false}
            actionRef={cjwtactionRef}
            columns={[
              ...questionColumns,
              {
                title: "操作",
                valueType: "option",
                width: 180,
                render: (text, row, _, action) => [
                  edit(text, row, _, action),
                  removeq(text, row, _, action),
                ],
              },
            ]}
wuhao's avatar
wuhao committed
343
            path="/busTrainQuestion/page"
wuhao's avatar
wuhao committed
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
            extraparams={{
              trainId: params?.id,
            }}
          />
        </Box>
      ),
    },
  ];

  const addHandel = (val) => {
    if (active === "1") {
      // 预览
    } else if (active === "2") {
      // 添加实验
      setDrawer({ open: true, title: "添加实验" });
    } else {
      // 添加问题
      setDrawer({ open: true, title: "添加问题" });
    }
  };

  const handleClose = () => {
    setDrawer((s) => ({
      ...s,
      open: false,
    }));
  };

  return (
    <Container maxWidth={false}>
      <DraggableDialog
        handleClose={handleClose}
        dialogprops={drawer}
        loading={loading}
        maxWidth={drawer?.maxWidth ?? "sm"}
      >
        {active === "2" ? (
          <InitForm
            defaultFormValue={drawer?.defaultFormValue ?? null}
            fields={[
              {
                title: "实验",
                dataIndex: "id",
                key: "id",
                valueType: "select",
                options: {
                  path: "/busTrainExperiment/selection",
                  params: { trainId: params?.id },
                },
                formItemProps: {
                  rules: [
                    {
                      required: true,
                      message: "此项为必填项",
                    },
                  ],
                },
              },
              {
                title: "权重(%)",
                dataIndex: "weight",
                key: "weight",
                valueType: "digit",
                formItemProps: {
                  rules: [
                    {
                      required: true,
                      message: "此项为必填项",
                    },
                  ],
                },
              },
              {
                title: "考试时间(分钟)",
                dataIndex: "testTime",
                key: "testTime",
                valueType: "digit",
                formItemProps: {
                  rules: [
                    {
                      required: true,
                      message: "此项为必填项",
                    },
                  ],
                },
              },
              {
                title: "截止时间",
                dataIndex: "deadline",
                key: "deadline",
                valueType: "date",
                formItemProps: {
                  rules: [
                    {
                      required: true,
                      message: "此项为必填项",
                    },
                  ],
                },
              },
            ]}
            onFinish={(val, extra) => {
              let postdata;
              switch (drawer?.title) {
                case "添加实验":
                  postdata = {
                    ...val,
                    trainId: params?.id,
                  };
                  break;
                case "编辑":
                  postdata = {
                    ...val,
                    id: drawer?.defaultFormValue?.id,
                    trainId: params?.id,
                  };
                default:
                  break;
              }
              runAsync({
                url: "/busTrainExperiment/update",
                params: postdata,
              });
            }}
            onValuesChange={(curval, vals, formRef) => {
              if (Object.keys(curval)[0] === "id") {
                doFetch({
                  url: "/busTrainExperiment/detail",
                  params: { id: Object.values(curval)[0] },
                }).then((res) => {
                  formRef?.current?.setFieldsValue({
                    weight: res?.data?.data?.weight,
                  });
                });
              }
            }}
          ></InitForm>
        ) : (
          <InitForm
            defaultFormValue={drawer?.defaultFormValue ?? null}
            fields={[
              {
                title: "问题内容",
                dataIndex: "question",
                key: "question",
                valueType: "textarea",
              },
              {
                title: "参考答案",
                dataIndex: "answer",
                key: "answer",
                valueType: "textarea",
              },
            ]}
            onFinish={(val, extra) => {
              let postdata;
              switch (drawer?.title) {
                case "添加问题":
                  postdata = {
                    ...val,
                    trainId: params?.id,
                  };
                  break;
                case "编辑":
                  postdata = {
                    ...val,
                    id: drawer?.defaultFormValue?.id,
                    trainId: params?.id,
                  };
                default:
                  break;
              }
              runAsync({
wuhao's avatar
wuhao committed
517
                url: "/busTrainQuestion/saveOrUpdate",
wuhao's avatar
wuhao committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
                params: postdata,
              });
            }}
          ></InitForm>
        )}
      </DraggableDialog>

      <Box
        display={"flex"}
        justifyContent={"space-between"}
        alignItems={"center"}
        sx={{ mb: 2.5 }}
        mt={0}
      >
        <Typography variant="h5">
          {lessonDetail?.trainName ?? "暂无名称"}
        </Typography>
        <Stack spacing={2} direction="row">
          <PremButton
            btn={{
              variant: "outlined",
              onClick: (e) => {
                e.stopPropagation();
                history.back();
              },
            }}
          >
            返回
          </PremButton>
          <PremButton
            btn={{
wuhao's avatar
wuhao committed
549
              disabled:ifs,
wuhao's avatar
wuhao committed
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
              variant: "contained",
              onClick: addHandel,
            }}
          >
            {active === "1" ? "预览" : active === "2" ? "添加实验" : "添加问题"}
          </PremButton>
        </Stack>
      </Box>
      <Box>
        <Tabs
          activeKey={active}
          onChange={setactive}
          items={items}
          tabPosition="top"
          animated={true}
        />
      </Box>
    </Container>
  );
}

export default Dolessons;