<template>
  <div>
    <el-dialog :title="props.title" width="1000px" v-model="props.addDiaShow" :before-close="handleClose">
      <!-- 培训计划新增或修改 -->
      <div class="config">
        <el-form ref="fromref" :model="ruleForm" :rules="rules" label-width="150px" class="demo-ruleForm.value"
          :disabled='!saveBtnShow' style="width: 100% !important;">
          <el-form-item label="经办单位：" prop="jname">
            <el-input v-model="ruleForm.jname" clearable style="width: 100%"></el-input>
          </el-form-item>
          <el-form-item label="性别：" prop="jsex">
            <MOSTY.DictRidioAndSelect :dictData="props.zdlist.D_BZ_XB" v-model="ruleForm.jsex" selectType="select"
              :disabled='!saveBtnShow' style="width: 100%" />
          </el-form-item>
          <el-form-item label="关系：" prop="impact">
            <MOSTY.DictRidioAndSelect :dictData="props.zdlist.D_TB_IMPACT" v-model="ruleForm.impact" selectType="select"
              :disabled='!saveBtnShow' style="width: 100%" />
          </el-form-item>
          <el-form-item label="出生年月：" prop="jbirth">
            <el-date-picker v-model="ruleForm.jbirth" style="width: 100%" type="date" format="YYYY-MM-DD"
              value-format="YYYY-MM-DD" />
          </el-form-item>
          <el-form-item label="年龄：" prop="jage">
            <el-input type="number" v-model="ruleForm.jage" style="width: 100%" />
          </el-form-item>
          <el-form-item label="职业情况" prop="professionalsttus">
            <el-input type="text" v-model="ruleForm.professionalsttus" style="width: 100%" />
          </el-form-item>
          <el-form-item label="政治面貌：" prop="jzzmm">
            <MOSTY.DictRidioAndSelect :dictData="props.zdlist.D_BZ_AQRYZZMM" v-model="ruleForm.jzzmm"
              selectType="select" :disabled='!saveBtnShow' style="width: 100%" />
          </el-form-item>
          <el-form-item label="文化程度：" prop="jleducation">
            <MOSTY.DictRidioAndSelect :dictData="props.zdlist.D_BZ_AQRYWHCD" v-model="ruleForm.jleducation"
              selectType="select" :disabled='!saveBtnShow' style="width: 100%" />
          </el-form-item>
          <el-form-item label="工作单位：" prop="jworkunit">
            <el-input v-model="ruleForm.jworkunit" clearable style="width: 100%"></el-input>
          </el-form-item>
          <!-- <el-form-item label="籍贯：" prop="jnativeplace">
                    <MOSTY.DictRidioAndSelect :dictData="props.zdlist.D_BZ_XZQH" v-model="ruleForm.jnativeplace"
                                selectType="select" :disabled='!saveBtnShow' style="width: 100%"/>
          </el-form-item> -->
          <el-form-item label="身体情况：" prop="jbody">
            <el-input v-model="ruleForm.jbody" clearable style="width: 100%"></el-input>
          </el-form-item>
          <el-form-item label="工资收入：" prop="wageincome">
            <el-input type="number" v-model="ruleForm.wageincome" style="width: 100%" />
          </el-form-item>
          <el-form-item label="备注：" style='width:100%'>
            <el-input v-model="ruleForm.jremark" style="width: 100%" />
          </el-form-item>

        </el-form>
      </div>

      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" :loading="loading" @click="handleSave(fromref)" v-if="saveBtnShow">
            <el-icon>
              <DocumentChecked />
            </el-icon>
            <span>保存</span>
          </el-button>
          <el-button @click="handleClose" type="primary">
            <el-icon>
              <DocumentDelete />
            </el-icon>
            <span>关闭</span>
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
// 引入
import {
  ref,
  onMounted,
  defineProps,
  defineEmits,
  reactive,
  watchEffect,
  getCurrentInstance
} from "vue";
import * as rule from "@/utils/rules.js";
import * as MOSTY from "@/components/MyComponents/index";
import { ElMessage } from "element-plus";
import { postApi } from "@/api/tobAcco_api.js";
const { proxy } = getCurrentInstance();
//表单数据
const ruleForm = ref({
});
const fromref = ref()
//规则校验
const rules = reactive({
  jname: [{ required: true, message: "经办单位不能为空", trigger: "blur" }],
  jsex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
  impact: [{ required: true, message: "关系不能为空", trigger: "blur" }],
  jbirth: [{ required: true, message: "出生年月不能为空", trigger: "blur" }],
  ...rule.numberzzsRule({ validator: true, require: true, num: 90 }, "jage"),
  jzzmm: [{ required: true, message: "政治面貌不能为空", trigger: "blur" }],
  jleducation: [{ required: true, message: "文化程度不能为空", trigger: "blur" }],
  jworkunit: [{ required: true, message: "工作单位不能为空", trigger: "blur" }],
  jbody: [{ required: true, message: "身体情况不能为空", trigger: "blur" }],
  ...rule.numbernineRule({ validator: true, require: false, iszs: true, firstNum: 9, lastNum: 2 }, "wageincome"),
});
const props = defineProps({
  addDiaShow: {
    type: Boolean,
    default: false
  },
  rowxq: {
    type: Object,
    default: () => { }
  },
  title: String,
  zdlist: Array,
  saveBtnShow: {
    default: true,
    type: Boolean
  }
});

