<template>
  <lay-layer :title="modelConfig.title" v-model="modelConfig.visible" area="auto" :btn="operator">
    <div class="width-500 custom-layer" style="padding: 20px">
      <lay-form :model="modelParams" ref="modelFormRef" required>
        <lay-form-item label="是否自动调参" prop="isAuto">
          <lay-switch v-model="modelParams.isAuto" :onswitch-value="1" :unswitch-value="0" />
          <div class="tips">开启后会自动开关机机器，自动修改参数</div>
        </lay-form-item>
        <lay-form-item label="是否参考币安" prop="isRefBinance">
          <lay-switch v-model="modelParams.isRefBinance" />
          <div class="tips">开启后如果币安交易所存在该币对就会优先参考币安交易所</div>
        </lay-form-item>
        <lay-form-item label="调参间隔时间" prop="timeInterval">
          <lay-input v-model="modelParams.timeInterval" placeholder="调参间隔时间（分钟）" />
        </lay-form-item>
        <lay-form-item label="取第几个" prop="coinIndex">
          <lay-input v-model="modelParams.coinIndex" placeholder="设置排行榜排名第几的币对" />
        </lay-form-item>
        <lay-form-item label="开仓系数" prop="openRatio">
          <lay-input v-model="modelParams.openRatio" placeholder="排行榜开仓值乘以系数" />
        </lay-form-item>
        <lay-form-item label="最小总分数" prop="scoreLimit">
          <lay-input v-model="modelParams.scoreLimit" placeholder="排行榜总分数最小值" />
        </lay-form-item>
        <lay-form-item label="最小波动分数" prop="fluctuationScore">
          <lay-input v-model="modelParams.fluctuationScore" placeholder="排行榜最小波动分数最小值" />
        </lay-form-item>
        <lay-form-item label="最小频次分数" prop="frequencyScore">
          <lay-input v-model="modelParams.frequencyScore" placeholder="排行榜最小交易频次分数最小值" />
        </lay-form-item>
        <lay-form-item label="最小量分数" prop="transactionVolumeScore">
          <lay-input v-model="modelParams.transactionVolumeScore" placeholder="排行榜最小交易量分数最小值" />
        </lay-form-item>
      </lay-form>
    </div>
  </lay-layer>
</template>

<script lang="ts" setup>
import { ref, reactive, getCurrentInstance } from "vue";
import { set_robot_auto } from "@/api";

const { proxy }: any = getCurrentInstance();

const modelFormRef = ref();

interface ModelConfig {
  title: string;
  visible: boolean;
  isUpdate: boolean;
  loading: boolean;
}
interface ModelParams {
  id?: string;
  exchange?: string;
  isAuto?: number;
  isRefBinance?: boolean;
  timeInterval?: string;
  coinIndex?: string;
  openRatio?: string;
  scoreLimit?: string;
  fluctuationScore?: string;
  frequencyScore?: string;
  transactionVolumeScore?: string;
}

let modelParams = ref<ModelParams>({});
let modelConfig: ModelConfig = reactive({ title: "", visible: false, isUpdate: false, loading: false });

let handleResult = reactive<{ resolve?: any; reject?: any }>({});

const show = (params?: any) => {
  modelConfig.visible = true;
  modelConfig.title = "设置自动调参";
  let exchange = params.configList.find((item: any) => item.code == "exchange")?.val || undefined;
  let refExchange = params.configList.find((item: any) => item.code == "ref_exchange")?.val || undefined;
  modelParams.value = { id: params.id, isAuto: params.isAuto || 0, isRefBinance: false, ...params.autoConfig, exchange, refExchange };
  return new Promise(async (resolve, reject) => {
    handleResult.resolve = resolve;
    handleResult.reject = reject;
  });
};

const operator = reactive([
  {
    text: "确认",
    callback: () => {
      modelFormRef.value.validate((isValidate: boolean) => {
        if (isValidate) {
          modelConfig.loading = true;
          const params = {
            robotId: modelParams.value.id,
            exchange: modelParams.value.exchange,
            isAuto: modelParams.value.isAuto,
            isRefBinance: modelParams.value.isRefBinance,
            timeInterval: modelParams.value.timeInterval,
            coinIndex: modelParams.value.coinIndex,
            openRatio: modelParams.value.openRatio,
            scoreLimit: modelParams.value.scoreLimit,
            fluctuationScore: modelParams.value.fluctuationScore,
            frequencyScore: modelParams.value.frequencyScore,
            transactionVolumeScore: modelParams.value.transactionVolumeScore,
          };
          set_robot_auto(params, (data: any) => {
            modelConfig.loading = false;
            if (data.code == 200) {
              proxy.$message("设置成功！");
              modelConfig.visible = false;
              handleResult.resolve(true);
            }
          });
        }
      });
    },
  },
  {
    text: "取消",
    callback: () => {
      modelConfig.visible = false;
      handleResult.resolve(false);
    },
  },
]);
defineExpose({ show });
</script>
<style lang="scss" scoped>
.tips {
  font-size: 12px;
  color: #2c2c2c;
  &.danger {
    color: var(--button-danger-color);
  }
}
.split-text {
  padding: 10px 0 30px;
}
</style>
