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 { SaveMattressType, DeleteMattressType } 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: "typename",
      title: "名称",
      basicinfo: {
        el: "input",
        order: 1,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        rules: [{ required: true }]
      }
    },
    {
      field: "typecode",
      title: "编码",
      basicinfo: {
        el: "input",
        order: 2,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        rules: [{ required: true }]
      }
    },
    {
      field: "if_top_side",
      title: "面裥面",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 6,
        span: 2,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    {
      field: "if_button_sdie",
      title: "底裥面",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 7,
        span: 2,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    {
      field: "if_big_side",
      title: "大侧裥面",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 8,
        span: 2,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    {
      field: "formula_big_side",
      title: "大侧高度公式",
      basicinfo: {
        el: "input",
        order: 18,
        span: 6,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "if_small_side",
      title: "小侧裥面1",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 9,
        span: 1,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    {
      field: "formula_small_side",
      title: "小侧高度公式",
      basicinfo: {
        el: "input",
        order: 19,
        span: 6,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "if_v_side",
      title: "V侧裥面1",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 12,
        span: 1,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    {
      field: "formula_v_side",
      title: "V侧高度公式",
      basicinfo: {
        el: "input",
        order: 20,
        span: 6,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "biandaiqty",
      title: "边带条数",
      basicinfo: {
        el: "input",
        order: 3,
        span: 2,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "hrcost_formula",
      title: "基础款式单价公式",
      basicinfo: {
        el: "input",
        order: 17,
        span: 6,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "dianceng_sort",
      title: "垫层默认设置",
      basicinfo: {
        el: "input",
        order: 15,
        span: 6,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "dianceng_area",
      title: "垫层可选位置",
      basicinfo: {
        el: "input",
        order: 16,
        span: 6,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "if_small_side2",
      title: "小侧裥面2",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 10,
        span: 1,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    ,
    {
      field: "if_small_side3",
      title: "小侧裥面3",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 11,
        span: 1,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    ,
    {
      field: "if_v_side2",
      title: "V侧裥面2",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 13,
        span: 1,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    ,
    {
      field: "if_v_side3",
      title: "V侧裥面3",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        order: 14,
        span: 1,
        render: (scope: any) => {
          const { column, searchParam } = scope;

          return (
            <>
              <div class="flx-align-center">
                <el-checkbox v-model={searchParam[column.field]} true-value={1} false-value={0} />
              </div>
            </>
          );
        }
      }
    },
    {
      field: "zhizao_amt",
      title: "制造费用",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        order: 4,
        span: 2,
        props: {
          placeholder: "请输入制造费用"
        },
        rules: [{ trigger: "change" }]
      }
    },
    {
      field: "guanli_rate",
      title: "管理费率",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        order: 5,
        span: 2,
        props: {
          placeholder: "请输入管理费率"
        },
        rules: [{ trigger: "change" }]
      }
    },
    {
      field: "typerate",
      title: "款式系数",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        span: 6,
        props: {
          type: "number"
        },
        group: "款式系数"
      }
    },
    {
      field: "dannum1_rate",
      title: "散单系数浮动",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        span: 3,
        props: {
          type: "number"
        },
        group: "款式系数"
      }
    },
    {
      field: "dannum2_rate",
      title: "标准系数浮动",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        span: 3,
        props: {
          type: "number"
        },
        group: "款式系数"
      }
    },
    {
      field: "dannum3_rate",
      title: "大单系数浮动",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        span: 3,
        props: {
          type: "number"
        },
        group: "款式系数"
      }
    },
    {
      field: "dannum4_rate",
      title: "小单系数浮动",
      basicinfo: {
        el: "input",
        editable: ALLOW_EDIT_STATE,
        span: 3,
        props: {
          type: "number"
        },
        group: "款式系数"
      }
    }
  ];

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

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