import { reactive, onMounted, ref, ElMessage, ElMessageBox, http, getGroupUrl, RegularVerification, verification, PaginationProps } from "@/utils/importUsed";
import { useRoute } from "vue-router";
import router from "@/router";
// 表单实例
const ruleFormRef = ref()
export function usePriRecursionStartCondition() {
  // 接口列表实例
  let UrlList = reactive(null)
  // 获取当前板块接口列表
  onMounted(async () => {
    const route = useRoute();
    form.loopSetId = Number(route.query.loopSetId);

    UrlList = await getGroupUrl(["prizeSet"]);
    onSearch(ruleFormRef.value);
    loopSetIdQuery();

  });
  let form = reactive({
    loopSetId: null, //递归配置ID
    fieldQueryModel: "", //匹配方式


  });
  const dataList = ref([]);
  const loading = ref(false);
  const dialogAddVisible = ref(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "ID",
      prop: "id",
      minWidth: 200
    },
    {
      label: "递归配置ID",
      prop: "loopSetId",
      minWidth: 200
    },
    {
      label: "条件返回字段",
      prop: "returnFieldId",
      minWidth: 200
    },
    {
      label: "匹配条件",
      prop: "fieldQueryKind",
      minWidth: 200
    },
    {
      label: "匹配方式",
      prop: "fieldQueryModel",
      minWidth: 200
    },
    {
      label: "匹配值",
      prop: "fieldQueryValue",
      minWidth: 200
    },
    {
      label: "匹配值类型",
      prop: "fieldQueryValueType",
      minWidth: 200
    },
    {
      label: "奖励比例",
      prop: "prizePercent",
      minWidth: 200
    },
    {
      label: "奖励固定金额",
      prop: "prizeAmount",
      minWidth: 200
    },

    {
      label: "操作",
      fixed: "right",
      width: 200,
      slot: "operation"
    }
  ];
  // 当前页数量切换
  function handleSizeChange(val: number) {
    if (typeof val === "number") {
      pagination.pageSize = val;
      onSearch(ruleFormRef.value);
    }
  }
  // 当前页码切换
  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    if (typeof val === "number") {
      pagination.currentPage = val;
      onSearch(ruleFormRef.value);
    }
  }
  // 选择表格项
  function handleSelectionChange(val) {
    console.log(`SelectionChange: ${val}`);
    onSearch(ruleFormRef.value);
  }
  // 搜索列表
  async function onSearch(formEl) {
    // 表单校验拦截
    if (!formEl) return
    await formEl.validate(async (valid, fields) => {
      if (valid) {
        //表单校验成功回调
        console.log('submit!')
        // 状态调整为加载中
        loading.value = true;
        // 调用接口（需动态生成接口）
        const { status, msg, data }: any = await http.Request({
          method: UrlList.prizeSet.prigetPriRecursionStartConditionList.method,
          url: UrlList.prizeSet.prigetPriRecursionStartConditionList.url,
          params: {
            ...form,
            pageSize: pagination.pageSize,
            pageNum: pagination.currentPage
          }
        });
        dataList.value = data.records;
        pagination.total = data.total;
        setTimeout(() => {
          loading.value = false;
        }, 500);
      } else {
        //表单校验失败回调
        ElMessage({
          message: "请输入完整信息",
          type: "error"
        });
      }
    })
  }
  // 递归配置ID选项数据
  const loopSetIdOptionList = ref([]);
  //获取递归配置ID数据
  async function loopSetIdQuery() {
    const { status, data }: any = await http.Request({ method: UrlList.prizeSet.prigetPriLoopSetDic.method, url: UrlList.prizeSet.prigetPriLoopSetDic.url, params: {} });
    if (status === 1) {
      loopSetIdOptionList.value = data.records;
    }
  }
  // 匹配方式选项数据
  const fieldQueryModelOptionList = [
    { id: 'request_param', label: '入参' },
    { id: 'fixed_value', label: '固定值' },
  ]




  // 删除
  function handleDelete(row) {
    ElMessageBox.confirm(
      `是否删除该递归开始条件? `,
      "提示",
      {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning"
      }
    ).then(async () => {
      const { status, msg }: any = await http.Request({ method: UrlList.prizeSet.prideletePriRecursionStartCondition.method, url: UrlList.prizeSet.prideletePriRecursionStartCondition.url, params: String(row.id) });
      if (status === 1) {
        ElMessage({
          message: "删除成功",
          type: "success"
        });
        onSearch(ruleFormRef.value);
      } else {
        ElMessageBox.alert(msg, "提示", {
          confirmButtonText: "关闭",
          type: "warning"
        });
      };
    })
  }

  // 新增
  const addVisible = ref(false);
  function handleAdd() {
    addVisible.value = true;
  };

  // 修改
  const editUpdatePriRecursionStartConditionVisible = ref(false);
  const editUpdatePriRecursionStartConditionFormData = ref({});
  function handleUpdatePriRecursionStartCondition(row) {
    editUpdatePriRecursionStartConditionVisible.value = true;
    // 表格数据赋值
    editUpdatePriRecursionStartConditionFormData.value = row;
  };

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    onSearch,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange,
    ruleFormRef,
    loopSetIdQuery,
    loopSetIdOptionList,
    fieldQueryModelOptionList,

    handleAdd,
    addVisible,
    handleUpdatePriRecursionStartCondition,
    editUpdatePriRecursionStartConditionVisible,
    editUpdatePriRecursionStartConditionFormData,
    handleDelete,

  };
}
