<template>
  <div class="app-container">
    <el-card v-loading="loading">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:sciencePlan:add']"
          >新增</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="list" border>
        <el-table-column label="序号" type="index" align="center" width="55" />
        <el-table-column label="排序" align="center" prop="sortOrder" width="80" />
        <el-table-column label="标题" align="center" prop="title" show-overflow-tooltip />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:sciencePlan:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:sciencePlan:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排序" prop="sortOrder">
          <el-input-number v-model="form.sortOrder" :min="0" controls-position="right" style="width: 100%" />
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="300" />
        </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 { listSciencePlan, getSciencePlan, addSciencePlan, updateSciencePlan, delSciencePlan } from "@/api/system/sciencePlan"
import Editor from "@/components/Editor"

export default {
  name: "SciencePlan",
  components: {
    Editor
  },
  data() {
    return {
      loading: true,
      list: [],
      title: "",
      open: false,
      form: {
        id: undefined,
        title: '',
        content: '',
        sortOrder: 0
      },
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listSciencePlan().then(response => {
        this.list = response.data || []
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.resetForm()
    },
    handleAdd() {
      this.resetForm()
      this.open = true
      this.title = "添加科研计划与国际合作"
    },
    handleUpdate(row) {
      this.resetForm()
      const id = row.id || this.ids
      getSciencePlan(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改科研计划与国际合作"
      })
    },
    handleDelete(row) {
      this.$modal.confirm('是否确认删除标题为"' + row.title + '"的数据项？').then(() => {
        return delSciencePlan(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateSciencePlan(this.form).then(() => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSciencePlan(this.form).then(() => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    resetForm() {
      this.form = {
        id: undefined,
        title: '',
        content: '',
        sortOrder: 0
      }
      if (this.$refs.form) {
        this.$refs.form.resetFields()
      }
    }
  }
}
</script>
