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 { SaveMtrlDef, DeleteMtrlDef, BanMtrlDef } from "@/api/modules/basicinfo";
import { ElMessage, ElMessageBox } from "element-plus";
import { useHooks as useHooks_mtrltype } from "@/views/baseinfo/mtrltype/hooks/index";
const { gMtrltypeList } = useHooks_mtrltype();
interface defaultState {
  /**
   * @description 单据当前状态
   */
  orderStatus: string;
  /**
   * @description 列表Ref
   */
  VxeTableRef: any;
  /**
   * @description 详情页Ref
   */
  LjDetailRef: any;
  /**
   * @description 详情页明细表格Ref
   */
  VxeTableMxRef: any;
}

const state = reactive<defaultState>({
  orderStatus: "",
  VxeTableRef: null,
  LjDetailRef: null,
  VxeTableMxRef: null
});
/**
 * @description 表格多选数据操作
 * @param {String} rowKey 当表格可以多选时，所指定的 id
 * */
export const useHooks = (t?: any) => {
  // 表格配置项
  const columns: ColumnProps<any>[] = [
    { type: "checkbox", width: 80, fixed: "left" },
    {
      field: "name",
      title: "物料名称",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "price",
      title: "收缩率",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "thickness",
      title: "厚度",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "pricelistprice",
      title: "单价单位",
      visible: false,
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "isuse",
      title: "有效",
      visible: false,
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 2,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-label="1" false-label="0" />
              </div>
            </>
          );
        }
      }
    },
    {
      field: "lastdate",
      title: "有效时间",
      visible: false,
      basicinfo: {
        el: "date-picker",
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "handtype",
      title: "默认类别",
      visible: false,
      basicinfo: {
        el: "tree-select",
        span: 2,
        editable: ALLOW_EDIT_STATE
      },
      enum: gMtrltypeList,
      fieldNames: {
        label: "mtrltype",
        value: "mtrltypeid",
        children: "children"
      }
    },
    {
      field: "pricelistid",
      title: "价格表",
      visible: false,
      limited: true,
      search: {
        el: "select",
        key: "pricelistid",
        props: {
          filterable: false,
          defaultFirstOption: false
        }
      }
    },
    {
      field: "dscrp",
      title: "备注",
      basicinfo: {
        el: "input",
        span: 4,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 5 },
        row: 3
      }
    }
  ];

  const columns_mx: ColumnProps<any>[] = [
    {
      field: "pricetype",
      title: "单价方式",
      filters: [
        { label: "直接单价", value: "0" },
        { label: "公式单价", value: "1" }
      ],
      editRender: {
        name: "VxeSelect",
        options: [
          { label: "直接单价", value: "0" },
          { label: "公式单价", value: "1" }
        ]
      }
    },
    {
      field: "price",
      title: "单价",
      editRender: {
        name: "$input",
        props: {}
      }
    },
    {
      field: "price_formula",
      title: "单价公式",
      editRender: {
        name: "$input",
        props: {}
      }
    },
    {
      field: "qty_formula",
      title: "用量公式",
      editRender: {
        name: "$input",
        props: {}
      }
    }
  ];

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

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

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