<template>
  <div class="ResidentMainBox p-2 w-full">
    <PageHeader></PageHeader>
    <div class="ResidentMainBody">
      <el-card>
        <template #header>
          <div class="title">基本信息</div>
        </template>
        <div class="addBuildingFormBody">
          <div class="formbox">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="房屋" prop="houseId">
                    <el-cascader @change="handleChange" v-model="userHousepath" :options="treedata" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请人" prop="applyName">
                    <el-input v-model.trim="form.applyName" placeholder="请输入申请人" />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row>
                <el-col :span="12">
                  <el-form-item label="装修公司" prop="decorationCompany">
                    <el-input v-model.trim="form.decorationCompany" placeholder="请输入装修公司" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="装修内容" prop="decorationContent">
                    <el-input v-model.trim="form.decorationContent" placeholder="请输入装修内容" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="12">
                  <el-form-item label="装修押金(元)" prop="decorationDeposit">
                    <el-input v-model.trim="form.decorationDeposit" placeholder="请输入装修押金" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item>
                <el-button type="primary" @click="submitForm">提交</el-button>
                <el-button @click="handleBack">返回</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import PageHeader from '@/components/Pageheader/index.vue';
import { addDecoration, getDecoration, updateDecoration } from '@/api/system/Decoration';
import { useHouseStore } from '@/store/modules/house';
import { CascaderValue } from 'element-plus';
import { getHousePathById } from '@/utils/house';

const houseStore = useHouseStore();
const treedata = ref([]);

const route = useRoute();
const router = useRouter();
const formRef = ref();
const form = ref({
  'id': null,
  'houseId': null,
  'applyName': '',
  'decorationCompany': '',
  'decorationContent': '',
  'decorationDeposit': null
});
const rules = {
  houseId: [{ required: true, message: '请选择具体房屋', trigger: 'blur' }],
  applyName: [{ required: true, message: '请输入申请人', trigger: 'blur' }],
  decorationDeposit: [{ required: true, message: '请输入装修押金', trigger: 'blur' }],
  decorationContent: [{ required: true, message: '请输入装修内容', trigger: 'blur' }]
};
const userid = ref();

// 获取房屋
const userHousepath = ref([]);
function handleChange(val: (string | number)[]) {
  if (val.length >= 3) {
    form.value.houseId = val.pop() as number;
  } else {
    form.value.houseId = null;
  }
}

//  ! 投诉 ====================================================================================================
function getTree() {
  houseStore.getCurrentVilageTreeHouse().then((res) => {
    treedata.value = res;
    if (route.query.id) {
      userid.value = route.query.id;
      init();
    }
  });
}
getTree();
function init() {
  getDecoration(userid.value).then((res) => {
    form.value = {
      ...form.value,
      ...res.data
    };
    const arr = getHousePathById(treedata.value, form.value.houseId);
    userHousepath.value = arr;
  });
}

// 提交
const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      if (userid.value) {
        updateDecoration(form.value).then(() => {
          ElMessage.success('编辑成功');
          handleBack();
        });
      } else {
        addDecoration(form.value).then(() => {
          ElMessage.success('添加成功');
          handleBack();
        });
      }
    }
  });
};

//  返回
function handleBack() {
  router.push({
    path: '/repair/decoration'
  });
}
</script>

<style scoped lang="scss">
.ResidentMainBox {
  display: flex;
  flex-direction: column;
  .ResidentMainBody {
    margin-top: 20px;
    flex: 1;

    .formbox {
      width: 1000px;
      margin: 0 auto;
    }
    &:deep(div.el-step__head.is-process) {
      & > div.el-step__line {
        width: 0;
        border: 1px dashed gray;
        background: transparent;
      }
    }
    &:deep(.el-step.is-vertical .el-step__line) {
      top: 20px;
    }
    &:deep(div.el-step__head.is-finish) {
      & > div.el-step__line {
        width: 0;
        border: 1px solid var(--el-color-primary);
        background: transparent;
      }
    }
  }
}
</style>