watchEffect(() => {
  if (props.rowxq) {
    ruleForm.value = JSON.parse(JSON.stringify(props.rowxq));
  } else {
    ruleForm.value = {}
  }
});
const emits = defineEmits(["changeAddVi", "changeclose"]);
const loading = ref(false)
onMounted(() => { });

// 关闭
const handleClose = () => {
  if (props.saveBtnShow) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        emits("changeclose", false);
      })
      .catch(() => {
        // catch error
      })
  } else {
    emits("changeclose", false);
  }

};

// 关闭×
const closeDialog = () => {
  emits("changeclose", false);
};
// 保存
const handleSave = async (formEl) => {
  loading.value = true
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.value) {
        if (ruleForm.value.id) {
          postApi(ruleForm.value, "/mosty-jcgl/driverDft/updateDriverDft")
            .then(() => {
              ElMessage.success("修改成功");
              emits("changeclose", false);
              emits("changeAddVi", ruleForm.value);
              loading.value = false
            }).catch((err) => {
              loading.value = false
            })
        } else {
          emits("changeAddVi", ruleForm.value);
          loading.value = false
          emits("changeclose", false);
        }
      }
    } else {
      ElMessage.warning('请输入必填项！！！')
      loading.value = false
    }
  })
}

</script>

<style lang="scss" scoped>
.fj {
  .el-form {
    padding: 10px;
    margin-top: 10px;
    flex-wrap: wrap;
    // width: 1170px;
    justify-content: flex-end;
  }

  .from_fix {
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    margin-top: 10px;

    // .el-form-item--default .el-form-item__content {
    //   flex-wrap: nowrap !important;
    // }

    .el-input {
      width: 100%;
    }
  }
}

::v-deep .el-form-item__content {
  flex-wrap: nowrap !important;
}

.config {
  width: 955px;

  .el-form {
    padding: 10px 40px;
    margin-top: 10px;
    flex-wrap: wrap;
    justify-content: space-between;
  }

  .el-form-item {
    width: 46%;
    text-align: center;
  }
}

.pxdx_box {
  width: 500px;
}

.titleBox {
  align-items: center;
  margin: 0;
  padding: 0;
  height: 60px;
  border: 1px solid rgb(32, 75, 54);
  padding: 20px;
}

.form>div {
  position: relative;
  margin-top: 30px;
  display: flex;
  justify-content: space-between;

  div {
    display: flex;
    align-items: center;

    .el-select {
      width: 65%;
    }
  }
}

.el-input {
  width: 75%;
}

.two .el-input {
  width: 65%;
}

.three {
  justify-content: flex-start !important;

  .el-select {
    width: 15%;
  }
}

.four {
  display: flex;
  align-items: center;

  span {
    width: 5%;
  }

  .el-textarea__inner {
    width: 95%;
  }
}

.group {
  margin-top: 50px;
  // background-color: red;
  position: relative;
}

.fj {
  margin-top: 50px;
  width: 100%;

  .content {
    width: 100%;
    display: flex;
    padding: 20px;

    div {
      display: flex;
      width: 85%;
      align-items: center;
    }

    justify-content: space-between;
  }
}

.jc {
  margin-top: 50px;
}

.tabBox {
  height: 300px !important;
}

.el-table {
  height: 300px !important;
  border: 1px solid var(--el-color-primary);
}

.el-form-item__content {
  flex-wrap: nowrap;
}
</style>