<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">

      <el-form-item label="隐患标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入隐患标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="隐患来源" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择隐患来源" clearable size="small">
          <el-option
            v-for="item in dangerSource"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>


     <!-- <el-form-item label="隐患级别" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择隐患级别" clearable size="small">
          <el-option label="重大" value="1" />
          <el-option label="一般" value="2" />
        </el-select>
      </el-form-item>-->

      <el-form-item label="责任归属" prop="flag">
        <el-select v-model="queryParams.flag" placeholder="请选择责任归属" clearable size="small">
          <el-option label="安全管理部" value="1" />
          <el-option label="客服运营部" value="2" />
          <el-option label="生产运营部" value="3" />
        </el-select>
      </el-form-item>


      <el-form-item label="隐患状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择隐患状态" clearable size="small">
          <el-option label="待处理" value="0" />
          <el-option label="拒绝整改" value="1" />
          <el-option label="待复核" value="2" />
          <el-option label="已处理" value="3" />
          <el-option label="已归档" value="4" />
        </el-select>
      </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="['checkTask:danger: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="['checkTask:danger:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dangerList">
      <el-table-column label="序号" align="center" prop="id" v-if="!$store.state.user.roles.includes('visitor')"/>
      <el-table-column label="隐患标题" align="center" prop="title" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="隐患来源" align="center" prop="type" :formatter="typeFormatter"/>
      <el-table-column label="责任归属" align="center" prop="flag">
        <template slot-scope="scope">
          <span v-if="scope.row.flag == 1">安全管理部</span>
          <span v-else-if="scope.row.flag == 2">客服运营部</span>
          <span v-else-if="scope.row.flag == 3">生产运营部</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患级别" align="center" prop="level">
        <template slot-scope="scope">
          <span v-if="scope.row.level == 1">重大</span>
          <span v-if="scope.row.level == 2">一般</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患地址" align="center" prop="address" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="隐患状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: red">待处理</span>
          <span v-if="scope.row.status == 1" style="color: blue">拒绝整改</span>
          <span v-if="scope.row.status == 2" style="color: blue">待复核</span>
          <span v-if="scope.row.status == 3" style="color: green">已处理</span>
          <span v-if="scope.row.status == 4" style="color: green">已归档</span>
        </template>
      </el-table-column>
      <el-table-column label="上报人" align="center" prop="reportName" />
      <el-table-column label="上报时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column label="备注" align="center" prop="remark" width="100" :show-overflow-tooltip="true"/>-->
      <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)"
            v-hasPermi="['checkTask:danger:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['checkTask:danger: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="900px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="隐患标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入隐患标题" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="隐患来源" prop="type">
              <el-select v-model="form.type" placeholder="请选择隐患来源" style="width: 100%">
                <el-option label="居民" value="1" />
                <el-option label="工商户" value="2" />
                <el-option label="工业" value="3" />
                <el-option label="其他" value="4" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="居民" prop="receiveId">
              <el-input v-model="form.receiveId" placeholder="请输入居民" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="隐患级别" prop="level">
              <el-select v-model="form.level" placeholder="请选择隐患级别" style="width: 100%">
                <el-option label="重大" value="1" />
                <el-option label="一般" value="2" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="隐患内容">
          <el-input v-model="form.beforeContent" type="textarea" placeholder="请输入隐患内容" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="责任归属" prop="flag">
              <el-select v-model="form.flag" placeholder="请选择责任归属" style="width: 100%">
                <el-option label="安全管理部" value="1" />
                <el-option label="客服运营部" value="2" />
                <el-option label="生产运营部" value="3" />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="具体地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入具体地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="经纬度坐标" prop="longitude">
              <el-col :span="8">
                <el-input v-model="form.longitude" placeholder="请输入经度"/>
              </el-col>
              <el-col :span="8" style="margin-left: 20px">
                <el-input v-model="form.latitude" placeholder="请输入纬度"/>
              </el-col>
              <el-col :span="4" style="margin-left: 30px">
                <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="隐患整改前图片" prop="beforeImg">
          <el-input v-model="form.beforeImg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="隐患整改前视频" prop="beforeVideo">
          <el-input v-model="form.beforeVideo" placeholder="请输入隐患整改前视频" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </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="dialogTableVisibles"
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
      style="z-index: 3000"
    />

    <!-- 详情 -->
    <DangerDetail ref="dangerDetail"/>
  </div>
</template>

<script>
import { listDanger, getDanger, delDanger, addDanger, updateDanger, exportDanger } from "@/api/checktask/danger";
import Editor from '@/components/Editor';
import GetPos from '@/components/GetPos';
import DangerDetail from "./components/DangerDetail";
export default {
  name: "Danger",
  components: {
    Editor,
    GetPos,
    DangerDetail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安检隐患整改表格数据
      dangerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        inspectId: null,
        type: null,
        category: 1,
        receiveId: null,
        title: null,
        level: null,
        beforeContent: null,
        address: null,
        longitude: null,
        latitude: null,
        beforeImg: null,
        beforeVideo: null,
        flag: null,
        afterContent: null,
        afterImg: null,
        startTime: null,
        endTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      dialogTableVisibles: false,
      devicePos: [],
      dangerSource: []
    };
  },
  created() {
    this.getList();
    this.getDangerSource();
  },
  methods: {
    /** 查询安检隐患整改列表 */
    getList() {
      this.loading = true;
      listDanger(this.queryParams).then(response => {
        this.dangerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //隐患来源
    getDangerSource(){
      this.getDicts("t_danger_source").then(response => {
          this.dangerSource = response.data;
      });
    },
    typeFormatter(row){
      return this.selectDictLabel(
        this.dangerSource,
        row.type
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        inspectId: null,
        type: null,
        receiveId: null,
        title: null,
        level: null,
        beforeContent: null,
        address: null,
        longitude: null,
        latitude: null,
        beforeImg: null,
        beforeVideo: null,
        flag: null,
        afterContent: null,
        afterImg: null,
        startTime: null,
        endTime: null,
        status: 0,
        createTime: null,
        remark: 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.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安检隐患整改";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDanger(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改安检隐患整改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDanger(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDanger(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除安检隐患整改名称为"' + row.title + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDanger(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有安检隐患整改数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDanger(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    /**
     * 经纬度坐标方法
     */
    MapdialogFun() {
      this.dialogTableVisibles = true;
    },
    /**
     * 地图关闭方法
     */
    dialogcancelFun() {
      this.dialogTableVisibles = false;
    },
    /**
     * 经纬度 选择
     * @param res
     */
    getPath(res){
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    //详情
    handleDetail(row){
      this.$refs.dangerDetail.handleDangerDetail(row.id);
    }
  }
};
</script>
