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 { SaveBedNetType, DeleteBedNetType } 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",
        span: 2,
        editable: ALLOW_EDIT_STATE,
        rules: [{ required: true }]
      }
    },
    {
      field: "typecode",
      title: "编码",
      basicinfo: {
        el: "input",
        span: 2,
        editable: ALLOW_EDIT_STATE,
        rules: [{ required: true }]
      }
    },
    {
      field: "if_double_net",
      title: "双网",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "if_double_spring",
      title: "双簧",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "if_15cm_strip",
      title: "15分布条",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "if_part",
      title: "多区袋装",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "if_snake_wire",
      title: "蛇线",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "snake_wire_diameter",
      title: "蛇线线径MM",
      basicinfo: {
        el: "input",
        span: 3,
        editable: ALLOW_EDIT_STATE
      }
    },
    {
      field: "snake_wire_mtrl_formula",
      title: "蛇线材料成本公式",
      basicinfo: {
        el: "input",
        offset: 1,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "if_hard_around",
      title: "四周加硬",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "hard_around_mtrl_formula",
      title: "四周加硬材料成本公式",
      basicinfo: {
        el: "input",
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "hard_around_hr_formula",
      title: "四周加硬人力成本公式",
      basicinfo: {
        el: "input",
        offset: 1,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "if_pocket_around",
      title: "四周口袋",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "pocket_around_spring_formula",
      title: "四周口袋弹簧成本公式",
      basicinfo: {
        el: "input",
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "pocket_around_fabrics_formula",
      title: "四周口袋无纺布成本公式",
      basicinfo: {
        el: "input",
        offset: 1,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "if_side_iron",
      title: "边铁",
      basicinfo: {
        editable: ALLOW_EDIT_STATE,
        span: 1,
        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: "side_iron_mtrl_formula",
      title: "边铁材料成本公式",
      basicinfo: {
        el: "input",
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "side_iron_hr_formula",
      title: "边铁人力成本公式",
      basicinfo: {
        el: "input",
        offset: 1,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    },
    {
      field: "glue_mtrl_formula",
      title: "胶水材料成本公式",
      basicinfo: {
        el: "input",
        offset: 1,
        span: 3,
        editable: ALLOW_EDIT_STATE,
        props: { type: "textarea", rows: 3 }
      }
    }
  ];

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

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