<template>
  <el-dialog :title="props.title" v-model="isShowDetail" width="800px" :before-close="closed">
    <div class="config">
      <el-form ref='ruleFormRef' label-width="auto" :model="info" label-suffix="：" :rules="rules" label-position="right"
        :disabled="!props.savebtn" style='width:400px;margin:10px auto !important;'>
        <el-form-item label="所属单位" prop="orgName">
          <el-input v-model="info.orgName" type="text" disabled style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="介质名称" prop="preVesMediumName">
          <el-input v-model="info.preVesMediumName" type="text" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="压力容器类型" prop="preVesCategoryCode">
          <div style="width: 250px;">
            <MOSTY.DictRidioAndSelect :dictData="props.sbsslist.D_BZ_YLRQLB" v-model="info.preVesCategoryCode"
              selectType="select" />
          </div>
        </el-form-item>
        <el-form-item label="数量" prop="preVesQty">
          <el-input-number v-model="info.preVesQty" :min="0" style="width:250px" />
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="onSave" v-if="props.savebtn" style="margin-right: 10px">
          <el-icon>
            <DocumentChecked />
          </el-icon>
          <span>确定</span></el-button>
        <el-button @click="closed"> <el-icon>
            <DocumentDelete />
          </el-icon>
          <span>关闭</span></el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, toRefs, onMounted, getCurrentInstance } from 'vue'
import { getApi, postApi } from "@/api/tobAcco_api.js";
import * as MOSTY from "@/components/MyComponents/index";
const { proxy } = getCurrentInstance();
import { ElMessage } from "element-plus";
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  detail: {
    type: Object,
    default: null
  },
  orgObj: Object,
  savebtn: {
    type: Boolean,
    default: false
  },
  sbsslist: {
    type: Object,
    default: {}
  }
})
const emits = defineEmits(["close"]);
const isShowDetail = ref(true)
const ruleFormRef = ref()

const rules = reactive({
  orgName: [{ required: true, message: "请选择单位", trigger: "blur" }],
  preVesMediumName: [{ required: true, message: "请输入介质名称", trigger: "blur" }],
  preVesCategoryCode: [{ required: true, message: "请选择压力容器类型", trigger: "blur" }],
  preVesQty: [{ required: true, message: "请输入数量", trigger: "blur" }],

});
const info = ref({})
onMounted(() => {
  if (props.detail) {
    info.value = props.detail
  } else {
    info.value = {
      orgName: props.orgObj.orgname,
      orgCode: props.orgObj.orgcode
    }
  }

})
const closed = () => {
  if(!ruleFormRef.value) return
  if (props.savebtn) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        emits("close");
      })
      .catch(() => {
        // catch error
      })
  } else {
    emits("close");
  }
};

const onSave = async () => {
  // /
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      const url = props.title.includes('新增') ? '/mosty-jcgl/data/perves/add' : '/mosty-jcgl/data/perves/edit'
      info.value.preVesCategoryName = props.sbsslist.D_BZ_YLRQLB && props.sbsslist.D_BZ_YLRQLB.find(item => item.value == info.value.preVesCategoryCode)?.label
      postApi(info.value, url)
        .then((res) => {
          ElMessage.success(`${props.title.includes('新增') ? '新增' : '编辑'}成功`);
          emits("close");
        })
        .finally(() => {
          // butLoading.value = false
        });
    }
  });
};



</script>
<style scoped lang="scss">
.stage-wrapper .el-dialog__body {
  height: 100% !important;
}

::v-deep .el-dialog__body {
  height: 100% !important;
}

.config {
  height: 20vh;
}

::v-deep .el-form-item--default {
  margin-left: 10px;
}
</style>
