<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <date-range-picker
          type="datetimerange"
          v-model="customs.timeRange"
          class="date-item"
          style="width: 360px !important"
        />
        <el-select
          v-model="query.typeId"
          filterable
          clearable
          size="small"
          placeholder="请选择类型"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option v-for="item of typeList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
        <el-select
          v-model="query.level"
          filterable
          clearable
          size="small"
          placeholder="请选择事件等级"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item of levelList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-select
          v-model="query.recoveryStatus"
          filterable
          clearable
          size="small"
          placeholder="请选择事件状态"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option label="未解除" value="0" />
          <el-option label="解除" value="1" />
        </el-select>
        <rrOperation />
      </div>
      <crudOperation :permission="permission">
        <template slot="right">
          <el-button
            class="filter-item"
            size="mini"
            type="primary"
            icon="el-icon-connection"
            :disabled="crud.selections.length === 0"
            @click="handleEliminate(crud.selections)"
          >
            解除
          </el-button>
        </template>
      </crudOperation>
    </div>
    <!--表单组件-->
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="550px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="事件时间" prop="eventTime">
          <el-date-picker
            v-model="form.eventTime"
            type="datetime"
            placeholder="选择日期"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 370px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="事件类型" prop="typeId">
          <el-select
            v-model="form.typeId"
            filterable
            clearable
            size="small"
            placeholder="请选择"
            class="filter-item"
            style="width: 370px"
            @change="changeType"
          >
            <el-option
              v-for="item of typeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事件等级" prop="level">
          <el-select
            v-model="form.level"
            filterable
            size="small"
            placeholder="请选择"
            class="filter-item"
            style="width: 370px"
          >
            <el-option
              v-for="item of levelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" rows="3" v-model="form.content" style="width: 370px" />
        </el-form-item>
        <el-form-item label="切换方式" prop="triggerType">
          <el-select
            v-model="form.triggerType"
            filterable
            size="small"
            placeholder="请选择"
            class="filter-item"
            style="width: 370px"
          >
            <el-option label="自动" value="0"></el-option>
            <el-option label="人工" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="解除时间" prop="recoveryTime">
          <el-date-picker
            v-model="form.recoveryTime"
            type="datetime"
            placeholder="选择日期"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            style="width: 370px"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="解除方式" prop="recoveryType">
          <el-select
            v-model="form.recoveryType"
            filterable
            size="small"
            placeholder="请选择"
            class="filter-item"
            style="width: 370px"
          >
            <el-option label="自动" value="0"></el-option>
            <el-option label="人工" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联应急处置卡" prop="disposalCardId">
          <el-select
            v-model="form.disposalCardId"
            filterable
            size="small"
            placeholder="请选择"
            class="filter-item"
            style="width: 370px"
          >
            <el-option
              v-for="item of cardList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联应急预案" prop="planId">
          <el-select
            v-model="form.planId"
            filterable
            size="small"
            placeholder="请选择"
            class="filter-item"
            style="width: 370px"
          >
            <el-option
              v-for="item of planList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="crud.cancelCU">取消</el-button>
        <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU"
          >确认</el-button
        >
      </div>
    </el-dialog>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column
        label="事件时间"
        prop="eventTime"
        align="center"
        width="160"
      ></el-table-column>
      <el-table-column label="风险事件类型" prop="typeName" align="center"></el-table-column>
      <!-- <el-table-column label="事件等级" prop="levelStr" align="center"> </el-table-column> -->
      <el-table-column label="事件内容" prop="content" align="center" width="300"></el-table-column>
      <!-- <el-table-column label="切换方式" prop="triggerType" align="center">
        <template slot-scope="scope">
          {{ scope.row.triggerType == 0 ? '自动' : '人工' }}
        </template>
      </el-table-column> -->
      <el-table-column
        label="解除时间"
        prop="recoveryTime"
        align="center"
        width="160"
      ></el-table-column>
      <el-table-column label="解除方式" prop="recoveryType" align="center">
        <template slot-scope="scope">
          {{ scope.row.recoveryType == 0 ? '自动' : '人工' }}
        </template>
      </el-table-column>
      <el-table-column label="解除状态" prop="recoveryStatus" align="center">
        <template slot-scope="scope">
          {{ scope.row.recoveryType == 1 ? '解除' : '未解除' }}
        </template>
      </el-table-column>
      <el-table-column label="关联应急处置卡" prop="disposalCardId" align="center" width="150">
        <template slot-scope="scope">
          {{
            cardList.find((item) => item.id === scope.row.disposalCardId)
              ? cardList.find((item) => item.id === scope.row.disposalCardId).name
              : ''
          }}
        </template>
      </el-table-column>
      <el-table-column label="关联应急预案" prop="planId" align="center">
        <template slot-scope="scope">
          {{
            planList.find((item) => item.id === scope.row.planId)
              ? planList.find((item) => item.id === scope.row.planId).name
              : ''
          }}
        </template>
      </el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'eventRecord:edit', 'eventRecord:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-connection"
            type="primary"
            @click="handleEliminate(scope.row)"
          >
            解除
          </el-button>
          <!-- <udOperation :data="scope.row" :permission="permission" /> -->
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudRecord from '@/api/risk/record'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { getToken } from '@/utils/auth'
import DateRangePicker from '@/components/DateRangePicker'
import { initData } from '@/api/data'
import Dayjs from 'dayjs'
const defaultForm = {
  id: '',
  eventTime: '', // 事件时间
  typeId: '', // 风险事件类型ID
  level: '', // 事件等级
  content: '', // 事件内容
  triggerType: '', // 自动、人工
  recoveryTime: '', // 解除时间
  recoveryType: '', // 自动、人工
  disposalCardId: '', // 关联应急处置卡
  planId: '', // 关联应急预案
}

