<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryParams" :inline="true" v-show="showSearch" label-width="100px">
      <!--<el-form-item label="事故名称" prop="troubleName">
        <el-input
          v-model="queryParams.troubleName"
          placeholder="请输入事故名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="事故类型" prop="troubleType">
        <el-select v-model="queryParams.troubleType" placeholder="请选择事故类型" clearable size="small">
          <el-option
            v-for="dict in troubleTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否处理" prop="isDeal">
        <el-select v-model="queryParams.isDeal" placeholder="请选择是否处理" clearable size="small">
          <el-option
            v-for="dict in isDealOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="事故发生时间" prop="happenDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.happenDateStart"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择起始时间">
        </el-date-picker><span style="color: #bebfc3"> - </span>
        <el-date-picker clearable size="small"
                        v-model="queryParams.happenDateEnd"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        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"
        >新增</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="troubleList" >
      <el-table-column label="事故名称" align="center" prop="troubleName" width="200px"/>
      <el-table-column label="事故类型" align="center" prop="troubleType">
        <template slot-scope="scope">
          <span v-if="scope.row.troubleType == 1">生产安全事故</span>
          <span v-if="scope.row.troubleType == 2">非生产安全事故</span>
        </template>
      </el-table-column>
      <el-table-column label="事故地点" align="center" prop="troubleLocation" width="300px"/>
      <el-table-column label="事故发生时间" align="center" prop="happenDate" width="150px"/>
      <el-table-column label="是否人员伤亡" align="center" prop="isCasualties" width="150px">
        <template slot-scope="scope">
          <span v-if="scope.row.isCasualties == 1">是</span>
          <span v-if="scope.row.isCasualties == 2">否</span>
        </template>
      </el-table-column>
      <el-table-column label="责任单位" align="center" prop="responsibleUnit" width="150px"/>
      <el-table-column label="责任人员" align="center" prop="responsiblePeople"/>
      <el-table-column label="是否处理" align="center" prop="isDeal">
        <template slot-scope="scope">
          <span v-if="scope.row.isDeal == 1">已处理</span>
          <span v-if="scope.row.isDeal == 2">未处理</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="处理完成时间" align="center" prop="dealDate" width="180px"/>-->
      <el-table-column label="操作" align="center" width="180px">
        <template slot-scope="scope">
          <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-edit"
            @click="showDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(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="title1" :visible.sync="open1" width="800px" append-to-body @cancel="cancel1">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="事故名称" prop="troubleName">
              <el-input v-model="form.troubleName" placeholder="请输入事故名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="事故类型" prop="troubleType">
              <el-select v-model="form.troubleType" placeholder="请选择事故类型" style="width: 100%">
                <el-option label="生产安全事故" value="1" />
                <el-option label="非生产安全事故" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="事故地点" prop="troubleLocation">
              <el-input v-model="form.troubleLocation" placeholder="请输入事故地点" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="经纬度坐标" prop="longitude">
              <el-col :span="9">
                <el-input v-model="form.longitude" placeholder="请输入经度" />
              </el-col>
              <el-col :span="9" style="margin-left: 10px">
                <el-input v-model="form.latitude" placeholder="请输入纬度"/>
              </el-col>
              <el-col :span="3" style="margin-left: 30px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="事故原因" prop="troubleReason">
              <el-input v-model="form.troubleReason" placeholder="请输入事故原因" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="事故发生时间" prop="happenDate">
              <el-date-picker clearable size="small"
                              v-model="form.happenDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择事故发生时间"
                              style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="简要经过" prop="briefProcess">
              <el-input type="textarea" v-model="form.briefProcess" placeholder="请输入简要经过" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="是否人员伤亡" prop="isCasualties">
              <el-select v-model="form.isCasualties" placeholder="请选择是否人员伤亡" style="width: 100%">
                <el-option label="是" value="1" />
                <el-option label="否" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.isCasualties == '1'">
          <el-col :span="11">
            <el-form-item label="受伤人数" prop="injuryNum">
              <el-input v-model.number="form.injuryNum" placeholder="请输入受伤人数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="死亡人数" prop="deathNum">
              <el-input v-model.number="form.deathNum" placeholder="请输入死亡人数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="责任单位" prop="responsibleUnit">
              <el-input v-model="form.responsibleUnit" placeholder="请输入责任单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人员" prop="responsiblePeople">
              <el-input v-model="form.responsiblePeople" placeholder="请输入责任人员" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="是否处理" prop="isDeal">
              <el-select v-model="form.isDeal" placeholder="请选择处理结果" filterable style="width: 100%" >
                <el-option
                  v-for="dict in isDealOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理完成时间" prop="dealDate">
              <el-date-picker clearable size="small"
                              v-model="form.dealDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择处理完成时间"
                              style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="备注信息" prop="remarks">
              <el-input type="textarea" v-model="form.remarks" 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="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title2" :visible.sync="open2" width="1200px" append-to-body @cancel="cancel2" @close="cancel2">
      <el-row>
        <el-col :span="14">
          <el-form ref="detailForm" :model="detailForm" label-width="120px">
            <el-row>
              <el-col :span="11">
                <el-form-item label="事故名称:">
                  <el-input v-model="detailForm.troubleName" disabled/>
                </el-form-item>
                <el-form-item label="事故原因:">
                  <el-input v-model="detailForm.troubleReason" disabled/>
                </el-form-item>
                <el-form-item label="责任单位:">
                  <el-input v-model="detailForm.responsibleUnit" disabled/>
                </el-form-item>
                <el-form-item label="是否人员伤亡:">
                  <el-input v-if="detailForm.isCasualties == '1'" value="是" disabled/>
                  <el-input v-if="detailForm.isCasualties == '2'" value="是" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="事故类型:">
                  <el-input v-if="detailForm.troubleType == '1'" value="安全生产事故" disabled/>
                  <el-input v-if="detailForm.troubleType == '2'" value="非生产安全事故" disabled/>
                </el-form-item>
                <el-form-item label="事故地点:">
                  <el-input v-model="detailForm.troubleLocation" disabled/>
                </el-form-item>
                <el-form-item label="责任人员:">
                  <el-input v-model="detailForm.responsiblePeople" disabled/>
                </el-form-item>
                <el-form-item label="是否处理:">
                  <el-input v-if="detailForm.isDeal == '1'" value="已处理" disabled/>
                  <el-input v-if="detailForm.isDeal == '2'" value="未处理" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row v-if="detailForm.isCasualties == '1'">
              <el-col :span="11">
                <el-form-item label="受伤人数:">
                  <el-input v-model="detailForm.injuryNum" disabled/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="死亡人数:">
                  <el-input v-model="detailForm.deathNum" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="23">
                <el-form-item label="处理完成时间:">
                  <el-input v-model="detailForm.dealDate" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="23">
                <el-form-item label="简要经过:">
                  <el-input type="textarea" v-model="detailForm.briefProcess" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="23">
                <el-form-item label="备注信息:">
                  <el-input type="textarea" v-model="detailForm.remarks" disabled/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>

        <el-col :span="9">
          <div style="width: 100%;height: 390px; border: 1px solid rgb(218, 213, 213);margin-bottom: 10px;">
            <div style="width: 100%;height: 100%" id="troubleContainer"></div>
          </div>
        </el-col>
      </el-row>
    </el-dialog>

    <GetPos
      :dialogVisible.sync="dialogTableVisible"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />

  </div>
