<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-select v-model="query.cates" placeholder="事件种类" @change="handleQuery" clearable>
          <el-option v-for="item in cateOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-select v-model="query.area" placeholder="发生区域" @change="handleQuery" style="margin-left:2px" clearable
          filterable>
          <el-option v-for="item in areaOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
        <el-date-picker v-model="timeRange" type="datetimerange" range-separator="至" start-placeholder="发生时间始"
          end-placeholder="发生时间止" style="margin-left:2px" value-format="YYYY-MM-DD HH:mm:ss" @change="handleQuery"
          clearable />
        <el-button type="danger" @click="handleDelete" style="margin-left: 2px;">批量删除</el-button>
      </div>
      <div class="right-panel">
        <el-button type="primary" :loading='dLoading' icon="el-icon-download" @click="exportList">下载</el-button>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" row-key="id" fit stripe @selection-change="handleSelectionChange"
        @resetQuery="resetQuery">
        <el-table-column type="selection" width="55" />
        <el-table-column label="#" type="index" width="50"></el-table-column>
        <el-table-column label="事件种类" width="240" :show-overflow-tooltip="true">
          <template #default="scope">
            <el-tag v-for="item in scope.row.cates_" :key="item.id" type="warning" effect="plain">{{ item.name }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="发生区域" prop="area_.name"></el-table-column>
        <el-table-column label="事件类型" prop="obj_cate" width="100">
          <template #default="scope">
            {{ objCateOptions[scope.row.obj_cate] }}
          </template>
        </el-table-column>
        <el-table-column label="事件对象" :show-overflow-tooltip="true">
          <template #default="scope">
            <span v-if="scope.row.opl">{{ scope.row.operation_name }}</span>
            <span v-else-if="scope.row.employee">
              {{ eTypeOptions[scope.row.employee_.type] }}-{{ scope.row.employee_.name }}-{{
                scope.row.employee_.belong_dept_name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="发生时间" prop="create_time" width="150"></el-table-column>

        <el-table-column label="处理人" prop="handle_user_name"></el-table-column>
        <el-table-column label="事件标记" prop="mark">
          <template #default="scope">
            <el-tag v-if="scope.row.mark == 20" type="warning" effect="plain">误报</el-tag>
            <el-tag v-else type="success" effect="plain">正确</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理超时">
          <template #default="scope">
            <el-tag v-if="scope.row.is_timeout" type="warning">是</el-tag>
            <el-tag v-else type="success" effect="plain">否</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" align="center">
          <template #default="scope">
            <el-button link type="info" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
            <el-button link type="primary" size="small" v-if="scope.row.handle_user == null"
              @click="table_handle(scope.row, scope.$index)">处理</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row)">
              <template #reference>
                <el-button link type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
    <detail-dialog v-if="dialog.handle" ref="handleDialog" :eventID="eventID" @oplDetail="oplDetail"
      @success="handleSuccess" @closed="dialog.handle = false"></detail-dialog>
    <el-drawer :size="'50%'" v-model="showLimited" title="作业许可证详情" :close-on-click-modal="false">
      <sc-fire :id="oplId"></sc-fire>
    </el-drawer>
  </el-container>
</template>
<script>
import detailDialog from "./event_handlefrom.vue";
import { CircleCheckFilled, CircleCloseFilled } from "@element-plus/icons-vue";
export default {
  name: "event",
  components: {
    CircleCheckFilled,
    CircleCloseFilled,
    detailDialog
  },
  data() {
    return {
      dLoading: false,
      showLimited: false,
      defaultTime: ['00:00:00', '23:59:59'],
      timeRange: [],
      selectionList: [],
      objCateOptions: {
        'people': '人员',
        'opl': '作业',
        'other': '其他'
      },
      eTypeOptions: {
        'employee': '正式员工',
        'remployee': '相关方',
        'visitor': '访客',
        'driver': '司机'
      },
      cateOptions: [],
      areaOptions: [],
      dialog: {
        save: false,
        handle: false,
        permission: false,
      },
      apiObj: this.$API.ecm.event.list,
      query: {},
      selection: [],
      search: {
        keyword: null,
      },
      eventID: '',
      oplId: null,
    };
  },
  created() {
    this.getCateOptions();
    this.getAreaOptions();
  },
  methods: {
    //处理
    table_handle(row) {
      this.eventID = row.id;
      this.dialog.handle = true;
      this.$nextTick(() => {
        this.$refs.handleDialog.open("edit").setData(row);
      });
    },
    handleSelectionChange(val) {
      this.selectionList = val;
    },
    table_del(row) {
      this.$API.ecm.event.delete.req(row.id).then(res => {
        this.$refs.table.refresh();
      })
    },
    handleDelete() {
      let that = this;
      if (that.selectionList.length > 0) {
        that.$confirm(`确定删除选中的${that.selectionList.length}条数据吗？`, "提示", { type: "warning" }).then(() => {
          let ids = [];
          that.selectionList.forEach(function (item) {
            ids.push(item.id);
          });
          that.$API.ecm.event.delete.req('bulk', { ids: ids }).then((res) => {
            that.$message.success("批量删除成功");
            that.$refs.table.refresh();
            return res;
          }).catch((err) => {
            that.$message.warning(err);
          });
        })
      }
    },
    //查看
    table_show(row) {
      this.eventID = row.id;
      this.dialog.handle = true;
      this.$nextTick(() => {
        this.$refs.handleDialog.open("show").setData(row);
      });
    },
    getCateOptions() {
      this.$API.ecm.event_cate.list.req({ page: 0 }).then((res) => {
        this.cateOptions = res;
      });
    },
    getAreaOptions() {
      this.$API.am.area.list.req({ page: 0 }).then(res => {
        this.areaOptions = res;
      })
    },
    handleSuccess() {
      this.$refs.table.refresh();
    },
    oplDetail(data) {
      debugger;
      console.log(data)
      this.oplId = data;
      this.showLimited = true;
    },
    //搜索
    handleQuery() {
      if (this.timeRange) {
        this.query.start_create = this.timeRange[0]
        this.query.end_create = this.timeRange[1]
      } else {
        this.query.end_create = null
        this.query.start_create = null
      }
      if (this.query.cates) {

      } else {
        this.query.cates = null
      }
      this.$refs.table.queryData(this.query);
    },
    //本地更新数据

    resetQuery() {
      this.query = {};
    },

    exportList() {
      this.dLoading = true;
      this.$API.ecm.event.export_excel
        .req(this.query)
        .then(res => {
          window.open(res.path, "_blank");
          this.dLoading = false;
        }).catch(e => { this.dLoading = false; })
    }
  },
};
</script>