<template>
    <el-dialog
      v-model="open"
      :title="title"
      width="800px"
      append-to-body
    >
      <el-form ref="algRef" :model="form" :rules="rules" label-width="80px">
            <el-row>
               <el-col :span="12">
                <el-form-item label="算法名称" prop="algorithmName">
                  <el-input v-model="form.algorithmName" placeholder="请输入岗位名称" />
                </el-form-item>
               </el-col>
               <el-col :span="12">
                <el-form-item label="算法英文" prop="algorithmEnglish">
                  <el-input v-model="form.algorithmEnglish" placeholder="请输入编码名称" />
                </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                <el-form-item label="硬件平台" prop="algorithmPlat">
                  <el-input v-model="form.algorithmPlat" placeholder="请输入编码名称"  />
                </el-form-item>
               </el-col>
               <el-col :span="12">
                <el-form-item label="告警等级" prop="alarmLevel">
                  <el-input v-model="form.alarmLevel" placeholder="请输入告警等级"   />
                </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="12">
                <el-form-item label="卡片文案" prop="cardCopywriting">
                  <el-input v-model="form.cardCopywriting" type="textarea"  :rows="8" placeholder="请输入内容" />
                </el-form-item>
               </el-col>
               <el-col :span="12">
                <el-form-item label="卡片图片" prop="status">
                  <ImageUpload :modelValue="form.cardImg" :limit='1' :fileSize="5" @update:modelValue="getImageUrl"/>
                </el-form-item>
               </el-col>
            </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
</template>

<script setup>
import { addAlg, updateAlg , detailAlg} from "@/api/algorithmList/index.js";
const { proxy } = getCurrentInstance();
const emit = defineEmits();
const open = ref(false);
const title = ref("");
const ids = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    algorithmEnglish: undefined,
    algorithmName: undefined,
    status: undefined
  },
  rules: {
    algorithmName: [{ required: true, message: "算法名称不能为空", trigger: "blur" }],
    algorithmEnglish: [{ required: true, message: "算法英文不能为空", trigger: "blur" }],
    algorithmPlat: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

function getImageUrl(url){
form.value.cardImg=url
console.log("🚀 ~ getImageUrl ~ orm.cardImg:", form)
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    algorithmEnglish: undefined,
    algorithmName: undefined,
    algorithmPlat: 0,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("algRef");
}


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加算法";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  detailAlg(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改算法";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["algRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateAlg(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          emit("getList");
        });
      } else {
        addAlg(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          emit("getList");
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
defineExpose({ handleAdd , handleUpdate })
</script>

<style scoped lang="scss">
// .el-upload--picture-card 控制加号部分
:deep(.hide .el-upload--picture-card) {
    display: none;
}
</style>