<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>项目实施进展</span>
            <el-button type="primary" size="small" @click="toggleEdit" v-if="!isEditing">编辑</el-button>
            <el-button type="success" size="small" @click="handleSave" v-if="isEditing">保存</el-button>
          </div>
        </template>
        
        <el-divider></el-divider>
        
        <!-- 标题 -->
        <el-form-item label="标题" prop="title">
          <el-input 
            v-model="form.title" 
            placeholder="请输入标题" 
            :disabled="!isEditing"
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        
        <!-- 详细介绍 -->
        <el-form-item label="详细介绍" prop="content">
          <editor 
            v-model="form.content"
            :disabled="!isEditing"
          ></editor>
        </el-form-item>
      </el-card>
    </el-form>
  </div>
</template>

<script>
import { getProjectProgress, saveProjectProgress } from "@/api/system/projectProgress";
import Editor from "@/components/Editor";

export default {
  components: {
    Editor
  },
  data() {
    return {
      isEditing: false,
      form: {
        id: null,
        title: "",
        content: "",
        createBy: "",
        createTime: "",
        updateBy: "",
        updateTime: "",
        remark: ""
      },
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          { max: 50, message: "标题长度不能超过50个字符", trigger: "blur" }
        ],
        content: [
          { required: true, message: "请输入详细介绍", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    getInfo() {
      getProjectProgress().then(response => {
        if (response.data) {
          this.form = response.data;
        }
      });
    },
    toggleEdit() {
      this.isEditing = true;
    },
    handleSave() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveData = {
            id: this.form.id,
            title: this.form.title,
            content: btoa(unescape(encodeURIComponent(this.form.content || '')))
          };
          saveProjectProgress(saveData).then(response => {
            if (response.code === 200) {
              this.$modal.msgSuccess("保存成功");
              this.isEditing = false;
              this.getInfo();
            } else {
              this.$modal.msgError(response.msg);
            }
          });
        }
      });
    }
  }
};
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
