<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="24">
                <el-form-item label="算法名称" prop="algorithmName">
                  <el-input v-model="form.algorithmName" placeholder="请输入岗位名称" disabled/>
                </el-form-item>
               </el-col>
               <el-col :span="24">
                <el-form-item label="算法英文" prop="algorithmEnglish">
                  <el-input v-model="form.algorithmEnglish" placeholder="请输入编码名称" disabled/>
                </el-form-item>
               </el-col>
            </el-row>
            <el-row>
               <el-col :span="24">
                <el-form-item label="预警等级" prop="alarmLevel">
                  <el-select v-model="form.alarmLevel" placeholder="请选择预警等级" clearable >
                    <el-option 
                    v-for="disc in levelList"
                    :key="disc.id"
                    :label="disc.alarmName"
                    :value="disc.id"
                    >  
                    <span>
                      <el-tag type="success" :style="{backgroundColor: disc.alarmColor,width:'40px'}">{{ }}</el-tag>
                    </span>
                    {{ disc.alarmName }}
                  </el-option>
                  </el-select>
                </el-form-item>
               </el-col>
            </el-row>
      </el-form>
      <el-card class="left-list">
        <el-table :data="wrapList">
          <el-table-column type="expand">
            <template #default="scope">
              <div m="4">
                <el-tag v-for="item in scope.row.labelList" :key="item.id" :type="'primary'">{{ item.labelCode + ':' +
                  item.labelThreshold }}</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="文件名称" align="center" prop="algorithmName" />
          <el-table-column label="状态" align="center" prop="algorithmStatus" >
          <template #default="scope">
            <el-tag  :type="scope.row.algorithmStatus=='2'?'success':'primary'">{{ scope.row.algorithmStatus=='2'?'使用中':'已上传' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="文件大小" align="center" prop="fileSize" />
          <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)"
                              >版本内容</el-button> -->
              <el-button link type="primary" icon="Delete" @click="useNewFile(scope.row.id)">版本切换</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, updateMyAlg , detailAlg,detailMyAlg} from "@/api/algorithmList/index.js";
import { addFile, deleteFile, updateFile, listFile,useCurrentFil } from "@/api/algorithmList/algorithmFile.js";
import { listAlgLevel} from "@/api/algorithmList/algorithmDown.js";
const { proxy } = getCurrentInstance();
const emit = defineEmits();
const open = ref(false);
const title = ref("");
const levelList = ref([]);
const ids = ref([]);
const wrapList = ref([]);
const data = reactive({
  form: {
    id: '',
    algorithmEnglish: '',
    algorithmName: '',
    algorithmPlat: 0,
    status: "0",
    remark: ''
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    algorithmEnglish: '',
    algorithmName: '',
    status: ''
  },
  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: '',
    algorithmEnglish: '',
    algorithmName: '',
    algorithmPlat: 0,
    status: "0",
    remark: ''
  };
  proxy.resetForm("algRef");
}

/** 查询算法列表 */
function getLevelList() {
  listAlgLevel({ pageNum: 1, pageSize: 100,}).then(response => {
    levelList.value = response.rows
  });
}

/** 获取文件列表*/
function getList(id) {
  listFile({ algorithmId: id }).then(response => {
    wrapList.value = response.rows;
  });
}
/** 获取文件列表*/
function useNewFile(id) {
  useCurrentFil(id).then(response => {
    proxy.$modal.msgSuccess("变更成功");
    getList(ids.value)
  });
}


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

/** 修改按钮操作 */
function handleUpdate(row) {
  console.log("🚀 ~ handleUpdate ~ row:", row)
  const id = row.userAlgorithmId || ids.value;
  ids.value=row.id
  getList(row.id)
  detailMyAlg(id).then(response => {
    form.value = response.data;
    form.value.algorithmName = row.algorithmName;
    form.value.algorithmEnglish = row.algorithmEnglish;
    open.value = true;
    title.value = "修改我的算法";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["algRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateMyAlg(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();
}
getLevelList()
defineExpose({ handleAdd , handleUpdate })
</script>

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