<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-form ref="viewForm" :model="form" label-width="120px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="项目名称">
                <span class="form-view-text">{{ form.projectName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实施周期">
                <span class="form-view-text">{{ form.implementationPeriod }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="牵头单位">
                <span class="form-view-text">{{ form.leadUnit }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="核心参与单位">
                <span class="form-view-text">{{ form.coreUnits }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="项目背景">
            <span class="form-view-text">{{ form.projectBackground }}</span>
          </el-form-item>
          <el-form-item label="总体目标">
            <span class="form-view-text">{{ form.overallGoal }}</span>
          </el-form-item>
          <el-form-item label="项目详情">
            <div class="form-view-text" v-html="form.projectDetail"></div>
          </el-form-item>
        </el-form>
      </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;
}

.form-view-text {
  color: #606266;
}
</style>
