import { ref, reactive, computed, toRefs, nextTick } from "vue";
import { Table } from "@/hooks/interface";
import { ColumnProps } from "@/components/LjVxeTable/interface";
import { ALLOW_EDIT_STATE } from "@/config/index";
import { SaveSoftBedFormula, DeleteSoftBedFormula } from "@/api/modules/basicinfo";
import { ElMessage, ElMessageBox, ElRow, ElCol, ElInput, ElButton } from "element-plus";
import EditorModel from "@/views/system/formula-editor/editorModel.vue";

interface defaultState {
  /**
   * @description 单据当前状态
   */
  orderStatus: string;
  /**
   * @description 列表Ref
   */
  VxeTableRef: any;
  /**
   * @description 详情页Ref
   */
  LjDetailRef: any;
  MtrldefDialogRef: any;
  MtrldefDialogProps: any;
  /**
   * @description 公式编辑弹窗Ref
   */
  formulaEditorRef: any;
  /**
   * @description 编辑行
   */
  editRow: any;
}

const state = reactive<defaultState>({
  orderStatus: "",
  VxeTableRef: null,
  LjDetailRef: null,
  MtrldefDialogRef: null,
  MtrldefDialogProps: {},
  formulaEditorRef: null,
  editRow: null
});
/**
 * @description 表格多选数据操作
 * @param {String} rowKey 当表格可以多选时，所指定的 id
 * */
export const useHooks = (t?: any) => {
  // 表格配置项
  const columns: ColumnProps<any>[] = [
    { type: "checkbox", width: 80, fixed: "left" },
    {
      field: "formulaname",
      title: "公式名",
      basicinfo: {
        el: "input",
        // order: 5,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        rules: [{ required: true }]
      }
    },
    {
      field: "use_formula",
      title: "用料量公式",
      basicinfo: {
        el: "input",
        // order: 13,
        span: 6,
        render: (scope: any) => {
          return (
            <>
              <EditorModel v-model={scope.searchParam.use_formula} title="用料量公式编辑器" />
            </>
          );
        }
      }
    },
    {
      field: "price_formula",
      title: "单价公式",
      basicinfo: {
        el: "input",
        // order: 12,
        span: 6,
        render: (scope: any) => {
          return (
            <>
              <EditorModel v-model={scope.searchParam.price_formula} title="单价公式编辑器" />
            </>
          );
        }
      }
    }
  ];

  // 保存
  const fSave = (param: any) => {
    return new Promise((resolve, reject) => {
      ElMessageBox.confirm("是否确定要保存吗?", "询问", {
        confirmButtonText: "是",
        cancelButtonText: "否",
        type: "warning"
      })
        .then(() => {
          SaveSoftBedFormula(param).then(() => {
            ElMessage.success("保存成功！");
            state.VxeTableRef?.refresh();
            resolve({});
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "操作取消"
          });
        });
    });
  };

  // 删除
  const fDelete = () => {
    const checkDate = state.VxeTableRef?.element.getCheckboxRecords();
    if (checkDate.length === 0) {
      ElMessage.error("请选择要删除的数据！");
      return;
    }
    const delArr = checkDate.map((item: any) => {
      return { formulaid: parseInt(item.formulaid) };
    });
    ElMessageBox.confirm("是否确定要删除吗?", "询问", {
      confirmButtonText: "是",
      cancelButtonText: "否",
      type: "warning"
    })
      .then(() => {
        DeleteSoftBedFormula({ list: delArr }).then(() => {
          ElMessage.success("删除成功！");
          state.VxeTableRef?.refresh();
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "操作取消"
        });
      });
  };

  return {
    ...toRefs(state),
    columns,
    fSave,
    fDelete
  };
};
