<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="hiddenId" width="80px"/>
      <el-table-column label="隐患名称" align="center" prop="hiddenTitle" />
      <el-table-column label="隐患位置" align="center" prop="hiddenLocation" />
      <el-table-column label="隐患类型" align="center" prop="hiddenType" />
      <el-table-column label="隐患发现人员" align="center" prop="hiddenFindPeople" />
      <el-table-column label="发现时间" align="center" prop="hiddenFindDate" width="180px">
        <template slot-scope="scope">
          <span>{{ scope.row.hiddenFindDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理方案" align="center" prop="dealPlan" >
        <template slot-scope="scope">
          <span
            class="dbtn sd qiCr"
            @click="checkFile(scope.row.dealPlan)"
            v-if="scope.row.dealPlan != ''"
          >
            <i class="el-icon el-icon-view"></i>查看/下载
          </span>
        </template>
      </el-table-column>
      <el-table-column label="整治情况" align="center" prop="remediation" />
      <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>
      <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">
              <editor v-model="form.hiddenContent" :min-height="192"/>
            </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.number="form.longitude" placeholder="请输入经度" />
                </el-col>
                <el-col :span="9" style="margin-left: 10px">
                  <el-input v-model.number="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">
              <MyFileUpload
                listType="picture-card"
                @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>

    <Mapdialog
      v-if="loadmap"
      :dialogTableVisible="dialogTableVisible"
      @dialogcancelFun="dialogcancelFun"
      :slat="form.latitude"
      :slng="form.longitude"
      @confirmFun="confirmFun($event)"
    ></Mapdialog>

  </div>
</template>

<script>
import { listHidden, getHidden, delHidden, addHidden, updateHidden, exportHidden } from "@/api/standingBook/hidden";
import Editor from '@/components/Editor';
import MyFileUpload from '@/components/MyFileUpload';

export default {
  name: "Hidden",
  components: {
    Editor,
    MyFileUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 隐患整治台账表格数据
      hiddenList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 隐患类型字典
      typeOptions: [],
      // 上传文件列表
      fileList: [],
      // 地图
      loadmap: false,
      dialogTableVisible: false,
      // 查询参数
      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");
    },
    /** 搜索按钮操作 */
    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;
        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('http://localhost:8903/gassafety' + url,'_blank')
      // window.open('http://222.223.203.154:8092/gassafety' + url,'_blank')
    },
    getFileInfo(res){
      this.form.dealPlan = res.url;
    },
    listRemove(e) {
      this.form.dealPlan = "";
      this.fileList = [];
    },
    confirmFun(res) {
      //确认选择经纬度
      this.form.longitude = res.lng;
      this.form.latitude = res.lat;
    },
    MapdialogFun() {
      this.loadmap = true;
      this.dialogTableVisible = true;
    },
    dialogcancelFun() {
      this.loadmap = false;
      this.dialogTableVisible = false;
    },
  }
};
</script>
