<template>
  <div>
    <MOSTY.FromPage :title="props.title" @closeDialog="handleClose">
      <template #content>
        <div class="config">
          <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="130px" class="demo-ruleForm.value"
            :="true" :disabled="!props.saveBtnShow">
            <el-form-item label="证书类别：" prop="zslx">
              <MOSTY.DictTree v-model="ruleForm.zslx" :zdbh="'D_TB_XGFZSLB'" style="width: 337px" @onChange="getxzqh" />
            </el-form-item>
            <el-form-item label="证书名称：" prop="name">
              <el-input v-model="ruleForm.name" maxlength="50" autosize type="textarea" show-word-limit
                placeholder="不能超过50个字" style="width: 337px"></el-input>
            </el-form-item>
            <el-form-item label="证书编号：" prop="zsbh">
              <el-input v-model="ruleForm.zsbh" clearable style="width: 337px"></el-input>
            </el-form-item>
            <el-form-item label="颁发单位：" prop="bfdw">
              <el-input v-model="ruleForm.bfdw" maxlength="50" autosize type="textarea" show-word-limit
                placeholder="不能超过50个字" style="width: 337px"></el-input>
            </el-form-item>
            <el-form-item label="证书有效期:" >
              <el-select class="pxdx_box" v-model="zsyxqDate" clearable style="width: 280px">
                <el-option label="非长期有效" :value="true" />
                <el-option label="长期有效" :value="false" />
              </el-select>
            </el-form-item>
            <el-form-item label="发证日期始：" prop="startDate">
              <el-date-picker style="width: 337px" v-model="ruleForm.startDate" type="date" format="YYYY-MM-DD"
                value-format="YYYY-MM-DD" />
            </el-form-item>
            <el-form-item label="发证日期止：" v-if="zsyxqDate" prop="endDate">
              <el-date-picker style="width: 337px" v-model="ruleForm.endDate" type="date" format="YYYY-MM-DD"
                value-format="YYYY-MM-DD" :disabled-date="disablednowDate" />
            </el-form-item>

            <el-form-item label="备注：" style="width: 100%">
              <el-input v-model="ruleForm.bz" maxlength="1000" type="textarea" show-word-limit placeholder="不能超过1000个字"
                style="width: 100%" />
            </el-form-item>
            <el-form-item label="上传附件：" style="width: 100%">
              <MOSTY.Upload :isAutoWidth='true' :limit="1" :nopicer="true" :isImg="false" :ShowfileList="true"
                :gsgfList="['pdf', 'ofd', 'doc', 'docx', 'xls', 'xlsx', 'wps']" v-model="ruleForm.files"
                :showinput="true" :delrow="delrow"></MOSTY.Upload>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>

        <el-button type="primary" v-if="props.saveBtnShow" :loading="loading" @click="handleSave(ruleFormRef)">
          <el-icon>
            <DocumentChecked />
          </el-icon>
          <span>保存</span>
        </el-button>
        <el-button @click="handleClose" type="primary">
          <el-icon>
            <DocumentDelete />
          </el-icon>
          <span>关闭</span>
        </el-button>
      </template>
    </MOSTY.FromPage>


  </div>
</template>

