<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" v-if='fromObj.sitename'>
            <el-input v-model="fromObj.sitename" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item :label="fromObj.publicarea == '1' ? '所属场区:' : '所属建筑物:'" prop="buildingname"
            v-if='fromObj.buildingname'>
            <el-input v-model="fromObj.buildingname" style="width: 337px" disabled />
          </el-form-item>
          <el-form-item label="所属楼层:" prop="floorname" v-if='fromObj.floorname'>
            <el-input v-model.number="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="responsibilitydeptname" v-if='fromObj.responsibilitydeptname'>
            <el-input v-model="fromObj.responsibilitydeptname" disabled style="width: 337px" />
          </el-form-item>
          <el-form-item label="责任人" prop="responsibilitypersonname" v-if='fromObj.responsibilitypersonname'
            style="width:100%;">
            <el-input v-model="fromObj.responsibilitypersonname" type='textarea' :rows='4' disabled
              style="width: 100%" />
          </el-form-item>

          <el-form-item label="姓名:" prop="username">
            <el-input v-model="fromObj.username" readonly disabled style="width: 337px">
              <template #append>
                <el-button class='cursor' @click="ChooesDdld('01')">选择</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="性别:" prop="sex">
            <el-select v-model="fromObj.sex" style="width: 337px" clearable placeholder="请选择性别：">
              <el-option v-for="item in D_BZ_XB" :key="item" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="电话号码:" prop="phone">
            <MaskInput v-model="fromObj.phone" placeholder="请输入电话号码" style="width: 337px" />
          </el-form-item>
          <!-- <el-form-item label="电话号码:" prop="phone">
            <el-input v-model="fromObj.phone" style="width: 337px" />
          </el-form-item> -->
          <el-form-item label="备注:" prop="bz">
            <el-input v-model="fromObj.bz" clearable style="width: 337px" />
          </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>
      <template #dwbox>
        <!-- 选择带队领导 -->
        <!-- <ChooseUser v-model="chooseUserVisible" :Single="singleShow" :data="memberList" :leaderid="leaderid"
          @choosedUsers="getSonValue"></ChooseUser> -->
        <Wxxfzrytab v-model="chooseUserVisible" :Single="singleShow" :data="memberList" :ssdwid="ssdwid"
          :leaderid="leaderid" @choosedUsers="getSonValue"></Wxxfzrytab>
      </template>

    </MOSTY.FromPage>
  </div>
</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 { getItem, setItem } from "@/utils/storage"
// import ChooseUser from "@/components/MyComponents/ChooseUser/Leader.vue";
import * as rule from "@/utils/rules.js";
import Wxxfzrytab from "./wxxfzrytab.vue";
import MaskInput from "@/components/MaskInput.vue"

const ssdwid = ref(getItem("deptId").ssdwid)
const { proxy } = getCurrentInstance();
const { D_BZ_XB } = proxy.$dict("D_BZ_XB");
const rules = {
  sitename: [{ required: true, message: "请输入场所名称", trigger: "blur" }],
  buildingname: [{ required: true, message: "请选择所属建筑物", trigger: "blur" }],
  floorname: [{ required: true, message: "请选择所属楼层", trigger: "blur" }],
  partname: [{ required: true, message: "请选择所属部位", trigger: "blur" }],
  username: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
  ...rule.phoneRule({ require: true, validator: true }, "phone"),
};
const props = defineProps({
  formData: {
    type: Object,
    default: {}
  }
});
const saveloading = ref(false)
const chooseUserVisible = ref(false); //选择领导弹窗
const singleShow = ref(true); //单选多选
const memberList = ref([]); //成员选择的id回显
const leaderid = ref("");
const zzlx = ref(null);
const fromObj = ref({})
const emit = defineEmits("changePage");
const ruleFormRef = ref();
onMounted(() => {
  fromObj.value = props.formData
});
//点击带队领导选择按钮
function ChooesDdld(val) {
  zzlx.value = val;
  leaderid.value = fromObj.value.membernamecode;
  singleShow.value = true;
  chooseUserVisible.value = true;

}
const getSonValue = (value) => {
  fromObj.value.username = value[0].membername;
  fromObj.value.userid = value[0].memberid;
  fromObj.value.membersid = value[0].id;
  fromObj.value.xfdid = value[0].informationid;
  fromObj.value.sex = value[0].membersex;
};


// 保存
const submit = async (ruleFormRef) => {
  saveloading.value = true
  if (!ruleFormRef) return;
  await ruleFormRef.validate((valid) => {
    if (valid) {
      if (fromObj.value) {
        if (fromObj.value.zbid) {
          if (fromObj.value.id) {
            postApi(fromObj.value, "/mosty-jcgl/xfkzzs/updateWxxfzOne")
              .then(() => {
                ElMessage.success("修改成功");
                emit("changePage", 'xg');
              }).finally(() => {
                saveloading.value = false
              })
          } else {
            postApi(fromObj.value, "/mosty-jcgl/xfkzzs/addWxxfzOne")
              .then((res) => {
                ElMessage.success("新增成功");
                emit("changePage", 'xz', res);
              }).finally(() => {
                saveloading.value = false
              })
          }

        } else {
          if (fromObj.value.ind || fromObj.value.ind == 0) {
            ElMessage.success("修改成功");
            emit("changePage", 'xg', fromObj.value);
            saveloading.value = false
          } else {
            ElMessage.success("新增成功");
            emit("changePage", 'xz', fromObj.value);
            saveloading.value = false
          }
        }



      }
    } else {
      ElMessage.error("请输入必填项!");
      saveloading.value = false
    }
  });
};


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

};

</script>

<style lang="scss" scoped>
::v-deep .el-form-item__content {
  margin-left: 0px !important;
}

.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: 1580px;
  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;
  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%;
}
</style>