<template>
  <lay-layer :title="modelConfig.title" v-model="modelConfig.visible" :area="['500px', '450px']" :btn="operator">
    <div style="padding: 20px">
      <lay-form :model="modelParams" ref="modelFormRef" required>
        <lay-form-item label="执行指令" prop="commandId">
          <lay-select v-model="modelParams.commandId">
            <lay-select-option v-for="item in commandList" :value="item.instructId" :label="item.title" />
          </lay-select>
        </lay-form-item>
      </lay-form>
    </div>
  </lay-layer>
</template>

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

const { proxy }: any = getCurrentInstance();

interface ModelConfig {
  title: string;
  visible: boolean;
  isUpdate: boolean;
  loading: boolean;
}
interface ModelParams {
  commandId?: string;
}

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

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

let commandList = ref();
let userServerId = ref();

const show = (params?: any) => {
  get_instruct_list();
  modelConfig.visible = true;
  modelConfig.title = "执行指令";
  userServerId.value = params;
  modelParams.value = {};
  return new Promise(async (resolve, reject) => {
    handleResult.resolve = resolve;
    handleResult.reject = reject;
  });
};

const get_instruct_list = () => {
  const params = {};
  get_instruct_list_all(params, (data: any) => {
    if (data.code == 200) {
      commandList.value = data.data;
    }
  });
};

const operator = reactive([
  {
    text: "执行",
    callback: () => {
      const params = { serverList: userServerId.value, ...modelParams.value };
      modelConfig.loading = true;
      run_execut_server(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>