export default {
  name: 'eventRecord',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: '风险事件记录',
      url: 'risk/record/getList',
      listOrder: [],
      crudMethod: { ...crudRecord },
      customs: { timeRange: '' },
      optShow: { add: false, edit: false, del: false, reset: true },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
    VUE_APP_BASE_API() {
      return process.env.VUE_APP_BASE_API
    },
    token() {
      return getToken() ?? ''
    },
  },
  data() {
    return {
      permission: {
        add: ['admin', 'eventRecord:add'],
        edit: ['admin', 'eventRecord:edit'],
        del: ['admin', 'eventRecord:del'],
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
      },
      cardList: [],
      planList: [],
      typeList: [],
      levelList: [
        { label: '一级', text: '一级', value: '1' },
        { label: '二级', text: '二级', value: '2' },
        { label: '三级', text: '三级', value: '3' },
      ],
    }
  },
  created() {
    this.getCardData()
    this.getPlanData()
    this.getTypeData()
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      if (!this.customs.timeRange) {
        this.customs.timeRange = [
          Dayjs(new Date()).subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss'),
          Dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
        ]
      }
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    getTypeData() {
      initData('risk/type/getList', { pageIndex: 1, pageSize: 999 }).then((res) => {
        let content = res.data.content || []
        this.typeList = content
      })
    },
    getCardData() {
      initData('emergency/card/getList', { pageIndex: 1, pageSize: 999 }).then((res) => {
        let content = res.data.content || []
        this.cardList = content
      })
    },
    getPlanData() {
      initData('emergency/plan/getList', { pageIndex: 1, pageSize: 999 }).then((res) => {
        let content = res.data.content || []
        this.planList = content
      })
    },
    changeType(data) {
      const typeData = this.typeList.find((item) => item.id === data)
      if (!!typeData) {
        this.form.disposalCardId = typeData.disposalCardId
        this.form.planId = typeData.planId
      } else {
        this.form.disposalCardId = ''
        this.form.planId = ''
      }
    },
    async handleEliminate(data) {
      let ids = []
      if (Array.isArray(data)) {
        ids = data.map((d) => d.id)
      } else {
        ids = [data.id]
      }
      this.$confirm('确定解除风险提示?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          try {
            const { code, msg } = await initData('risk/record/eliminate', ids)
            this.$message({
              type: code == 20000 ? 'success' : 'danger',
              message: msg,
            })
            this.crud.toQuery()
          } catch (error) {
            console.log(error)
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.table-opt-box {
  display: flex;
  justify-content: center;
  .slot-btn {
    .el-button {
      margin-left: 0;
      margin-right: 8px;
    }
  }
}
</style>
