import { ref, reactive, computed, toRefs } from "vue";
import { Table } from "@/hooks/interface";
import { ColumnProps } from "@/components/LjVxeTable/interface";
import { ALLOW_EDIT_STATE } from "@/config/index";
import { SaveSpring, DeleteSpring, AuditSpring } from "@/api/modules/basicinfo";
import { ElMessage, ElMessageBox } from "element-plus";
interface defaultState {
  /**
   * @description 单据当前状态
   */
  orderStatus: string;
  /**
   * @description 列表Ref
   */
  VxeTableRef: any;
  /**
   * @description 详情页Ref
   */
  LjDetailRef: any;
}

const state = reactive<defaultState>({
  orderStatus: "",
  VxeTableRef: null,
  LjDetailRef: null
});
/**
 * @description 表格多选数据操作
 * @param {String} rowKey 当表格可以多选时，所指定的 id
 * */
export const useHooks = (t?: any) => {
  // 表格配置项
  const columns: ColumnProps<any>[] = [
    { type: "checkbox", width: 80, fixed: "left" },
    {
      field: "line_diameter",
      title: "线径MM",
      search: {
        el: "input",
        order: 3
      },
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "height",
      title: "高度CM",
      search: {
        el: "input"
      },
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "caliber",
      title: "口径CM",
      search: {
        el: "input"
      },
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "center_diameter",
      title: "中心直径CM",
      search: {
        el: "input"
      },
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "cyclenum",
      title: "圈数",
      search: {
        el: "input"
      },
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "gram_weight",
      title: "克重KG",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "dscrp",
      title: "备注",
      basicinfo: {
        el: "input",
        span: 4,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "bedtypeid",
      title: "床网类型",
      search: {
        el: "select",
        key: "bedtypeid",
        order: 1,
        props: {
          filterable: false,
          defaultFirstOption: false
        }
      },
      basicinfo: {
        el: "select",
        span: 2,
        editable: ALLOW_EDIT_STATE,
        rules: [{ required: true }],
        defaultValue: "0"
      }
    },
    {
      field: "springtypeid",
      title: "弹簧类型",
      search: {
        el: "select",
        key: "springtypeid",
        order: 2,
        props: {
          filterable: false,
          defaultFirstOption: false
        }
      },
      basicinfo: {
        el: "select",
        span: 2,
        editable: ALLOW_EDIT_STATE,
        defaultValue: "0"
      }
    },
    {
      field: "free_height",
      title: "自由高度",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "actual_height",
      title: "实际高度",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "hardness",
      title: "硬度",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "hardness_indentation_1",
      title: "压陷硬度10%",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "hardness_indentation_2",
      title: "压陷硬度20%",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "hardness_indentation_4",
      title: "压陷硬度40%",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "arrangement_width",
      title: "排列宽",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "arrangement_height",
      title: "排列长",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "roll_width",
      title: "卷排列宽",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "roll_length",
      title: "卷排列长",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "comperssion_rate",
      title: "压缩比",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    }
  ];

  // 保存
  const fSave = (param: any) => {
    return new Promise((resolve, reject) => {
      ElMessageBox.confirm("是否确定要保存吗?", "询问", {
        confirmButtonText: "是",
        cancelButtonText: "否",
        type: "warning"
      })
        .then(() => {
          SaveSpring(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 { springid: parseInt(item.springid) };
    });
    ElMessageBox.confirm("是否确定要删除吗?", "询问", {
      confirmButtonText: "是",
      cancelButtonText: "否",
      type: "warning"
    })
      .then(() => {
        DeleteSpring({ list: delArr }).then(() => {
          ElMessage.success("删除成功！");
          state.VxeTableRef?.refresh();
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "操作取消"
        });
      });
  };

  // 审核
  const fAudit = type => {
    let operate = "审核";
    switch (type) {
      case 1:
        operate = "撤审";
        break;
      case 2:
        operate = "禁用";
        break;
      case 3:
        operate = "反禁用";
        break;
      default:
        operate = "审核";
        break;
    }

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

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