
<template>
  <BaseDialog
    :dialogVisible="props.visible"
    @close="onclone"
    :titleName="props.form.opInstructionId ? '编辑应急队伍' : '新增应急队伍'"
    width="50%"
    @onSubmit="handleSubmit"
    :footerclosed="true"
    :diafooter="true"
  >
    <div class="detailForm-content">
      <el-form
        ref="formRef"
        label-position="right"
        label-width="150px"
        :rules="ruleForm"
        :model="addPostFactorForm"
      >
        <el-form-item prop="teamname" label="队伍名称">
          <el-input
            :disabled="readonly"
            v-model="addPostFactorForm.teamname"
            placeholder="请输入队伍名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="orgcode" label="所属区域">
          <el-cascader
            ref="cascader"
            v-model="addPostFactorForm.orgcode"
            placeholder="请选择地区"
            :options="orgList"
            :props="{
              checkStrictly: true,
              label: 'orgname',
              value: 'orgcode',
            }"
            clearable
          ></el-cascader>
        </el-form-item>
       
        <el-form-item prop="chkbillname" label="负责人">
          <el-input
            placeholder="请输入负责人"
            v-model="addPostFactorForm.principal"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="chkbillname"  label="负责人电话">
          <el-input
            placeholder="请输入负责人电话"
            v-model="addPostFactorForm.principaltel"
          >
          </el-input>
        </el-form-item>
        <el-form-item prop="personnum" label="人数">
          <el-input-number
            v-model="addPostFactorForm.personnum"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item prop="chkbillname" style="width: 100%;" label="队伍简介">
          <el-input
            placeholder="请输入队伍简介"
            type="textarea"
            :rows="3"
            v-model="addPostFactorForm.introduction"
          >
          </el-input>
        </el-form-item>
      </el-form>
    </div>
  </BaseDialog>
</template>
<script setup lang="ts" >
import { reactive, ref, onMounted, watch } from "vue";
import useUserStore from "@/store/modules/user";
import { ElMessage, FormInstance, FormRules } from "element-plus";
import { getYears, getUUID } from "@/utils/common";
import { areaTree } from "@/api/account";
import { saveOthteam } from "@/api/Othteam";

const formRef = ref<FormInstance>();
const userStore = useUserStore();
const user = ref(JSON.parse(userStore.userInfo));
const userId = ref(user.value.userId);
const planId = ref("");
const indexClassify = ref([]);
const fileList = ref<any>([]);
const ruleForm = reactive<FormRules>({
  teamname: [{ required: true, message: "请输入队伍名称", trigger: "change" }],
  orgcode: [{ required: true, message: "请选择所属区域", trigger: "blur" }],
  personnum: [{ required: true, message: "请输入人数", trigger: "change" }],
});
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  form: Object,
  readonly: {
    type: Boolean,
    default: false,
  },
  id: String,
});
const listperformid = ref(props.id);
const addPostFactorForm = ref(<any>{});
const emits = defineEmits(["close", "onSubmit"]);
//保存并退出
const handleSubmit = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
        addPostFactorForm.value.orgcode = addPostFactorForm.value.orgcode[addPostFactorForm.value.orgcode.length - 1];
      addPostFactorForm.value.othteamid = getUUID();
      addPostFactorForm.value.userId = userId.value;
      saveOthteam(addPostFactorForm.value).then((res: any) => {
        if (res.code == 1) {
          ElMessage.success({
            message: "保存成功",
            type: "success",
          });
          onclone();
        }
      });
    }
    return false;
  });
};
//关闭弹窗
const onclone = () => {
  emits("close");
};
//获取地区
const orgList = ref([]);
const getareaTree = () => {
  areaTree(userId.value).then((res: any) => {
    orgList.value = res.data;
  });
};
onMounted(() => {
  getareaTree();
});
watch(
  () => props.form,
  (val) => {
    console.log(333, props);
    if (val) {
      if (!props.visible) return;
      addPostFactorForm.value = val;
    }
  }
);
</script>

<style lang="scss" scoped>
.detailForm-content {
    margin-right: 30px;
}
:deep(.el-form) {
    display: flex;
    flex-wrap: wrap;
    // justify-content: space-around;
    justify-content: space-between;
}
:deep(.el-form-item) {
    width: 47%;
}
</style>