<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :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="dealDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.dealDateStart"
                        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.dealDateEnd"
                        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"
          v-hasPermi="['standingBook:trouble:add']"
        >新增</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="['standingBook:trouble:export']"
        >导出</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" width="180px">
        <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="responsibleUnit" />
      <el-table-column label="责任人员" align="center" prop="responsiblePeople" />
      <el-table-column label="是否处理" align="center" prop="isDeal" width="120px">
        <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" 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="['standingBook:trouble:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showDetail(scope.row)"
            v-hasPermi="['standingBook:trouble:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['standingBook:trouble: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 @cancel="cancel">
      <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-row>
                <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-row>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="事故原因" prop="troubleReason">
              <el-input v-model="form.troubleReason" placeholder="请输入事故原因" />
            </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="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="请选择处理结果">
                <el-option label="已处理" value="1" />
                <el-option label="未处理" value="2" />
              </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="cancel">取 消</el-button>
      </div>
    </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';

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: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        troubleName: null,
        troubleType: null,
        isDeal: null,
        dealDateStart: null,
        dealDateEnd: null
      },
      // 表单参数
      form: {},
      // 表单校验
      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" },
        ],
        troubleReason: [
          { required: true, message: "请输入事故原因", trigger: "blur" },
        ],
        responsibleUnit: [
          { required: true, message: "请输入责任单位", trigger: "blur" },
        ],
        responsiblePeople: [
          { required: true, message: "请输入责任人员", trigger: "blur" },
        ],
      }
    };
  },
  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;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        troubleId: null,
        troubleName: null,
        troubleLocation: null,
        troubleType: null,
        briefProcess: null,
        troubleReason: null,
        responsibleUnit: null,
        responsiblePeople: null,
        isDeal: null,
        dealDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加事故台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const troubleId = row.troubleId || this.ids
      getTrouble(troubleId).then(response => {
        this.form = response.data;
        this.devicePos = [this.form.longitude, this.form.latitude];
        this.open = true;
        this.title = "修改事故台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.troubleId != null) {
            updateTrouble(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTrouble(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = 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.$router.push({
        path: '/standingBook/troubleDetail',
        query: {
          troubleId: row.troubleId
        }
      })
    },
    MapdialogFun() {
      this.dialogTableVisible = true;
    },
    dialogcancelFun() {
      this.dialogTableVisible = false;
    },
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    }
  }
};
</script>
