import { defineComponent } from "vue";
import { actionProps } from "../types";
import { useRenderIcon } from "/@/components/ReIcon/src/hooks";
import { useResponseHandle } from "/@/hooks/useAsync";

const ActionStatus = defineComponent({
  name: "ActionDelete",
  props: {
    ...actionProps,
    row: {
      type: Object as PropType<any>,
      required: true
    },
    prop: {
      type: String
    }
  },
  emits: ["reload"],
  setup(props, { emit }) {
    const responseHandle = useResponseHandle();

    async function handleStatus() {
      const { config, row, isRequesetArray } = props;
      const { status: rawStatus, id } = row;

      const status = String(rawStatus) === "1" ? "0" : "1";
      const { code, message } = await config.api({
        ...(props.prop
          ? { [props.prop]: row[props.prop] }
          : { id: isRequesetArray ? [id] : id }),
        status
      });

      responseHandle({
        code,
        message,
        handler: () => emit("reload")
      });
    }

    return () => (
      <el-popconfirm
        title={String(props.row.status) === "1" ? "改为禁用？" : "改为启用？"}
        onConfirm={handleStatus}
        v-slots={{
          reference: () => (
            <el-button
              class="reset-margin"
              link
              type="primary"
              icon={useRenderIcon(
                props.row.status === "1"
                  ? "close-circle-line"
                  : "checkbox-circle-line"
              )}
            />
          )
        }}
      />
    );
  }
});

export default ActionStatus;
