<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评估风险等级" prop="assessmentLevel">
        <el-select v-model="queryParams.assessmentLevel" placeholder="请选择评估风险等级" clearable size="small">
          <el-option
            v-for="dict in assessmentLevelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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="['system:assessment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:assessment:edit']"
        >修改</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="['system:assessment:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:assessment:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assessmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="评估风险等级" align="center" prop="assessmentId" />
      <el-table-column label="排查任务id" align="center" prop="workId" />
      <el-table-column label="排查反馈内容" align="center" prop="checkFeedback" />
      <el-table-column label="现场图片或视频" align="center" prop="checkUrl" />
      <el-table-column label="排查时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排查人" align="center" prop="checkUser" />
      <el-table-column label="评估内容" align="center" prop="assessmentContent" />
      <el-table-column label="评估时间" align="center" prop="assessmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assessmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="评估人" align="center" prop="assessmentUser" />
      <el-table-column label="评估风险等级" align="center" prop="assessmentLevel" :formatter="assessmentLevelFormat" />
      <el-table-column label="是否需要整改 0否 1是" align="center" prop="isModify" />
      <el-table-column label="整改意见" align="center" prop="modifyOpinion" />
      <el-table-column label="整改人" align="center" prop="modifyPeople" />
      <el-table-column label="整改人电话" align="center" prop="modifyPhone" />
      <el-table-column label="整改期限" align="center" prop="modifyTerm" />
      <el-table-column label="整改开始时间" align="center" prop="modifyBegin" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyBegin, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="整改结束时间" align="center" prop="modifyEnd" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="整改是否完成 0否 1是" align="center" prop="modifyComplete" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:assessment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:assessment: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="排查任务id" prop="workId">
          <el-input v-model="form.workId" placeholder="请输入排查任务id" />
        </el-form-item>
        <el-form-item label="排查反馈内容" prop="checkFeedback">
          <el-input v-model="form.checkFeedback" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="现场图片或视频" prop="checkUrl">
          <el-input v-model="form.checkUrl" placeholder="请输入现场图片或视频" />
        </el-form-item>
        <el-form-item label="排查时间" prop="checkTime">
          <el-date-picker clearable size="small"
            v-model="form.checkTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择排查时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="排查人" prop="checkUser">
          <el-input v-model="form.checkUser" placeholder="请输入排查人" />
        </el-form-item>
        <el-form-item label="评估内容">
          <editor v-model="form.assessmentContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="评估时间" prop="assessmentTime">
          <el-date-picker clearable size="small"
            v-model="form.assessmentTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择评估时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评估人" prop="assessmentUser">
          <el-input v-model="form.assessmentUser" placeholder="请输入评估人" />
        </el-form-item>
        <el-form-item label="评估风险等级" prop="assessmentLevel">
          <el-select v-model="form.assessmentLevel" placeholder="请选择评估风险等级">
            <el-option
              v-for="dict in assessmentLevelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否需要整改 0否 1是" prop="isModify">
          <el-input v-model="form.isModify" placeholder="请输入是否需要整改 0否 1是" />
        </el-form-item>
        <el-form-item label="整改意见" prop="modifyOpinion">
          <el-input v-model="form.modifyOpinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="整改人" prop="modifyPeople">
          <el-input v-model="form.modifyPeople" placeholder="请输入整改人" />
        </el-form-item>
        <el-form-item label="整改人电话" prop="modifyPhone">
          <el-input v-model="form.modifyPhone" placeholder="请输入整改人电话" />
        </el-form-item>
        <el-form-item label="整改期限" prop="modifyTerm">
          <el-input v-model="form.modifyTerm" placeholder="请输入整改期限" />
        </el-form-item>
        <el-form-item label="整改开始时间" prop="modifyBegin">
          <el-date-picker clearable size="small"
            v-model="form.modifyBegin"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择整改开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="整改结束时间" prop="modifyEnd">
          <el-date-picker clearable size="small"
            v-model="form.modifyEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择整改结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="整改是否完成 0否 1是" prop="modifyComplete">
          <el-input v-model="form.modifyComplete" placeholder="请输入整改是否完成 0否 1是" />
        </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 { listAssessment, getAssessment, delAssessment, addAssessment, updateAssessment, exportAssessment } from "@/api/system/assessment";
import Editor from '@/components/Editor';

export default {
  name: "Assessment",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排查评估表格数据
      assessmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 评估风险等级字典
      assessmentLevelOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assessmentLevel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("assessment_level").then(response => {
      this.assessmentLevelOptions = response.data;
    });
  },
  methods: {
    /** 查询排查评估列表 */
    getList() {
      this.loading = true;
      listAssessment(this.queryParams).then(response => {
        this.assessmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 评估风险等级字典翻译
    assessmentLevelFormat(row, column) {
      return this.selectDictLabel(this.assessmentLevelOptions, row.assessmentLevel);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        assessmentId: null,
        workId: null,
        checkFeedback: null,
        checkUrl: null,
        checkTime: null,
        checkUser: null,
        assessmentContent: null,
        assessmentTime: null,
        assessmentUser: null,
        assessmentLevel: null,
        isModify: null,
        modifyOpinion: null,
        modifyPeople: null,
        modifyPhone: null,
        modifyTerm: null,
        modifyBegin: null,
        modifyEnd: null,
        modifyComplete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assessmentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加排查评估";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const assessmentId = row.assessmentId || this.ids
      getAssessment(assessmentId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排查评估";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assessmentId != null) {
            updateAssessment(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssessment(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assessmentIds = row.assessmentId || this.ids;
      this.$confirm('是否确认删除排查评估编号为"' + assessmentIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAssessment(assessmentIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有排查评估数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAssessment(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
