<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="card-title">项目简介</span>
        <el-button
          style="float: right"
          type="primary"
          size="small"
          icon="el-icon-edit"
          @click="handleEdit"
          v-hasPermi="['project:intro:edit']"
        >
          编辑
        </el-button>
      </div>
      <div class="project-intro">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="项目名称">{{ form.projectName }}</el-descriptions-item>
          <el-descriptions-item label="实施周期">{{ form.implementationPeriod }}</el-descriptions-item>
          <el-descriptions-item label="牵头单位">{{ form.leadUnit }}</el-descriptions-item>
          <el-descriptions-item label="核心参与单位">{{ form.coreUnits }}</el-descriptions-item>
          <el-descriptions-item label="项目背景" :span="2">{{ form.projectBackground }}</el-descriptions-item>
          <el-descriptions-item label="总体目标" :span="2">{{ form.overallGoal }}</el-descriptions-item>
          <el-descriptions-item label="项目详情" :span="2">
            <div v-html="form.projectDetail"></div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目背景" prop="projectBackground">
          <el-input v-model="form.projectBackground" type="textarea" :rows="3" placeholder="请输入项目背景" />
        </el-form-item>
        <el-form-item label="总体目标" prop="overallGoal">
          <el-input v-model="form.overallGoal" type="textarea" :rows="3" placeholder="请输入总体目标" />
        </el-form-item>
        <el-form-item label="实施周期" prop="implementationPeriod">
          <el-input v-model="form.implementationPeriod" placeholder="请输入实施周期" />
        </el-form-item>
        <el-form-item label="牵头单位" prop="leadUnit">
          <el-input v-model="form.leadUnit" placeholder="请输入牵头单位" />
        </el-form-item>
        <el-form-item label="核心参与单位" prop="coreUnits">
          <el-input v-model="form.coreUnits" type="textarea" :rows="2" placeholder="请输入核心参与单位" />
        </el-form-item>
        <el-form-item label="项目详情" prop="projectDetail">
          <el-input v-model="form.projectDetail" type="textarea" :rows="6" placeholder="请输入项目详情" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getProject, addProject, updateProject } from "@/api/project/info";

export default {
  name: "ProjectIntro",
  data() {
    return {
      loading: false,
      form: {
        id: null,
        projectName: "",
        projectBackground: "",
        overallGoal: "",
        implementationPeriod: "",
        leadUnit: "",
        coreUnits: "",
        projectDetail: ""
      },
      title: "编辑项目简介",
      open: false,
      rules: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        projectBackground: [
          { required: true, message: "项目背景不能为空", trigger: "blur" }
        ],
        overallGoal: [
          { required: true, message: "总体目标不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getDetail();
  },
  methods: {
    getDetail() {
      this.loading = true;
      getProject(1).then(response => {
        if (response.data) {
          this.form = response.data;
        }
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    handleEdit() {
      this.open = true;
      this.title = "编辑项目简介";
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getDetail();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getDetail();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        projectName: "",
        projectBackground: "",
        overallGoal: "",
        implementationPeriod: "",
        leadUnit: "",
        coreUnits: "",
        projectDetail: ""
      };
      this.resetForm("form");
    }
  }
};
</script>

<style scoped>
.card-title {
  font-size: 18px;
  font-weight: bold;
}

.project-intro {
  padding: 20px;
}
</style>
