<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <date-range-picker
          type="datetimerange"
          v-model="query.time"
          class="date-item"
          style="width: 360px !important"
        />
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入名称查询"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-input
          v-model="query.code"
          clearable
          size="small"
          placeholder="输入编码查询"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation />
      </div>
      <crudOperation :permission="permission"></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="130px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" style="width: 370px" />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input v-model="form.code" style="width: 370px" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input type="textarea" rows="3" v-model="form.desc" style="width: 370px" />
        </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-item label="相机视角切换方式" prop="perspectiveType">
          <el-select
            v-model="form.perspectiveType"
            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="perspective">
          <el-input v-model="form.perspective" style="width: 370px" />
        </el-form-item>
        <el-form-item label="执行附加脚本" prop="script">
          <el-input v-model="form.script" style="width: 370px" />
        </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="name" align="center"></el-table-column>
      <el-table-column label="类型编码" prop="code" align="center"></el-table-column>
      <el-table-column label="类型描述" prop="desc" align="center"></el-table-column>
      <el-table-column label="关联应急处置卡" prop="disposalCardId" align="center">
        <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 label="相机视角切换方式" prop="perspectiveType" align="center">
        <template slot-scope="scope">
          {{ scope.row.perspectiveType === 0 ? '按预设' : '按目标' }}
        </template>
      </el-table-column>
      <el-table-column label="预设的相机角度" prop="perspective" align="center"></el-table-column>
      <el-table-column label="执行的附加脚本" prop="script" align="center"></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'eventType:edit', 'eventType:del'])"
        label="操作"
        width="130px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <udOperation :data="scope.row" :permission="permission" />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>
import permission from '@/directive/permission'
import crudType from '@/api/risk/type'
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'

const defaultForm = {
  id: '',
  name: '', // 名称
  code: '', // 编码
  desc: '', // 描述
  disposalCardId: '', // 关联应急处置卡
  planId: '', // 关联应急预案
  perspectiveType: '', // 相机视角切换方式
  perspective: '', // 预设的相机角度
  script: '', // 执行的附加脚本
}

export default {
  name: 'eventType',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: '风险事件类型',
      url: 'risk/type/getList',
      listOrder: [],
      crudMethod: { ...crudType },
    })
  },
  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', 'eventType:add'],
        edit: ['admin', 'eventType:edit'],
        del: ['admin', 'eventType:del'],
      },
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        mac: [{ required: true, message: '请输入MAC', trigger: 'blur' }],
      },
      cardList: [],
      planList: [],
    }
  },
  created() {
    this.getCardData()
    this.getPlanData()
  },
  methods: {
    [CRUD.HOOK.afterRefresh]() {
      // this.crud.page.total = eventTypeList.length
      // this.crud.data = eventTypeList
      // this.crud.resetDataStatus()
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    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
      })
    },
  },
}
</script>

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