</template>

<script>
import { listTrouble, getTrouble, delTrouble, addTrouble, updateTrouble, exportTrouble } from "@/api/standingBook/trouble";
import GetPos from '@/components/GetPos';
import { EditorMap } from "@/utils/mapClass/getPath.js";

export default {
  name: "Trouble",
  components: {
    GetPos
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 事故台账表格数据
      troubleList: [],
      // 数据字典类型
      troubleTypeOptions: [],
      isDealOptions: [],
      // 地图
      dialogTableVisible: false,
      devicePos: [],
      map: null,
      // 弹出层标题
      title1: "",
      title2: "",
      // 是否显示弹出层
      open1: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        troubleName: null,
        troubleType: null,
        isDeal: null,
        happenDateStart: null,
        happenDateEnd: null,
        dealDateStart: null,
        dealDateEnd: null
      },
      // 表单参数
      form: {},
      detailForm: {},
      // 表单校验
      rules: {
        troubleName: [
          { required: true, message: "请输入事故名称", trigger: "blur" },
        ],
        troubleType: [
          { required: true, message: "请输入事故类型", trigger: "blur" },
        ],
        troubleLocation: [
          { required: true, message: "请输入事故地点", trigger: "blur" },
        ],
        longitude: [
          { required: true, message: "请输入经纬度", trigger: "blur" },
        ],
        happenDate: [
          { required: true, message: "请选择事故发生时间", trigger: "blur" },
        ],
        troubleReason: [
          { required: true, message: "请输入事故原因", trigger: "blur" },
        ],
        isCasualties: [
          { required: true, message: "请选择是否人员伤亡", trigger: "change" },
        ],
        responsibleUnit: [
          { required: true, message: "请输入责任单位", trigger: "blur" },
        ],
        responsiblePeople: [
          { required: true, message: "请输入责任人员", trigger: "blur" },
        ],
        isDeal: [
          { required: true, message: "请选择是否已处理", trigger: "change" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_trouble_type").then(response => {
      this.troubleTypeOptions = response.data;
    });
    this.getDicts("t_is_deal").then(response => {
      this.isDealOptions = response.data;
    });
  },
  methods: {
    /** 查询事故台账列表 */
    getList() {
      this.loading = true;
      listTrouble(this.queryParams).then(response => {
        this.troubleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel1() {
      this.open1 = false;
      this.reset1();
    },
    cancel2() {
      this.open2 = false;
      this.reset2();
      this.map.destroy();
    },
    // 表单重置
    reset1() {
      this.form = {
        troubleId: null,
        troubleName: null,
        troubleLocation: null,
        longitude: null,
        latitude: null,
        troubleType: null,
        briefProcess: null,
        troubleReason: null,
        isCasualties: null,
        injuryNum: null,
        deathNum: null,
        responsibleUnit: null,
        responsiblePeople: null,
        isDeal: null,
        happenDate: null,
        dealDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
      this.devicePos = [];
    },
    reset2() {
      this.detailForm = {
        troubleId: null,
        troubleName: null,
        troubleLocation: null,
        longitude: null,
        latitude: null,
        troubleType: null,
        briefProcess: null,
        troubleReason: null,
        isCasualties: null,
        injuryNum: null,
        deathNum: null,
        responsibleUnit: null,
        responsiblePeople: null,
        isDeal: null,
        happenDate: null,
        dealDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("detailForm");
      this.devicePos = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        troubleName: null,
        troubleType: null,
        isDeal: null,
        happenDateStart: null,
        happenDateEnd: null,
        dealDateStart: null,
        dealDateEnd: null
      },
      this.resetForm("queryParams");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset1();
      this.open1 = true;
      this.title1 = "添加燃气事故台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset1();
      getTrouble(row.troubleId).then(response => {
        this.form = response.data;
        this.devicePos = [this.form.longitude, this.form.latitude];
        this.open1 = true;
        this.title1 = "修改燃气事故台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.troubleId != null) {
            updateTrouble(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            });
          } else {
            addTrouble(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open1 = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const troubleIds = row.troubleId || this.ids;
      row.isDel = "1";
      this.$confirm('是否确认删除"' + row.troubleName + '"的台账?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateTrouble(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有事故台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTrouble(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 详细信息跳转 */
    showDetail(row) {
      this.reset2();
      getTrouble(row.troubleId).then(response => {
        this.detailForm = response.data;
        this.devicePos = [this.detailForm.longitude, this.detailForm.latitude];
        console.log("this.devicePos",this.devicePos);
        this.open2 = true;
        this.title2 = "燃气事故台账详情";

        this.$nextTick(() => {
          this.map = new EditorMap("troubleContainer", {}, this);
          this.map.addDevice({ path: this.devicePos });
          this.map.nowMouseTarget = null;
          this.map.mousetoolClose(false);
        });
      });
    },
    MapdialogFun() {
      this.dialogTableVisible = true;
    },
    dialogcancelFun() {
      this.dialogTableVisible = false;
    },
    getPath(res){
      console.log("res", res);
      console.log(this.form.longitude, this.form.latitude);
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    }
  }
};
</script>
