<template>
  <div>
    <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="success" plain icon="UploadFilled" @click="handleAdd">算法上传</el-button>
      </el-col>
    </el-row>
    <el-card class="left-list">
      <el-table :data="wrapList">
        <el-table-column type="expand">
          <template #default="scope">
            <div style="display: flex;justify-content: flex-start;gap: 20px;">
              <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" >
          <template #default="scope">
            <div>{{getFielName(scope.row.algorithmFile) }}</div>
          </template>
        </el-table-column>
        <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="MD5核对结果" align="center" prop="md5Result" /> -->
        <el-table-column label="文件大小" align="center" prop="fileSize" />
        <el-table-column label="上传时间" align="center" prop="createTime" />
        <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="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="open" width="500px" 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="24">
            <el-form-item label="算法标签" prop="labels">
              <el-input type="textarea" :rows="4" v-model="form.labels" placeholder="请输入算法标签" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="算法文件" prop="algorithmFile">
              <FileAlgUpload @update:modelValue="getFile" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="配置文件" prop="configFile">
              <FileAlgUpload @update:modelValue="getConfigFile" />
            </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>
  </div>
</template>

<script setup>
import { addFile, deleteFile, updateFile, listFile } from "@/api/algorithmList/algorithmFile.js";
import FileAlgUpload from '@/components/FileAlgUpload'
const { proxy } = getCurrentInstance();
const emit = defineEmits();
const open = ref(false);
const title = ref("");
const total = ref("");
const wrapList = ref([]);
const props = defineProps({
  algId: {
    type: String,
    default: ''
  }
})
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
    postName: undefined,
    status: undefined
  },
  rules: {
    // postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
  }
});

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

/** 表单重置 */
function reset() {
  form.value = {

  };
  proxy.resetForm("algRef");
}

function getFielName(str){
  let array =str.split('/')
  let lastElement = array.pop();
  return lastElement
}

/** 获取文件列表*/
function getList() {
  listFile({ algorithmId: props.algId }).then(response => {
    console.log("🚀 ~ listFile ~ response:", response)
    wrapList.value = response.rows;
    total.value = response.total;
  });
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增算法文件";
}

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

/** 删除按钮操作 */
function handleDelete(row) {
  const id = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除算法文件名称为"' + row.algorithmName + '"的数据项？').then(function () {
    return deleteFile(id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["algRef"].validate(valid => {
    if (valid) {
      form.value.algorithmId = props.algId;
      if (form.value.postId != undefined) {
        updateFile(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFile(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

function getFile(url,size) {
  form.value.algorithmFile = url
  form.value.fileSize = size + 'm'
}

function getConfigFile(url,size) {
  form.value.configFile = url
}

defineExpose({ handleAdd, handleUpdate, getList })
</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>