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 { SaveErpMtrlPriceList } from "@/api/modules/basicinfo";
import { getPriceList } from "@/api/modules/saleprice";
import { ElMessage, ElMessageBox } from "element-plus";
import { transformTreeData } from "@/utils/index";
interface defaultState {
  /**
   * @description 单据当前状态
   */
  orderStatus: string;
  /**
   * @description 列表Ref
   */
  VxeTableRef: any;
  /**
   * @description 详情页Ref
   */
  LjDetailRef: any;
  /**
   * @description 详情页明细表格Ref
   */
  VxeTableMxRef: any;
  initParams: any;
}

const state = reactive<defaultState>({
  orderStatus: "",
  VxeTableRef: null,
  LjDetailRef: null,
  VxeTableMxRef: null,
  initParams: { mtrltypeid: undefined as Number }
});
/**
 * @description 表格多选数据操作
 * @param {String} rowKey 当表格可以多选时，所指定的 id
 * */
export const useHooks = (t?: any) => {
  // 表格配置项
  const columns: ColumnProps<any>[] = [
    {
      title: "序",
      field: "rowNum",
      width: 50,
      basicinfo: {
        visible: false
      }
    },
    {
      title: "物料编码",
      field: "mtrlcode",
      table: "u_mtrldef",
      type: "html"
    },
    {
      title: "物料名称",
      field: "mtrlname",
      table: "u_mtrldef",
      type: "html"
    },
    {
      title: "英文名称",
      field: "mtrlengname",
      table: "u_mtrldef"
    },
    {
      title: "物料类别",
      field: "mtrltype",
      table: "u_mtrldef"
    },
    {
      title: "物料规格",
      field: "mtrlmode",
      table: "u_mtrldef"
    },
    {
      title: "单位",
      field: "unit",
      table: "u_mtrldef"
    },
    {
      title: "自定义1",
      field: "mtrlsectype",
      table: "u_mtrldef"
    },
    {
      title: "自定义2",
      field: "zxmtrlmode",
      table: "u_mtrldef"
    },
    {
      title: "自定义3",
      field: "usermtrlmode",
      table: "u_mtrldef"
    },
    {
      title: "物料单价",
      field: "price",
      table: "u_erpmtrl_price",
      basicinfo: {
        el: "input-number",
        editable: ALLOW_EDIT_STATE,
        rules: [{ required: true }]
      }
    },
    {
      title: "建立时间",
      field: "create_date",
      table: "u_erpmtrl_price"
    },
    {
      title: "建立人",
      field: "create_emp",
      table: "u_erpmtrl_price"
    },
    {
      title: "修改时间",
      field: "update_date",
      table: "u_erpmtrl_price"
    },
    {
      title: "修改人",
      field: "update_emp",
      table: "u_erpmtrl_price"
    },
    {
      title: "状态",
      field: "isuse",
      table: "u_mtrldef",
      datatype: "checkbox",
      width: 80,
      basicinfo: {
        visible: false
      }
    }
  ];

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

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