<template>
  <div class="ResidentMainBox p-2 w-full">
    <PageHeader title="活动"></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="titile">
                    <el-input v-model.trim="form.titile" placeholder="请输入活动标题" />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="作者" prop="author">
                    <el-input v-model.trim="form.author" placeholder="请输入作者" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label="活动详情" prop="content">
                    <editor v-model="form.content" :height="500" :min-height="300" :read-only="false" :file-size="5" :type="'url'" />
                  </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 { addActivity, getActivity, updateActivity } from '@/api/system/Activity';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_repair_project_status } = toRefs<any>(proxy?.useDict('com_repair_project_status'));

const route = useRoute();
const router = useRouter();
const formRef = ref();
const form = ref({
  'id': null,
  'titile': '',
  'author': '',
  'content': '',
  'status': '0'
});
const rules = {
  titile: [{ required: true, message: '请输入活动标题', trigger: 'blur' }],
  author: [{ required: true, message: '请输入作者', trigger: 'blur' }],
  content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
};
const userid = ref();

//  ! 投诉 ====================================================================================================
function init() {
  getActivity(userid.value).then((res) => {
    form.value = {
      ...form.value,
      ...res.data
    };
    console.log(form.value);
  });
}

if (route.query.id) {
  userid.value = route.query.id;
  init();
}

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

//  返回
function handleBack() {
  router.push({
    path: '/repair/Activity'
  });
}
</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>
