<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目编码" prop="fIntersectConstructionId">
        <el-input
          v-model="queryParams.fIntersectConstructionId"
          placeholder="请输入项目编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="意外事件标题" prop="fTitle">
        <el-input
          v-model="queryParams.fTitle"
          placeholder="请输入意外事件标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		     :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="incList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目编码" align="center" prop="projectCode" :show-overflow-tooltip="true"/>
      <el-table-column label="意外事件标题" align="center" prop="fTitle" :show-overflow-tooltip="true"/>
      <el-table-column label="意外事件描述" align="center" prop="fDescription" :show-overflow-tooltip="true"/>
      <el-table-column label="意外事件发生事件" align="center" prop="fOccurrenctTime" :show-overflow-tooltip="true"/>
      <el-table-column label="附件" align="center" prop="fAttachment">
        <template slot-scope="scope">
          <span
            class="dbtn"
            @click="checkFile(scope.row.fAttachment)"
            v-if="scope.row.fAttachment != null && scope.row.fAttachment!=''"
          >
            <i class="el-icon el-icon-view"></i>查看/下载
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="fRemark" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.fRemark">
            {{ scope.row.fRemark }}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="fUpdateTime" />
      <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-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleReport(scope.row)"
          >上传</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 destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row class="el-row-table">
          <el-col :span="12">
            <el-form-item label="项目编码" prop="fIntersectConstructionId">
              <el-select
                v-model="form.fIntersectConstructionId"
                placeholder="请输入项目编码"
                style="width: 100%"
              >
                <el-option
                  v-for="item in projectInfo"
                  :key="'' + item.fConGasProInforId"
                  :label="item.fProjectCode"
                  :value="'' +item.fConGasProInforId"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="意外事件标题" prop="fTitle">
              <el-input v-model="form.fTitle" placeholder="请输入意外事件标题" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="意外事件描述" prop="fDescription">
              <el-input type="textarea" v-model="form.fDescription" placeholder="请输入意外事件描述" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="意外事件发生时间" prop="fOccurrenctTime">
              <el-date-picker
                style="width: 100%"
                v-model="form.fOccurrenctTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="意外事件发生事件时间">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="附件" prop="fAttachment">
              <FileUpload
                listType="picture"
                @resFun="getFileInfo"
                @remove="listRemove"
                :fileArr="fileList"
              />
              <el-input v-show="false" disabled v-model="form.fAttachment"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="fRemark">
              <el-input v-model="form.fRemark" placeholder="请输入备注" />
            </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>

    <!-- 详情 -->
    <DetailInfo ref="detail"/>
  </div>
</template>

<script>
import { listInc, getInc, delInc, addInc, updateInc, exportInc, reportAccidentInfo} from "@/api/thirdbuild/accident";
import { conGasProInfoList } from "@/api/thirdbuild/project";
import FileUpload from '@/components/FileUpload';
let uploadfile = require("@/assets/uploadfile.png");
import DetailInfo from "./components/DetailInfo";
export default {
  name: "Inc",
  components: {
    FileUpload,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 涉气第三方施工意外事件表格数据
      incList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fIntersectConstructionId: null,
        fTitle: null,
        fDescription: null,
        fOccurrenctTime: null,
        fAttachment: null,
        fRemark: null,
        fUpdateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fIntersectConstructionId: [
          { required: true, message: "项目编码不能为空", trigger: "blur" }
        ],
        fTitle: [
          { required: true, message: "意外事件标题不能为空", trigger: "blur" }
        ],
        fDescription: [
          { required: true, message: "意外事件描述", trigger: "blur" }
        ],
        fOccurrenctTime: [
          { required: true, message: "意外事件发生事件不能为空", trigger: "blur" }
        ],
        fAttachment: [
          { required: true, message: "附件不能为空", trigger: "blur" }
        ],
      },
      projectInfo: [],
      fileList: []
    };
  },
  created() {
    this.getList();
    this.getProjectInfo();
  },
  methods: {
    /** 查询涉气第三方施工意外事件列表 */
    getList() {
      this.loading = true;
      listInc(this.queryParams).then(response => {
        this.incList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fConGasProIncId: null,
        fIntersectConstructionId: null,
        fTitle: null,
        fDescription: null,
        fOccurrenctTime: null,
        fAttachment: null,
        fRemark: null,
        fUpdateTime: 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.fConGasProIncId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加涉气第三方施工意外事件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fConGasProIncId = row.fConGasProIncId || this.ids
      getInc(fConGasProIncId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改涉气第三方施工意外事件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fConGasProIncId != null) {
            updateInc(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInc(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fConGasProIncIds = row.fConGasProIncId || this.ids;
      this.$confirm('是否确认删除涉气第三方施工意外事件编号为"' + fConGasProIncIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInc(fConGasProIncIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有涉气第三方施工意外事件数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInc(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //获取第三方施工项目信息
    getProjectInfo(){
      conGasProInfoList({}).then(res =>{
        if(res.code == 200 && res.data){
          this.projectInfo = res.data;
        }
      })
    },
    //上传
    getFileInfo(res){
      this.form.fAttachment = res.url;
      this.fileList.push({
        name: res.fileName,
        url: uploadfile,
      });
    },
    listRemove(e) {
      this.form.fAttachment = "";
      this.fileList = [];
    },
    checkFile(url) {
      window.open(url,'_blank');
    },
    //详情
    handleDetail(row){
      this.$refs.detail.getDetailInfo(row.fConGasProIncId);
    },
    /** 删除按钮操作 */
    handleReport(row) {
      this.$confirm('是否确认上传项目编号为"' + row.projectCode + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return reportAccidentInfo({fConGasProIncId : row.fConGasProIncId});
        }).then(() => {
          this.getList();
        this.msgSuccess("上传成功");
      }).catch(() => {});
    },
  }
};
</script>
<style scoped lang="scss">
  .dbtn {
    display: inline-block;
    line-height: normal;
    padding-left: 2px;
    padding-right: 2px;
    cursor: pointer;
    border-radius: 3px;
    border-style: solid;
    border-width: 0;
    color: rgb(48, 180, 107);
  }
</style>
