<template>
  <div class="bigBox">
    <MOSTY.FromPage :title="props.formData.title" @closeDialog="black">
      <template #content>
        <MOSTY.Assort :title="'基础信息'" />
        <el-form class="mosty-from-wrap" ref="ruleFormRef" label-width="130px" :model="fromObj" :inline="true"
          :disabled="!props.formData.saveBtnShow">
          <el-form-item label="所属场所:" prop="sitename">
            <el-input v-model="props.formData.sitename" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item :label="props.formData.publicarea != '1' ? '所属建筑物:' : '所属场区:'" prop="buildingname">
            <el-input v-model="props.formData.buildingname" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item label="所属楼层:" prop="floorname" v-if='props.formData.publicarea != "1"'>
            <el-input v-model="props.formData.floorname" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item :label="props.formData.publicarea != '1' ? '场区部位:' : '所属部位:'" prop="partname"
            v-if='props.formData.partname'>
            <el-input v-model="props.formData.partname" disabled style="width: 337px" />
          </el-form-item>
          <el-form-item label="监测点名称:" prop="name" :rules="[{ required: true, message: '请输入监测点名称', trigger: 'blur' }]">
            <el-input v-model="fromObj.name" placeholder="请输入监测点名称" clearable style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="具体位置:" prop="location"
            :rules="[{ required: true, message: '请输入具体位置', trigger: 'blur' }]">
            <el-input v-model="fromObj.location" clearable style="width: 337px"></el-input>
          </el-form-item>
        </el-form>
      </template>
      <template #footer>
        <el-button type="primary" @click="submit(ruleFormRef)" v-loading='saveloading'
          v-if="props.formData.saveBtnShow"> <el-icon>
            <DocumentChecked />
          </el-icon>保存</el-button>
        <el-button @click="black">
          <el-icon>
            <DocumentDelete />
          </el-icon> 关闭</el-button>
      </template>
    </MOSTY.FromPage>
  </div>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance } from "vue";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
import { getApi, postApi } from "@/api/tobAcco_api.js";
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  },
  type: {
    default: '',
    type: String
  }
});
const saveloading = ref(false)
const fromObj = ref({})
const emit = defineEmits("saveSuccess");
const ruleFormRef = ref();
onMounted(() => {
  fromObj.value.zbid = props.formData.id
  if (props.formData.id) {
    getmain(props.formData.id)
  }
});
// 保存
const submit = async (ruleFormRef) => {
  saveloading.value = true
  if (!ruleFormRef) return;
  await ruleFormRef.validate((valid) => {
    if (valid) {
      if (fromObj.value) {
        if (fromObj.value.id) {
          postApi(fromObj.value, "/mosty-jcgl/aqsbssAtta/update")
            .then(() => {
              ElMessage.success("修改成功");
              emit("saveSuccess", 'xg');
            }).finally(() => {
              saveloading.value = false
            })
        } else {
          postApi(fromObj.value, "/mosty-jcgl/aqsbssAtta/add")
            .then((res) => {
              ElMessage.success("新增成功");
              emit("saveSuccess", 'xz', res);
            }).finally(() => {
              saveloading.value = false
            })
        }
      }
    } else {
      ElMessage.error("请输入必填项!");
      saveloading.value = false
    }
  });
};
//获取详情
function getmain(id) {
  getApi({ id }, `/mosty-jcgl/aqsbssAtta/getInfo`).then((res) => {
    if (res) {
      fromObj.value = res;
      fromObj.value.saveBtnShow = props.formData.saveBtnShow;
    }
  });
}

const black = () => {
  if (props.formData.saveBtnShow) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        // 关闭上一次的表单校验
        ruleFormRef.value.clearValidate();
        emit("saveSuccess");
      })
      .catch(() => {
        // catch error
      })
  } else {
    // 关闭上一次的表单校验
    ruleFormRef.value.clearValidate();
    emit("saveSuccess");
  }

};

</script>

<style lang="scss" scoped>
.table-lable {
  margin: 10px;
  color: var(--el-color-primary);
}

::v-deep ::-webkit-scrollbar {
  display: none !important;
}

::v-deep .el-pagination {
  justify-content: center !important;
}


::v-deep .el-form {
  width: 99%;
  margin: auto;
  padding: 10px;
  margin-top: 10px;
  flex-wrap: wrap;
  justify-content: start;
}

.el-form-item {
  display: flex;
  margin-right: 20px;
}

.bigBox {
  display: block !important;
  // padding: 0 14px;
  box-sizing: border-box;
}

::v-deep .el-table .cell {
  padding: 0 0 !important;
}

::v-deep .el-table--border .el-table__cell .cell {
  padding-left: 0px !important;

  >input {
    width: 100%;
    height: 33px;
  }
}



::v-deep .el-upload-list {
  width: 50%;
}

::v-deep .el-form-item__content {
  margin-left: 0px !important;
}
</style>