<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="事件名称" prop="eventName">
        <el-input
          v-model="queryParams.eventName"
          placeholder="请输入事件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事件时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <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="['progress:milestone:add']"
        >新增里程碑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :type="viewType === 'timeline' ? 'success' : 'info'"
          plain
          icon="el-icon-time"
          size="mini"
          @click="viewType = 'timeline'"
        >时间轴</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :type="viewType === 'list' ? 'success' : 'info'"
          plain
          icon="el-icon-s-grid"
          size="mini"
          @click="viewType = 'list'"
        >列表</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div v-loading="loading">
      <div v-if="viewType === 'timeline'" class="timeline-container">
        <el-timeline>
          <el-timeline-item
            v-for="(item, index) in milestoneList"
            :key="item.id"
            :timestamp="parseTime(item.eventTime, '{y}-{m}-{d}')"
            placement="top"
            :color="index === 0 ? '#409EFF' : '#909399'"
            :size="index === 0 ? 'large' : 'normal'"
          >
            <el-card class="timeline-card">
              <div class="timeline-header">
                <h3>{{ item.eventName }}</h3>
                <div class="timeline-actions">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(item)"
                    v-hasPermi="['progress:milestone:edit']"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(item)"
                    v-hasPermi="['progress:milestone:remove']"
                  >删除</el-button>
                </div>
              </div>
              <div class="timeline-time">事件时间：{{ parseTime(item.eventTime) }}</div>
              <div class="timeline-desc" v-html="item.eventDescription"></div>
              <div class="timeline-footer">
                <span>创建人：{{ item.createBy }}</span>
                <span>创建时间：{{ parseTime(item.createTime) }}</span>
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>

      <el-table v-else :data="milestoneList">
        <el-table-column label="序号" align="center" prop="id" width="80" />
        <el-table-column
          label="事件名称"
          align="center"
          prop="eventName"
          :show-overflow-tooltip="true"
          min-width="150"
        />
        <el-table-column label="事件时间" align="center" prop="eventTime" width="160">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.eventTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="事件描述" align="center" prop="eventDescription" :show-overflow-tooltip="true" min-width="200" />
        <el-table-column label="创建人" align="center" prop="createBy" width="100" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="160">
          <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="['progress:milestone:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['progress:milestone:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0 && viewType === 'list'"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="事件名称" prop="eventName">
              <el-input v-model="form.eventName" placeholder="请输入事件名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="事件时间" prop="eventTime">
              <el-date-picker
                v-model="form.eventTime"
                type="datetime"
                placeholder="选择事件时间"
                value-format="yyyy-MM-dd HH:mm:ss"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio label="0">正常</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="事件描述" prop="eventDescription">
              <editor v-model="form.eventDescription" :min-height="300"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :rows="2" />
            </el-form-item>
          </el-col>
        </el-row>
      </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 { listMilestone, getMilestone, delMilestone, addMilestone, updateMilestone } from "@/api/milestone"

export default {
  name: "Milestone",
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      milestoneList: [],
      viewType: 'timeline',
      title: "",
      open: false,
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        eventName: undefined,
        status: undefined
      },
      form: {},
      rules: {
        eventName: [
          { required: true, message: "事件名称不能为空", trigger: "blur" }
        ],
        eventTime: [
          { required: true, message: "事件时间不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryParams.params['beginTime'] = this.dateRange[0]
        this.queryParams.params['endTime'] = this.dateRange[1]
      }
      listMilestone(this.queryParams).then(response => {
        this.milestoneList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: undefined,
        eventName: undefined,
        eventTime: undefined,
        eventDescription: undefined,
        status: "0",
        remark: undefined
      }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "新增里程碑事件"
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getMilestone(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改里程碑事件"
      })
    },
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMilestone(this.form).then(() => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addMilestone(this.form).then(() => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除里程碑事件编号为"' + id + '"的数据项？').then(function() {
        return delMilestone(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.timeline-container {
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.timeline-card {
  margin-bottom: 10px;
}

.timeline-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.timeline-header h3 {
  margin: 0;
  font-size: 16px;
  color: #303133;
}

.timeline-actions {
  display: flex;
  gap: 5px;
}

.timeline-time {
  color: #909399;
  font-size: 14px;
  margin-bottom: 10px;
}

.timeline-desc {
  color: #606266;
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 10px;
  max-height: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.timeline-footer {
  display: flex;
  justify-content: space-between;
  color: #909399;
  font-size: 12px;
  border-top: 1px solid #ebeef5;
  padding-top: 10px;
}
</style>
