<template>
  <div class="AddBuildingBox">
    <PageHeader></PageHeader>
    <div class="AddBuildingBody">
      <div class="title">基本信息</div>
      <div class="addBuildingFormBody">
        <el-form class="formBody" label-position="right" ref="formRef" :model="form" :rules="rules" label-width="130px">
          <el-row>
            <el-col :span="11">
              <el-form-item label="维修项目名" prop="projectName">
                <el-input v-model.trim="form.projectName" placeholder="请输入维修项目名" />
              </el-form-item>
            </el-col>
            <el-col :span="2"></el-col>
            <el-col :span="11">
              <el-form-item label="项目类型" prop="projectType">
                <el-radio-group :disabled="form.projectLevel === 1" v-model="form.projectType">
                  <el-radio v-for="item in com_repair_project_types" :key="item.id" :value="item.value">{{ item.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="11">
              <el-form-item label="项目层级" prop="projectLevel">
                <el-radio-group v-model="form.projectLevel">
                  <el-radio v-for="item in com_repair_project_level" :key="item.id" :value="Number(item.value)">{{ item.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="2"></el-col>
            <el-col :span="11">
              <el-form-item label="项目状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio v-for="item in com_repair_project_status" :key="item.id" :value="item.value">{{ item.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="form.projectLevel === 1">
            <el-col :span="11">
              <el-form-item label="隶属父级" prop="email">
                <el-select @change="handleChange" v-model="form.parentId" placeholder="请选择隶属父级" style="width: 240px">
                  <el-option v-for="item in options" :key="item.id" :label="item.projectName" :value="item.id" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="cancelForm">返回</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import PageHeader from '@/components/Pageheader/index.vue';
import { addRepairProject, getRepairProject, listRepairProject, updateRepairProject } from '@/api/system/RepairProject';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_repair_project_types } = toRefs<any>(proxy?.useDict('com_repair_project_types'));
const { com_repair_project_status } = toRefs<any>(proxy?.useDict('com_repair_project_status'));
const { com_repair_project_level } = toRefs<any>(proxy?.useDict('com_repair_project_level'));

const router = useRouter();
const route = useRoute();

const formRef = ref();
const form = ref({
  id: null,
  projectName: null, // 项目名称
  parentId: '', // 父级项目id
  projectType: '0', // 项目类型
  projectLevel: 0, //项目层级
  status: '0' //状态
});
const rules = ref({
  projectName: [{ required: true, message: '请选择房屋', trigger: 'blur' }],
  parentId: [{ required: true, message: '请选择房屋', trigger: 'blur' }],
  projectType: [{ required: true, message: '请选择房屋', trigger: 'blur' }],
  projectLevel: [{ required: true, message: '请选择房屋', trigger: 'blur' }],
  status: [{ required: true, message: '请输入名称', trigger: 'blur' }]
});
const userid = ref(null);
const options = ref([]);

function handleChange(val) {
  if (!val) return;
  const find = options.value.find((item) => item.id === val);
  if (find) {
    form.value.projectType = find.projectType;
  }
}
// != ==========================================

function init() {
  form.value = {
    id: null,
    projectName: null, // 项目名称
    parentId: '', // 父级项目id
    projectType: '0', // 项目类型
    projectLevel: 0, //项目层级
    status: '0' //状态
  };
  listRepairProject().then((res) => {
    const arr = res.rows.filter((ite) => ite.projectLevel === 0);
    options.value = arr;
  });
  if (route.query.id) {
    userid.value = route.query.id;
    getRepairProject(userid.value).then((res) => {
      form.value = res.data;
      form.value.status = res.data.status ?? '0' + '';
      handleChange(res.data.parentId);
    });
  }
}
init();
// !== 提交 =============================================================================
// 提交
const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      if (!userid.value) {
        addRepairProject(form.value).then(() => {
          ElMessage.success('添加成功');
          cancelForm();
        });
      } else {
        updateRepairProject(form.value).then(() => {
          ElMessage.success('编辑成功');
          cancelForm();
        });
      }
    }
  });
};
// 推出
const cancelForm = () => {
  router.back();
};
</script>

<style scoped lang="scss">
.AddBuildingBox {
  padding: 15px;
  height: 100%;
  width: 100%;
}
.AddBuildingBody {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: white;
  padding: 15px;
  .title {
    height: 40px;
    line-height: 40px;
    padding-left: 20px;
    background-color: rgba(255, 255, 255, 1);
    color: rgba(16, 16, 16, 1);
    border-bottom: 1px solid #bbbbbb;
    margin-bottom: 60px;
  }

  .formBody {
    width: 1050px;
    margin: 0 auto;
    &:deep(.el-form-item__content, .el-select, .el-input) {
      width: 350px !important;
      margin-right: 10px;
      .el-cascader {
        width: 100%;
      }
    }
    .el-form-item {
      margin-bottom: 20px;
      align-items: center;
    }
    .el-button {
      width: 80px;
      height: 35px;
      margin-right: 20px;
    }
  }
}
</style>
