<template>
  <BaseDialog
    :dialogVisible="props.visible"
    @close="onclone"
    :titleName="titleName + '人员'"
    width="50%"
    @onSubmit="handleSubmit"
    :footerclosed="true"
    :footerkeepnaem="readonly ? false : true"
    :diafooter="true"
  >
    <div class="detailForm-content">
      <el-form
        ref="formRef"
        label-width="120"
        :rules="ruleForm"
        :model="formData"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
            :disabled="readonly"
            placeholder="请输入姓名"
            type="input"
            v-model="formData.name"
          />
        </el-form-item>
        <el-form-item label="电话" prop="phoneNumber">
          <el-input
            :disabled="readonly"
            placeholder="请输入电话"
            type="input"
            v-model="formData.phoneNumber"
          />
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-select
            :disabled="readonly"
            placeholder="请选择"
            v-model="formData.sex"
          >
            <el-option label="男" :value="1">男</el-option>
            <el-option label="女" :value="0">女</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人员标签" prop="peopleTag">
          <el-select
            :disabled="readonly"
            placeholder="请选择人员标签"
            v-model="formData.peopleTag"
          >
            <el-option label="常住人口" :value="1">常住人口</el-option>
            <el-option label="流动人口" :value="2">流动人口</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关系" prop="relationId">
          <el-select
            :disabled="readonly"
            placeholder="请选择关系"
            v-model="formData.relationId"
          >
            <el-option
              v-for="(item, index) in crRelationOptions"
              :key="index"
              :label="item.relationName"
              :value="item.relationId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker
            :disabled="readonly"
            v-model="formData.birthday"
            type="date"
            placeholder="请选择出生日期"
            size="default"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="类型" prop="typeId" style="width: 100%;">
          <el-select
            multiple
            :disabled="readonly"
            placeholder="请选择类型"
            v-model="formData.typeId"
          >
            <el-option
              v-for="(item, index) in typeList"
              :key="index"
              :label="item.typeName"
              :value="item.typeId"
              >{{ item.typeName }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="工作地点" prop="workAddress" style="width: 100%;">
          <el-input
            v-model="formData.workAddress"
            :disabled="readonly"
            :rows="2"
            type="textarea"
            placeholder="请输入工作地点"
          />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard" style="width: 100%;">
          <el-input
            :disabled="readonly"
            placeholder="请输入身份证号"
            type="input"
            v-model="formData.idCard"
          />
         <el-button @click="getPeopleIdCard" type="primary" round class="idCardButton">查看身份证号</el-button>
        </el-form-item>
        <el-form-item label="当前地址" prop="nowAddress" style="width: 100%;">
          <el-input
            :disabled="readonly"
            placeholder="请输入当前地址"
            type="input"
            v-model="formData.nowAddress"
          />
        </el-form-item>
        <el-form-item
          v-if="formData.peopleId"
          label="死亡状态"
          prop="deathState"
        >
          <el-select
            :disabled="readonly"
            placeholder="请选择死亡状态"
            v-model="formData.deathState"
          >
            <el-option label="正常" :value="1">正常</el-option>
            <el-option label="死亡" :value="2">死亡</el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="formData.peopleId"
          label="死亡时间"
          prop="deathTime"
        >
          <el-date-picker
            :disabled="readonly"
            v-model="formData.deathTime"
            type="date"
            placeholder="请选择死亡时间"
            size="default"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
    </div>
  </BaseDialog>
</template>
  <script setup lang='ts'>
import { reactive, ref, onMounted, watch } from "vue";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { areaTree } from "@/api/account";
import { crTypeList, peopleAdd, peopleDetail, crRelationList, peopleIdCard } from "@/api/base";
import useUserStore from "@/store/modules/user";
const userStore = useUserStore();
const user = ref(JSON.parse(userStore.userInfo));
const userId = ref(user.value.userId);

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  readonly: {
    type: Boolean,
    default: false,
  },
  id: {
    type: String,
    default: "",
  },
  form: Object,
});
const emits = defineEmits(["close"]);
const titleName = ref("新增");
// 表单
const formRef = ref<FormInstance>();
let isCardId = (rule, value, callback) => {
  if (!value) {
    return new Error("请输入身份证号)");
  } else {
    const reg = /^[0-9*]{15}$|^[0-9*]{17}[0-9Xx]$/;
    //  /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/; 身份证验证
    const card = reg.test(value);
    if (!card) {
      //判断是否15-18位
      callback(new Error("请输入长度为15或者18位"));
    } else {
      callback();
    }
  }
};
const ruleForm = reactive<FormRules>({
  name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  phoneNumber: [{ required: true, message: "请输入电话", trigger: "blur" }],
  idCard: [
    { required: true, message: "请输入身份证号", trigger: "blur" },
    { validator: isCardId, trigger: "blur" },
  ],
  sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
  nowAddress: [{ required: true, message: "请输入地址", trigger: "blur" }],
  peopleTag: [{ required: true, message: "请选择人员标签", trigger: "blur" }],
  relation: [{ required: true, message: "请选择人员关系", trigger: "blur" }],
  typeId: [{ required: true, message: "请选择类型", trigger: "blur" }],
});

const formData = ref<any>({});
//保存并退出
const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      if (formData.value.deathState == 2 && !formData.value.deathTime) {
        ElMessage.error({
          message: "请选择死亡时间",
          type: "error",
        });
      } else {
        let params = {
        ...formData.value,
        peopleId: formData.value.peopleId ? formData.value.peopleId : null,
        domicileId:props.id
      };
        peopleAdd(params).then((res: any) => {
          if (res.code == 1) {
            ElMessage.success({
              message: "操作成功",
              type: "success",
            });
            onclone();
          } else {
            ElMessage.error({
              message: res.message,
              type: "error",
            });
          }
        });
      }
    }
    return false;
  });
};
//获取类型
const typeList = ref([]);
const getCrTypeList = () => {
  crTypeList(userId.value).then((res: any) => {
    typeList.value = res.data;
  });
};
//查看人员详情
const getPeopleDetail = (id) => {
  peopleDetail({ peopleId: id }).then((res: any) => {
    console.log(res,'res===>');
    
    formData.value = res.data;
    formData.value.typeId = res.data.typeIds;
    formData.value.peopleTag = res.data.peopleTag * 1;
  });
};
//获取关系list
const crRelationOptions = ref([]);
const getCrRelationList = () => {
  crRelationList().then((res: any) => {
    crRelationOptions.value = res.data;
  });
};
//关闭弹窗
const onclone = () => {
  formRef.value?.clearValidate();
  formData.value = {};
  emits("close");
};
//获取身份证明文
const getPeopleIdCard = () =>{
  let params = {
      idCard:formData.value.idCard,
      peopleId:formData.value.peopleId,
  }
  peopleIdCard(params).then((res: any) => {
    if (res.data.idCard) {
      formData.value.idCard = res.data.idCard;
    }
  });
}
onMounted(() => {
  getCrTypeList();
  getCrRelationList();
});

//详情
watch(
  () => props.form,
  (val: any) => {
    titleName.value = val.peopleId ? "编辑" : "新增";
    if (val.peopleId) {
      getPeopleDetail(val.peopleId);
    }
  }
);
</script>
<style scoped lang="scss">
.detailForm-content {
    margin-right: 30px;
}
.idCardButton {
    margin-top: 10px;
}
</style>