<template>
  <div class="dialog" v-if="dialogForm">
    <div class="head_box">
      <span class="title">社会信息人员建设管理{{ title }} </span>
      <div>
        <el-button type="primary" size="small" :loading="loading" @click="submit">保存</el-button>
        <el-button size="small" @click="close">关闭</el-button>
      </div>
    </div>
    <div class="form_cnt">
      <FormMessage v-model="listQuery" :formList="formData" ref="elform" :rules="rules"></FormMessage>
    </div>
  </div>
</template>

<script setup>
import { IdCard } from "@/utils/validate.js";
import FormMessage from "@/components/aboutTable/FormMessage.vue";
import { qcckGet, qcckPost, qcckPut } from "@/api/qcckApi.js";
import * as rule from "@/utils/rules.js";
import {
  ref,
  defineExpose,
  reactive,
  onMounted,
  defineEmits,
  getCurrentInstance,
  nextTick
} from "vue";
const emit = defineEmits(["updateDate"]);
const props = defineProps({
  dic: Object
});
const validateIdentity = () => {
  return (rule, value, callback) => {
    if (!value) {
      return callback(new Error("身份证号不能为空"));
    } else if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(value)) {
      callback(new Error("输入的身份证长度或格式错误"));
    }
    //身份证城市
    var aCity = {
      11: "北京",
      12: "天津",
      13: "河北",
      14: "山西",
      15: "内蒙古",
      21: "辽宁",
      22: "吉林",
      23: "黑龙江",
      31: "上海",
      32: "江苏",
      33: "浙江",
      34: "安徽",
      35: "福建",
      36: "江西",
      37: "山东",
      41: "河南",
      42: "湖北",
      43: "湖南",
      44: "广东",
      45: "广西",
      46: "海南",
      50: "重庆",
      51: "四川",
      52: "贵州",
      53: "云南",
      54: "西藏",
      61: "陕西",
      62: "甘肃",
      63: "青海",
      64: "宁夏",
      65: "新疆",
      71: "台湾",
      81: "香港",
      82: "澳门",
      91: "国外"
    };
    if (!aCity[parseInt(value?.substr(0, 2))])
      callback(new Error("身份证地区非法"));
    // 出生日期验证
    var sBirthday = (
      value.substr(6, 4) +
      "-" +
      Number(value.substr(10, 2)) +
      "-" +
      Number(value.substr(12, 2))
    ).replace(/-/g, "-"),
      d = new Date(sBirthday);
    let yyyy = d.getFullYear();
    let mm = d.getMonth() + 1;
    let dd = d.getDate();
    if (sBirthday !== yyyy + "-" + mm + "-" + dd) {
      listQuery.value.ryCsrq = "";
      callback(new Error("身份证上的出生日期非法"));
    } else {
      let month = mm < 10 ? "0" + mm : mm;
      listQuery.value.ryCsrq = yyyy + "-" + month + "-" + dd;
    }
    // 身份证号码校验
    var sum = 0,
      weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
      codes = "10X98765432";
    for (var i = 0; i < value.length - 1; i++) {
      sum += value[i] * weights[i];
    }
    var last = codes[sum % 11]; //计算出来的最后一位身份证号码
    if (value[value.length - 1] !== last) {
      listQuery.value.ryXb = "";
      callback(new Error("输入的身份证号非法"));
    } else {
      if (value.length === 18) {
        listQuery.value.ryCsrq = IdCard(value, 1);
        listQuery.value.ryXb =
          parseInt(value.substr(16, 1)) % 2 === 1 ? "1" : "2";
      }
    }
    callback();
  };
};
const { proxy } = getCurrentInstance();
const dialogForm = ref(false); //弹窗
const formData = ref([
  { label: "管辖部门", prop: "gxbmDm", type: "department" },
  { label: "姓名", prop: "ryXm", type: "input" },
  { label: "联系电话", prop: "ryLxdh", type: "input" },
  { label: "身份证号", prop: "rySfzh", type: "input" },
  { label: "出生日期", prop: "ryCsrq", type: "date" },
  { label: "性别", prop: "ryXb", type: "select", options: props.dic.D_BZ_XB },
  { label: "民族", prop: "ryMz", type: "select", options: props.dic.D_BZ_MZ },
  { label: "学历", prop: "ryXl", type: "select", options: props.dic.D_BZ_WHCD },
  {
    label: "政治面貌",
    prop: "ryZzmm",
    type: "select",
    options: props.dic.D_BZ_ZZMM
  },
  { label: "所在单位", prop: "rySzdw", type: "input" },
  { label: "居住地地址", prop: "jzdDz", type: "textarea", width: "100%" }
]);
const listQuery = ref({}); //表单
const loading = ref(false);
const elform = ref();
const title = ref("");
const rules = reactive({
  gxbmDm: [{ required: true, message: "请选择管辖部门", trigger: "change" }],
  ryXm: [{ required: true, message: "请输入人员姓名", trigger: "blur" }],
  rySfzh: [
    {
      required: true,
      message: "请输入人员身份证号",
      trigger: ["blur", "change"]
    },
    { trigger: ["blur", "change"], validator: validateIdentity() }
  ],
  ...rule.phoneRule({ require: true, validator: true }, "ryLxdh") // 是否必填 是否进行校验 可以传第二个参数
});
onMounted(() => { });

// 初始化数据
const init = (type, row) => {
  dialogForm.value = true;
  title.value = type == "add" ? "新增" : "编辑";
  if (row) getDataById(row.id);
};
// 根据id查询详情
const getDataById = (id) => {
  qcckGet({}, "/mosty-gsxt/tbGsxtJwry/" + id).then((res) => {
    listQuery.value = res;
  });
};

// 提交
const submit = () => {
  elform.value.submit((data) => {
    let url =
      title.value == "新增"
        ? "/mosty-gsxt/tbGsxtJwry/save"
        : "/mosty-gsxt/tbGsxtJwry/update";
    let params = { ...data, ryLx: "01" };
    qcckPost(params, url)
      .then((res) => {
        proxy.$message({ type: "success", message: title.value + "成功" });
        emit("updateDate");
        close();
      })
      .catch(() => { });
  });
};

// 关闭
const close = () => {
  listQuery.value = {};
  dialogForm.value = false;
  loading.value = false;
};
defineExpose({ init });
</script>

<style lang="scss" scoped>
@import "~@/assets/css/layout.scss";
@import "~@/assets/css/element-plus.scss";
</style>
