<template>
    <el-dialog
      v-model="open"
      :title="title"
      width="800px"
      append-to-body
    >
    <template v-slot:header>
        <div class="cleartitle" style="display: flex;justify-content: flex-start;align-items: center;">
          <img src="@/assets/images/logo_video.png" width="25px" alt=""> <span>{{title}}</span>
        </div>
      </template>
        <el-row :gutter="10" class="mb8">
          <div class="form-title" style="display: flex;justify-content: flex-start;align-items: center;">
                <span>基础信息</span>
            </div>
        </el-row>
        <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-select v-model="form.algorithmPlat" placeholder="请选择硬件平台" clearable >
                      <el-option :label="'英伟达'" :value="'英伟达'"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="算法场景" prop="applicationScenarios">
                    <el-input v-model="form.applicationScenarios" 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="cardImg">
                    <ImageUpload :modelValue="form.cardImg" :limit='1' :fileSize="5" @update:modelValue="getImageUrl"/>
                  </el-form-item>
                </el-col>
              </el-row>
        </el-form>
        <el-row :gutter="10" class="mb8" style="justify-content: space-between;">
          <el-col :span="1.5">
            <div class="form-title" style="display: flex;justify-content: flex-start;align-items: center;">
               <span>算法维护</span>
            </div>
         </el-col>
         <el-col :span="6.8" style="display: flex;">
            <el-button
               type="primary"
               plain
               icon="Search"
               @click="handleAdd"
            >搜索</el-button>
            <el-button
               type="success"
               plain
               icon="Refresh"
               @click="handleUpdate"
            >版本更新</el-button>
            <el-button
               type="success"
               plain
               icon="UploadFilled"
               @click="handleUpdate"
            >算法上传</el-button>
         </el-col>
         </el-row>
         <el-card class="left-list">
          <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
            <el-table-column label="文件名称" align="center" prop="postId" />
            <el-table-column label="状态" align="center" prop="postCode" />
            <el-table-column label="MD5核对结果" align="center" prop="postName" />
            <el-table-column label="文件大小" align="center" prop="postSort" />
            <el-table-column label="进度" align="center" prop="postSort" />
            <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                  <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:post:edit']">修改</el-button>
                  <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:post:remove']">删除</el-button>
                </template>
            </el-table-column>
          </el-table>
          </el-card>
      <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" }],
    applicationScenarios: [{ required: true, message: "算法场景不能为空", trigger: "blur" }],
    cardCopywriting: [{ required: true, message: "卡片文案不能为空", trigger: "blur" }],
    cardImg: [{ 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: '',
    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">
    .form-title{
      display: flex;
      align-items:center;/*for vertical align*/
      font-size: 16px;
      font-weight: 600;
      border-radius: 5px;
      padding: 0;
    }
    .form-title::before{
      content:"";
      display: inline-block;
      width: 10px; /* 矩形的宽度 */
      height: 30px; /* 矩形的高度 */
      background-color: #1890FF; /* 矩形的背景颜色 */
      margin-right: 10px;
      border-radius: 8px;
    }
</style>