<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="隐患名称" prop="hiddenTitle">
        <el-input
          v-model="queryParams.hiddenTitle"
          placeholder="请输入隐患名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐患类型" prop="hiddenType">
        <el-select v-model="queryParams.hiddenType" placeholder="请选择隐患类型" clearable size="small">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发现时间" prop="hiddenFindDate">
        <el-date-picker clearable size="small"
                        v-model="queryParams.hiddenFindDateStart"
                        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.hiddenFindDateEnd"
                        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:hidden: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:hidden:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hiddenList" >
      <el-table-column label="隐患名称" align="center" prop="hiddenTitle" width="200px"/>
      <el-table-column label="隐患等级" align="center" prop="hiddenType" width="150px">
        <template slot-scope="scope">
          <span v-if="scope.row.hiddenType == '1'">一级隐患</span>
          <span v-if="scope.row.hiddenType == '2'">二级隐患</span>
          <span v-if="scope.row.hiddenType == '3'">三级隐患</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患位置" align="center" prop="hiddenLocation" width="300px"/>
      <el-table-column label="隐患发现人员" align="center" prop="hiddenFindPeople" width="150px"/>
      <el-table-column label="发现时间" align="center" prop="hiddenFindDate" width="150px"/>
      <el-table-column label="处理方案" align="center" prop="dealPlanUrl" width="150px">
        <template slot-scope="scope">
          <span
            class="dbtn"
            @click="checkFile(scope.row.dealPlanUrl)"
            v-if="scope.row.dealPlan != ''"
          >
            <i class="el-icon el-icon-view"></i>查看/下载
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="整治情况" align="center" prop="remediation" width="180px">
        <template slot-scope="scope">
          <span v-if="scope.row.remediation != '' && scope.row.remediation != null">{{ scope.row.remediation }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <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:hidden:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showDetail(scope.row)"
            v-hasPermi="['standingBook:hidden:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['standingBook:hidden: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="hiddenTitle">
              <el-input v-model="form.hiddenTitle" placeholder="请输入隐患名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="隐患等级" prop="hiddenType">
              <el-select v-model="form.hiddenType" placeholder="请选择隐患等级" style="width: 100%">
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="隐患内容" prop="hiddenContent">
              <el-input type="textarea" v-model="form.hiddenContent" placeholder="请输入隐患内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="隐患位置" prop="hiddenLocation">
              <el-input v-model="form.hiddenLocation" 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="11">
            <el-form-item label="隐患发现人员" prop="hiddenFindPeople">
              <el-input v-model="form.hiddenFindPeople" placeholder="请输入隐患发现人员" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发现时间" prop="hiddenFindDate">
              <el-date-picker clearable size="small"
                              v-model="form.hiddenFindDate"
                              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="dealPlan">
              <FileUpload
                listType="picture"
                @resFun="getFileInfo"
                @remove="listRemove"
                :fileArr="fileList"
              />
              <el-input v-show="false" disabled v-model="form.dealPlan"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="整治情况" prop="remediation">
              <el-input v-model="form.remediation" placeholder="请输入整治情况" />
            </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 { listHidden, getHidden, delHidden, addHidden, updateHidden, exportHidden } from "@/api/standingBook/hidden";
import Editor from '@/components/Editor';
import FileUpload from '@/components/FileUpload';
import GetPos from '@/components/GetPos';

let uploadfile = require("@/assets/uploadfile.png");
export default {
  name: "Hidden",
  components: {
    Editor,
    FileUpload,
    GetPos
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患整治台账表格数据
      hiddenList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 隐患类型字典
      typeOptions: [],
      // 上传文件列表
      fileList: [],
      // 地图
      dialogTableVisible: false,
      devicePos: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hiddenTitle: null,
        hiddenType: null,
        hiddenFindDateStart: null,
        hiddenFindDateEnd: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hiddenTitle: [
          { required: true, message: "请输入隐患名称", trigger: "blur" },
        ],
        hiddenType: [
          { required: true, message: "请选择隐患等级", trigger: "blur" },
        ],
        hiddenContent: [
          { required: true, message: "请输入隐患内容", trigger: "blur" },
        ],
        hiddenLocation: [
          { required: true, message: "请输入经纬度", trigger: "blur" },
        ],
        longitude: [
          { required: true, message: "请输入经纬度", trigger: "blur" },
        ],
        hiddenFindPeople: [
          { required: true, message: "请输入隐患发现人员", trigger: "blur" },
        ],
        hiddenFindDate: [
          { required: true, message: "请选择发现时间", trigger: "change" },
        ],
        dealPlan: [
          { required: true, message: "请上传文件", trigger: "change" },
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_hidden_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    /** 查询隐患整治台账列表 */
    getList() {
      this.loading = true;
      listHidden(this.queryParams).then(response => {
        this.hiddenList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.fileList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        hiddenId: null,
        hiddenTitle: null,
        hiddenContent: null,
        hiddenLocation: null,
        hiddenType: null,
        hiddenFindPeople: null,
        hiddenFindDate: null,
        dealPlan: null,
        remediation: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
      this.fileList = [];
      this.devicePos = [];
    },
    /** 搜索按钮操作 */
    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 hiddenId = row.hiddenId || this.ids
      getHidden(hiddenId).then(response => {
        this.form = response.data;
        if (this.form.dealPlan) {
          this.fileList.push({
            name: this.form.dealPlan,
            url: uploadfile,
          });
        }
        this.devicePos = [this.form.longitude, this.form.latitude];
        this.open = true;
        this.title = "修改隐患整治台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hiddenId != null) {
            updateHidden(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHidden(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const hiddenIds = row.hiddenId || this.ids;
      row.isDel = "1";
      this.$confirm('是否确认删除"' + row.hiddenTitle + '"的台账?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateHidden(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有隐患整治台账数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportHidden(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /** 详细信息跳转 */
    showDetail(row) {
      this.$router.push({
        path: '/standingBook/hiddenDetail',
        query: {
          hiddenId: row.hiddenId
        }
      })
    },
    checkFile(url) {
      window.open(url,'_blank');
    },
    getFileInfo(res){
      this.form.dealPlan = res.fileName;
      this.form.dealPlanUrl = res.url;
      this.fileList.push({
        name: res.fileName,
        url: uploadfile,
      });
    },
    listRemove(e) {
      this.form.dealPlan = "";
      this.fileList = [];
    },
    MapdialogFun() {
      this.dialogTableVisible = true;
      // this.devicePos = [this.form.longitude, this.form.latitude];
      console.log("devicePos",this.devicePos)
    },
    dialogcancelFun() {
      this.dialogTableVisible = false;
    },
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    }
  }
};
</script>
<style>
  .dbtn {
    display: inline-block;
    padding: 2px 5px;
    cursor: pointer;
    border-radius: 3px;
    border-style: solid;
    border-width: 0;
    color: rgb(48, 180, 107);
    font-size: 9px;
  }
  .dbtn:hover {
    border-width: 1px;
    border-color: rgb(48, 180, 107);
  }
</style>
