<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-select
          v-model="query.alarmCategory"
          filterable
          clearable
          size="small"
          placeholder="请选择系统"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in alarmCategoryOptions"
            :key="item.dictKey"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
        <el-select
          v-model="query.alarmType"
          filterable
          clearable
          size="small"
          placeholder="请选择分类"
          class="filter-item"
          style="width: 200px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in alarmTypeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <el-select
          v-model="query.alarmLevel"
          filterable
          clearable
          size="small"
          placeholder="请选择等级"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in alarmLevelOptions"
            :key="item.dictKey"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>

        <rrOperation />
      </div>
      <crudOperation :permission="permission" />
    </div>
    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crudCU"
      :title="crud.status.title"
      width="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="报警系统名称" prop="alarmCategory">
          <el-select
            v-model="form.alarmCategory"
            filterable
            size="small"
            placeholder="请选择报警系统"
            class="filter-item"
          >
            <el-option
              v-for="item in alarmCategoryOptions"
              :key="item.dictKey"
              :label="item.dictValue"
              :value="item.dictKey"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警分类" prop="alarmType">
          <el-select
            v-model="form.alarmType"
            filterable
            size="small"
            placeholder="请选择报警分类"
            class="filter-item"
          >
            <el-option
              v-for="item in alarmTypeOptions"
              :key="item.dictKey"
              :label="item.dictValue"
              :value="item.dictKey"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警等级" prop="alarmLevel">
          <el-select
            v-model="form.alarmLevel"
            filterable
            size="small"
            placeholder="请选择报警等级"
            class="filter-item"
          >
            <el-option
              v-for="item in alarmLevelOptions"
              :key="item.dictKey"
              :label="item.dictValue"
              :value="item.dictKey"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警推送人员" prop="SMSTo">
          <el-select
            v-model="form.smsTo"
            filterable
            size="small"
            placeholder="请选择推送人员"
            class="filter-item"
            :multiple="true"
          >
            <el-option
              v-for="item in positionOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </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 prop="alarmCategoryStr" label="报警系统" width="200" />
      <el-table-column prop="alarmTypeStr" label="报警分类" width="200" />
      <el-table-column prop="alarmLevelStr" label="报警等级" width="200" />
      <el-table-column prop="smsToString" label="报警推送人员" />
      <!-- <el-table-column prop="positionSort" label="排序">
          <template slot-scope="scope">
            {{ scope.row.positionSort }}
          </template>
        </el-table-column> -->
      <!-- <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enabled"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              @change="changeEnabled(scope.row, scope.row.enabled)"
            />
          </template>
        </el-table-column> -->
      <!-- <el-table-column prop="baseCreateTime" label="创建日期" /> -->
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'alarmlevel:config:edit', 'alarmlevel:config: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 crudJob from '@/api/system/alarmLevelConfig'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import DateRangePicker from '@/components/DateRangePicker'
import { initData } from '@/api/data'

const defaultForm = {
  id: '',
  alarmCategory: '',
  alarmType: '',
  alarmLevel: '',
  smsTo: [],
  description: '',
}
export default {
  name: 'alarmLevelConfig',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps, DateRangePicker },
  cruds() {
    return CRUD({
      title: '报警等级配置',
      url: 'system/alarmLevelConfig/getPageListJson',
      listOrder: ['id,desc'],
      crudMethod: { ...crudJob },
      optShow: { add: true, edit: true, del: true, reset: true, import: true, export: true },
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'alarmlevel:config:add'],
        edit: ['admin', 'alarmlevel:config:edit'],
        del: ['admin', 'alarmlevel:config:del'],
      },
      rules: {
        positionName: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        positionSort: [{ required: true, message: '请输入序号', trigger: 'blur', type: 'number' }],
      },
      alarmCategoryOptions: [],
      alarmTypeOptions: [],
      alarmLevelOptions: [],
      positionOptions: [],
    }
  },

  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  created() {
    this.getDictDOptions('024')
    this.getDictDOptions('100')
    this.getPositionOptions()
  },
  methods: {
    async getDictDOptions(type) {
      try {
        let res = await initData('system/dataDictDetail/getTypeListJson', {
          dictType: type,
        })
        if (res.code === 20000) {
          if (type == '100') {
            this.alarmCategoryOptions = res.data.content
          }
          if (type == '024') {
            this.alarmLevelOptions = res.data.content
          }
        }
      } catch (error) {
        console.log(error)
      }
    },
    async getPositionOptions(type) {
      try {
        let res = await initData('System/Position/GetJobList', {
          dictType: type,
        })
        if (res.code === 20000) {
          this.positionOptions = res.data.content
        }
      } catch (error) {
        console.log(error)
      }
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}
</style>
