<template>
  <!-- 审核详情 -->
  <el-dialog :title="diatitle" v-model="open" width="500px" append-to-body>
    <el-form
      ref="dkjbxxRef"
      :model="resultform"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="图斑编号：" prop="dkbh">
        {{ resultform.dkbh }}
      </el-form-item>
      <el-form-item label="审核结论：" prop="endTime">
        <el-radio-group v-model="resultform.decide" :disabled="!issh">
          <el-radio label="0" size="large" border>通过</el-radio>
          <el-radio label="1" size="large" border>不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="审核说明：" prop="notes" :disabled="!issh">
        <el-input
          v-model="resultform.notes"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
    <div>
      <!-- audiText[resultform.auditflowOperateType].replace("合规性", "") -->
    </div>
    <div>
      附件材料：
      <el-upload
        multiple
        :action="uploadFileUrl"
        :file-list="resultform.fjs"
        :limit="10"
        :on-error="handleUploadError"
        :on-success="handleUploadSuccess"
        :show-file-list="false"
        :headers="headers"
        class="upload-file"
        ref="fileUpload"
      >
        <!-- 上传按钮 -->
        <el-button type="primary">上传</el-button>
      </el-upload>
      <!-- <fileUpload
        :limit="10"
        :fileSize="50"
        :fileType="['zip', 'jpg', 'png']"
        :isShowTip="false"
        @changefile="changefile"
      ></fileUpload> -->

      <tablePage
        :cloumn="fileHeader"
        :table="{ total: 20, data: resultform.fjs }"
        :indexed="false"
        :check="false"
        layout=""
      >
        <template #action="{ row, $index }">
          <span class="usable" @click="preview(row)">查看</span>
          <span class="usable" @click="download(row)"> 下载 </span>
          <span class="usable" @click="handleDelete($index)"> 删除 </span>
        </template>
      </tablePage>
    </div>
    <div v-if="issh">说明：当审核结论为退回时，自动退回到核查任务环节。</div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="details">
import { getToken } from "@/utils/auth";

import {
  hgxinfo,
  scinfo,
  tjinfo,
  hgxDkjbxx,
  scDkjbxx,
  tjDkjbxx,
} from "@/api/rsmonitoring/dkjbxx";
const emit = defineEmits(["reset"]);
const { proxy } = getCurrentInstance();
const uploadFileUrl = ref(
  import.meta.env.VITE_APP_BASE_API + "/file/upload/v1"
);
const headers = ref({ Authorization: "Bearer " + getToken() });
const resultdata = reactive({
  open: false,
  issh: false,
  diatitle: "",
  resultform: { fjs: [] },
  // fileList: { total: 0, data: [{}] },
  fileList: [],
  fileHeader: [
    { label: "文件名称", prop: "fjName", align: "center" },
    { label: "文件格式", prop: "fjType", width: "120px" },
    { label: "文件大小(M)", prop: "fjSize", width: "120px" },
    { label: "操作", slot: "action", width: "120px", align: "center" },
  ],
});
const { open, issh, diatitle, resultform, fileList, fileHeader } =
  toRefs(resultdata);
function moreAct(actitem, infoObj) {
  diatitle.value = `${actitem.auditflowStepValue}结果`;
  open.value = true;
  issh.value = false;
  let pram = {
    dkid: infoObj.id,
    auditflowStep: actitem.auditflowStep,
    auditflowId: actitem.id,
  };
  if (actitem.auditflowOperateType == "001")
    hgxinfo(pram).then((res) => {
      resultform.value = res.data || {};
    });
  else if (actitem.auditflowOperateType == "002")
    scinfo(pram).then((res) => {
      resultform.value = res.data || {};
    });
  else if (actitem.auditflowOperateType == "003")
    tjinfo(pram).then((res) => {
      resultform.value = res.data || {};
    });
}
function operate(infoObj, aStep) {
  // if (oname == "sh") {
  open.value = true;
  issh.value = true;
  resultform.value.dkjbxxId = infoObj.id;
  resultform.value.dkbh = infoObj.dkbh;
  resultform.value.auditflowStep = aStep;

  // } else if (oname == "002")
  //   scDkjbxx(pram).then((res) => {
  //     resultform.value = res.data || {};
  //   });
  // else if (oname == "003")
  //   tjDkjbxx(pram).then((res) => {
  //     resultform.value = res.data || {};
  //   });
}

// 审核;
function submitForm() {
  if (!issh.value) {
    open.value = false;
    return;
  }
  proxy.$refs["dkjbxxRef"].validate((valid) => {
    if (valid) {
      scDkjbxx(resultform.value).then((res) => {
        proxy.$modal.msgSuccess("审核成功");
        open.value = false;
        reset();
        emit("reset");
      });
    }
  });
}
function reset() {
  resultform.value = {};
}
function download(row) {
  window.open(row.fjPath);
}
function preview(row) {
  window.open(row.fjPath, "_blank");
}
defineExpose({ moreAct, operate });

// 上传失败
function handleUploadError(err) {
  proxy.$modal.msgError("上传文件失败");
}
function handleUploadSuccess(res, file, UploadFile, uploadFiles) {
  if (res.code === 200) {
    resultform.value.fjs.push({
      fjName: file.name,
      fjPath: res.data.url,
      // url: res.data.path,
      fjSize: Number((file.size / 1024 / 1024).toFixed(2)),
      fjType: file.name.split(".").pop(),
    });
    proxy.$modal.closeLoading();
  } else {
    // number.value--;
    proxy.$modal.closeLoading();
    proxy.$modal.msgError(res.msg);
    proxy.$refs.fileUpload.handleRemove(file);
    // uploadedSuccessfully();
  }
}
function handleDelete(index) {
  resultform.value.fjs.splice(index, 1);
}
</script>

<style>
.upload-file {
  display: inline-block;
}
</style>