<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"
          :rules="rules" :disabled="!props.formData.saveBtnShow">
          <el-form-item label="所属场所:" prop="sitename">
            <el-input v-model="fromObj.sitename" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item :label="fromObj.publicarea != '1' ? '所属建筑物:' : '所属场区:'" prop="buildingname">
            <el-input v-model="fromObj.buildingname" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item label="所属楼层:" prop="floorname" v-if='fromObj.publicarea != "1"'>
            <el-input v-model="fromObj.floorname" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item :label="fromObj.publicarea != '1' ? '场区部位:' : '所属部位:'" prop="partname" v-if='fromObj.partname'>
            <el-input v-model="fromObj.partname" disabled style="width: 337px" />
          </el-form-item>
          <el-form-item label="区域名称:" prop="regionname">
            <el-input v-model="fromObj.regionname" placeholder="请输入区域名称" clearable style="width: 337px"></el-input>
          </el-form-item>
          <el-form-item label="责任人:" prop="liableperson" style="display: flex;width:100%;">
            <el-input v-model="fromObj.liableperson" type="textarea" :rows="4" readonly style="width: 93%" clearable>
            </el-input>
            <el-button class='cursor ml10' @click="ChooesDdld('02')">选择</el-button>
          </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>

  <!-- 选择责任人 -->
  <ChooseUser v-model="chooseUserVisible" :Single="false" :data="memberList" @choosedUsers="getSonValue">
  </ChooseUser>
</template>

<script setup>
import {
  reactive,
  ref,
  onMounted,
  getCurrentInstance
} from "vue";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
import { getApi, deleteApi, putApi, postApi } from "@/api/tobAcco_api.js";
import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue";
const { proxy } = getCurrentInstance();
const rules = {
  regionname: [{ required: true, message: "请输入区域名称", trigger: "blur" }],
  liableperson: [{ required: true, message: "请选择责任人", trigger: "blur" }]
};
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  },
  type: {
    default: '',
    type: String
  }
});
const saveloading = ref(false)
const fromObj = ref({})
const chooseUserVisible = ref(false); //选择领导弹窗
const memberList = ref([]);
const emit = defineEmits("saveSuccess");
const ruleFormRef = ref();
onMounted(() => {
  fromObj.value = props.formData
  if (props.formData.id) {
    getmain(props.formData.id)
  }
});

//选择部门
function ChooesDdld(val) {
  memberList.value = fromObj.value.liablepersonid ? fromObj.value.liablepersonid.split(',') : [];
  chooseUserVisible.value = true;

}
const getSonValue = (value) => {
  fromObj.value.liableperson = value.map(item => {
    if (item.mobile) {
      return item.userName + '（' + item.mobile + '）'
    } else {
      return item.userName
    }
  }).toString();
  fromObj.value.liablepersonid = value.map(item => {
    return item.id
  }).toString();
};
// 保存
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/region/updateRegionOne")
            .then(() => {
              ElMessage.success("修改成功");
              emit("saveSuccess", 'xg');
            }).finally(() => {
              saveloading.value = false
            })
        } else {
          postApi(fromObj.value, "/mosty-jcgl/region/addRegionOne")
            .then((res) => {
              ElMessage.success("新增成功");
              emit("saveSuccess", 'xz', res);
            }).finally(() => {
              saveloading.value = false
            })
        }
      }
    } else {
      ElMessage.error("请输入必填项!");
      saveloading.value = false
    }
  });
};
//获取详情
function getmain(id) {
  if (id) {
    getApi({}, `/mosty-jcgl/region/getRegionById/${id}`).then((res) => {
      if (res) {
        fromObj.value = res;
        fromObj.value.siteid = props.formData.siteid
        fromObj.value.sitename = props.formData.sitename
        fromObj.value.buildingid = props.formData.buildingid
        fromObj.value.buildingname = props.formData.buildingname
        fromObj.value.floorid = props.formData.floorid
        fromObj.value.floorname = props.formData.floorname
        fromObj.value.partname = props.formData.partname
        fromObj.value.publicarea = props.formData.publicarea ? props.formData.publicarea : null;
        fromObj.value.qtmc = props.formData.qtmc ? props.formData.qtmc : null;
        fromObj.value.sfcq = props.formData.sfcq ? props.formData.sfcq : null;
        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>