<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前状态" prop="currentStatus">
        <el-select v-model="queryParams.currentStatus" placeholder="请选择当前状态" clearable>
          <el-option label="正常" value="0" />
          <el-option label="延迟" value="1" />
          <el-option label="风险" value="2" />
        </el-select>
      </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="['project:progress:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:progress:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="progressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="center" prop="projectName" :show-overflow-tooltip="true" />
      <el-table-column label="当前状态" align="center" prop="currentStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.currentStatus === '0'" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.currentStatus === '1'" type="warning">延迟</el-tag>
          <el-tag v-else-if="scope.row.currentStatus === '2'" type="danger">风险</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="目前成果" align="center" prop="currentAchievement" :show-overflow-tooltip="true" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['project:progress:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['project:progress:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:progress:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :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="120px">
        <el-form-item label="项目名称" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择项目" filterable clearable style="width: 100%">
            <el-option
              v-for="project in projectList"
              :key="project.id"
              :label="project.projectName"
              :value="project.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="当前状态" prop="currentStatus">
          <el-select v-model="form.currentStatus" placeholder="请选择当前状态">
            <el-option label="正常" value="0" />
            <el-option label="延迟" value="1" />
            <el-option label="风险" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="目前成果" prop="currentAchievement">
          <el-input v-model="form.currentAchievement" type="textarea" :rows="3" placeholder="请输入目前成果" />
        </el-form-item>
        <el-form-item label="进展详情" prop="progressDetail">
          <el-input v-model="form.progressDetail" 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>

    <el-dialog title="进展详情" :visible.sync="viewOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="项目名称">{{ viewForm.projectName }}</el-descriptions-item>
        <el-descriptions-item label="当前状态">
          <el-tag v-if="viewForm.currentStatus === '0'" type="success">正常</el-tag>
          <el-tag v-else-if="viewForm.currentStatus === '1'" type="warning">延迟</el-tag>
          <el-tag v-else-if="viewForm.currentStatus === '2'" type="danger">风险</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="目前成果" :span="2">{{ viewForm.currentAchievement }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ parseTime(viewForm.updateTime) }}</el-descriptions-item>
        <el-descriptions-item label="更新人">{{ viewForm.updateBy }}</el-descriptions-item>
        <el-descriptions-item label="进展详情" :span="2">
          <div v-html="viewForm.progressDetail"></div>
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { listProgress, getProgress, delProgress, addProgress, updateProgress } from "@/api/project/progress";
import { listProject } from "@/api/project/info";

export default {
  name: "ProjectProgress",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      progressList: [],
      projectList: [],
      title: "",
      open: false,
      viewOpen: false,
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        currentStatus: null
      },
      form: {},
      viewForm: {},
      rules: {
        projectId: [
          { required: true, message: "项目名称不能为空", trigger: "change" }
        ],
        currentStatus: [
          { required: true, message: "当前状态不能为空", trigger: "change" }
        ],
        currentAchievement: [
          { required: true, message: "目前成果不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getProjectList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && '' != this.dateRange) {
        this.queryParams.params["beginTime"] = this.dateRange[0];
        this.queryParams.params["endTime"] = this.dateRange[1];
      }
      listProgress(this.queryParams).then(response => {
        this.progressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getProjectList() {
      listProject({ pageNum: 1, pageSize: 1000 }).then(response => {
        this.projectList = response.rows;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加进展记录";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProgress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改进展记录";
      });
    },
    handleView(row) {
      this.viewForm = row;
      this.viewOpen = true;
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const selectedProject = this.projectList.find(p => p.id === this.form.projectId);
          if (selectedProject) {
            this.form.projectName = selectedProject.projectName;
          }
          if (this.form.id != null) {
            updateProgress(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProgress(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除进展记录编号为"' + ids + '"的数据项？').then(function() {
        return delProgress(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        projectId: null,
        projectName: null,
        currentStatus: null,
        currentAchievement: null,
        progressDetail: null
      };
      this.resetForm("form");
    }
  }
};
</script>
