import React, { useState, useReducer, useRef, memo } from "react";
import { Card, Button } from "antd";
import { useRequest } from "umi";
import { doFetch } from "@/utils/doFetch";
import getPrem from "@/utils/getPrem"; //权限判断fn
import Tableform from "@/components/Tableform";
import DrawInitForm from "@/components/DrawInitForm";
import defaultFields from "./fields";
const initState = {
  vs: false,
  fields: {},
  iftype: {},
  curitem: {},
};
function reducer(state, action) {
  let { type } = action,
    newState = {};
  switch (type) {
    case "edit":
      newState = {
        ...state,
        vs: true,
        iftype: {
          title: "修改自定义字段",
          val: type,
        },
        fields: { ...action.fields },
        curitem: action.curitem,
      };
      break;
    case "close":
      newState = {
        ...state,
        vs: false,
        fields: {},
        iftype: {},
        curitem: {},
      };
      break;
  }

  return newState;
}
const ExpandedRowRender = memo(
  (props) => {
    const { checkedNodeKey, formCharId, expanded, tableRef, reloadChildren } =
        props,
      columns = [
        {
          title: "自定义字段名称",
          dataIndex: "fieldName",
          key: "fieldName",
          search: false,
        },
        {
          title: "自定义字段属性",
          dataIndex: "fieldCharName",
          key: "fieldCharName",
          search: false,
        },
        {
          title: "选项内容",
          dataIndex: "fieldCharValue",
          key: "fieldCharValue",
          search: false,
        },
        {
          title: "操作",
          dataIndex: "option_dataindex",
          key: "option_dataindex",
          valueType: "option",
          width: 150,
          render: (text, row, _, action) => extraAction(text, row, _, action),
        },
      ],
      [state, dispatch] = useReducer(reducer, initState),
      { vs, fields, iftype, curitem } = state,
      { run, loading } = useRequest(doFetch, {
        manual: true,
        formatResult: (res) => res,
        onSuccess: (result, params) => {
          if (result.code == "0000") {
            reload();
            dispatch({ type: "close" });
          }
        },
      });
    function reload() {
      reloadChildren && reloadChildren();
    }
    function extraAction(text, record, _, action, types) {
      return [
        getPrem("sysDepartment_save", action, "修改", () => {
          for (let i in defaultFields) {
            defaultFields[i].value = record[i];
          }
          dispatch({ type: "edit", fields: defaultFields, curitem: record });
        }),
        getPrem("sysDepartment_deleteById", action, "删除", null, {
          title: "确认删除该自定义字段?",
          onConfirm: () => {},
        }),
      ];
    }
    let saveData = (values, fn) => {
      let newfields = JSON.parse(JSON.stringify(values));
      //新增&修改
      let difrid = { id: curitem.id };
      run({
        url: "/ngic-base-business/paFormField/save",
        params: { ...newfields, ...difrid },
      });
    };
    return (
      <div>
        <div style={{ minHeight: 200 }}>
          {expanded && (
            <Tableform
              columns={columns}
              path="/ngic-base-business/paFormField/queryList"
              actionRef={tableRef}
              extraparams={{
                fieldType: 2,
                formId: checkedNodeKey,
                formCharId: formCharId,
              }}
              bordered={false}
            ></Tableform>
          )}
        </div>
        <DrawInitForm
          title={iftype.title}
          visible={vs}
          onClose={() => dispatch({ type: "close" })}
          footer={false}
          destroyOnClose={true}
          fields={fields}
          submitData={(values) => {
            saveData(values);
          }}
          onChange={(changedValues, allValues) => {
            //联动操作
          }}
          submitting={loading || !vs}
          width={"60%"}
        ></DrawInitForm>
      </div>
    );
  },
  (prev, next) => {
    if (prev.formCharId == next.formCharId) {
      return true;
    } else {
      return false;
    }
  }
);
export default ExpandedRowRender;