<script setup>
// 引入
import {
  ref,
  onMounted,
  defineProps,
  defineEmits,
  reactive,
  watch,
  getCurrentInstance
} from "vue";
import { getItem } from "@/utils/storage";
import { useRouter } from "vue-router";
import * as rule from "@/utils/rules.js";
import { createAndClickLink } from "@/utils/tools.js";
import { getApi, postApi, putApi } from "@/api/tobAcco_api.js";
import { ElMessage } from "element-plus";
import * as MOSTY from "@/components/MyComponents/index";
const { proxy } = getCurrentInstance();
const { D_TB_XGFZSLB, D_BZ_XB, D_BZ_AQRYWHCD } = proxy.$dict(
  "D_TB_XGFZSLB",
  "D_BZ_XB",
  "D_BZ_AQRYWHCD"
);
const loading = ref(false); //新增保存按钮截流
const ruleFormRef = ref();
const ruleForm = ref({});
const emits = defineEmits(["changeAddVi", "changeclose"]);
// 证书有效期
const zsyxqDate = ref()
//规则校验
const rules = reactive({
  name: [{ required: true, message: "证书名称不能为空", trigger: "blur" }],
  zsbh: [{ required: true, message: "证书编号不能为空", trigger: "blur" }],
  bfdw: [
    { trigger: "blur", required: true, message: "颁发单位不能为空" }
  ],
  zslx: [{ trigger: "blur", required: true, message: "证书类别不能为空" }],

  startDate: [
    { trigger: "blur", required: true, message: "发证日期始不能为空" }
  ],
  endDate: [
    { trigger: "blur", required: true, message: "发证日期止不能为空" }
  ],

});
const props = defineProps({
  saveBtnShow:Boolean,
  relatedUserId:String,
  title: String,
  id: String
});
function disablednowDate(time) {
  return time.getTime() + 24 * 60 * 60 * 1000 < new Date(ruleForm.value.startDate).getTime()
}
//获取详情
onMounted(() => {
  if (props.id) {
    geixqData(props.id)
  } else {
    ruleForm.value = {
      relatedUserId:props.relatedUserId
    };
  }
});
//获取证书类别
function getxzqh(val) {
  console.log(val,'val');
  ruleForm.value.name=val.label
}
function geixqData(id) {
  getApi({},`/mosty-jcgl/relatedUserAtta/getInfo?id=${id}`).then(res=>{
    ruleForm.value=res
    if(ruleForm.value.endDate==='2099-12-31') zsyxqDate.value=false
  })
}
// 关闭
const handleClose = () => {
  if (props.saveBtnShow) {
    proxy.$modal.confirm('离开会丢失页面中修改的内容，确认离开吗？')
      .then(() => {
        // 关闭上一次的表单校验
        ruleFormRef.value.clearValidate();
        ruleForm.value = {};
        emits("changeAddVi", false);
      })
      .catch(() => {
        // catch error
      })
  } else {
    // 关闭上一次的表单校验
    ruleFormRef.value.clearValidate();
    ruleForm.value = {};
    emits("changeAddVi", false);
  }

};

// 关闭×
const closeDialog = () => {
  loading.value = false;
  emits("changeAddVi", false);
  emits("changeclose", false);
};
// 保存
const handleSave = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true;
      ruleForm.value.files = ruleForm.value.files
        ? ruleForm.value.files.toString()
        : null;
if(zsyxqDate.value===false){
// ruleForm.value.startDate='1900-01-01'
ruleForm.value.endDate='2099-12-31'
        }
      if (!ruleForm.value.id) {
        postApi(ruleForm.value, "/mosty-jcgl/relatedUserAtta/add").then((res) => {
          ruleForm.value = {}
          ElMessage.success("新增成功");
          emits("changeAddVi", false);
        }).finally(() => {
          loading.value = false;
        });
      } else {
        postApi(ruleForm.value, "/mosty-jcgl/relatedUserAtta/edit").then((res) => {
          loading.value = false;
          ruleForm.value = {}
          ElMessage.success("修改成功");
          emits("changeAddVi", false);
        }).finally(() => {
          loading.value = false;
        });
      }
    } else {
      ElMessage.warning("请输入必填项");
    }
  });
};
</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 {
  margin-top: 20px;

  .el-form {
    width: 100%;
    padding: 10px;
    margin: 10px auto;
    flex-wrap: wrap;
    justify-content: space-between;
  }

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

.pxdx_box {
  width: 500px;
}

::v-deep .el-dialog__body {
  overflow: auto !important;
  max-height: 540px !important;
}

.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;
}

.el-collapse {
  border-color: var(--el-color-primary);
  // height: 460px;
}

::v-deep .el-collapse-item__header {
  border-bottom-color: var(--el-color-primary);
  color: #000 !important;
}

::v-deep .el-collapse-item__header.is-active {
  border-bottom-color: transparent;
}

::v-deep .el-date-editor .el-range-input {
  color: #0c0b0b !important;
}

::v-deep .el-range-editor.is-disabled input {
  background-color: #d3dbdd !important;
}

::v-deep .el-range-editor.is-disabled {
  background-color: #d3dbdd !important;
}
</style>
