<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <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="请输入标题" 
            maxlength="50"
            :disabled="!isEditing"
          ></el-input>
        </el-form-item>
        
        <!-- 详细介绍 -->
        <el-form-item label="详细介绍" prop="content">
          <editor 
            v-model="form.content"
            :disabled="!isEditing"
          ></editor>
        </el-form-item>
        
        <el-divider></el-divider>
        
        <!-- 备注 -->
        <el-form-item label="备注" prop="remark">
          <el-input 
            v-model="form.remark" 
            type="textarea"
            :rows="3"
            placeholder="请输入备注"
            :disabled="!isEditing"
          ></el-input>
        </el-form-item>
      </el-card>
    </el-form>
  </div>
</template>

<script>
import { getImplementationProgress, saveImplementationProgress } from "@/api/system/implementationProgress";
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" }
        ],
        content: [
          { required: true, message: "请输入详细介绍", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getInfo();
  },
  methods: {
    // 获取项目实施进展信息
    getInfo() {
      getImplementationProgress().then(response => {
        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 || ''))),
            remark: this.form.remark
          };
          saveImplementationProgress(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>