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 { AuditPriceList } from "@/api/modules/saleprice";
import { ElMessage, ElMessageBox } from "element-plus";

interface defaultState {
  /**
   * @description 单据当前状态
   */
  orderStatus: string;
  /**
   * @description 列表Ref
   */
  VxeTableRef: any;
  /**
   * @description 详情页Ref
   */
  LjDetailRef: any;
}
/**
 * @description 表格多选数据操作
 * @param {String} rowKey 当表格可以多选时，所指定的 id
 * */
export const useHooks = (t?: any) => {
  const state = reactive<defaultState>({
    orderStatus: "",
    VxeTableRef: null,
    LjDetailRef: null
  });

  // 表格配置项
  const columns: ColumnProps<any>[] = [
    { type: "checkbox", width: 80, fixed: "left" },
    { type: "seq", title: "#", fixed: "left", width: 80 },
    {
      field: "pricelistname",
      title: "价格表名称",
      align: "left",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "dscrp",
      title: "备注",
      align: "left",
      width: 300,
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 5 }
      }
    },
    {
      field: "createtime",
      title: "建立时间",
      basicinfo: {
        visible: false
      }
    },
    {
      field: "flag",
      title: "审核",
      datatype: "checkbox",
      basicinfo: {
        visible: false
      }
    },
    {
      field: "modemp",
      title: "修改人",
      basicinfo: {
        visible: false
      }
    },
    {
      field: "moddate",
      title: "修改时间",
      basicinfo: {
        visible: false
      }
    },
    {
      field: "auditemp",
      title: "审核人",
      basicinfo: {
        visible: false
      }
    },
    {
      field: "audidate",
      title: "审核时间",
      basicinfo: {
        visible: false
      }
    },
    {
      field: "inuse",
      title: "禁用",
      datatype: "checkbox",
      basicinfo: {
        visible: false
      }
    }
  ];

  // 审核
  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 { pricelistid: parseInt(item.pricelistid) };
    });
    ElMessageBox.confirm(`是否确定要${operate}吗?`, "询问", {
      confirmButtonText: "是",
      cancelButtonText: "否",
      type: "warning"
    })
      .then(() => {
        AuditPriceList({ list: delArr, type }).then(() => {
          ElMessage.success(`${operate}成功！`);
          state.VxeTableRef?.refresh();
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "操作取消"
        });
      });
  };

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