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 { SaveWorkmanShip, DeleteWorkmanShip, AuditWorkmanShip } from "@/api/modules/saleprice";
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: "deptid",
      title: "部门名称",
      basicinfo: {
        el: "select",
        props: {
          clearable: true,
          placeholder: "通用，不指定部门"
        },
        span: 2,
        editable: ALLOW_EDIT_STATE,
        defaultValue: 0
      }
    },
    {
      field: "mattresstypeid",
      title: "床垫类型",
      basicinfo: {
        el: "select",
        props: {
          clearable: true,
          placeholder: "通用，不指定床垫类型"
        },
        span: 2,
        editable: ALLOW_EDIT_STATE,
        defaultValue: 0
      }
    },
    {
      field: "workmanshipname",
      title: "特殊工艺",
      basicinfo: {
        el: "input",
        span: 1,
        editable: ALLOW_EDIT_STATE,
        props: {
          placeholder: "请输入特殊工艺名称"
        },
        rules: [{ required: true, trigger: "change" }]
      }
    },
    {
      field: "rate",
      title: "工艺利润率",
      width: 100,
      basicinfo: {
        el: "input",
        span: 1,
        editable: ALLOW_EDIT_STATE,
        props: {
          placeholder: "请输入工艺利润率"
        },
        rules: [{ required: true, pattern: new RegExp(/^\d+(\.\d+)?$/), message: "工艺利润率不能为0", trigger: "change" }]
      }
    },
    {
      field: "formula",
      title: "生效条件",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE,
        props: {
          type: "textarea",
          rows: 3,
          placeholder: "可输入生效条件"
        }
      }
    }
  ];

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

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