<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-select
          v-model="query.buildId"
          filterable
          size="small"
          placeholder="请选择建筑名称"
          class="filter-item"
          style="width: 160px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in optionsObj.Build"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-input
          v-model="query.name"
          clearable
          size="small"
          placeholder="输入区域名称"
          style="width: 160px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />

        <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="800px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-row>
          <el-col :span="12">
              <el-form-item label="建筑名称" prop="PositioningBuildId">
                <el-select
                  v-model="form.positioningBuildId"
                  filterable
                  size="small"
                  placeholder="请选择建筑名称"
                  class="filter-item"
                  style="width: 280px"
                  @change="handleChangeBuildId"
                >
                  <el-option
                    v-for="item in optionsObj.Build"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input
                  v-model="form.remark"
                  size="small"
                  class="filter-item"
                  style="width: 280px"
                />
              </el-form-item>
          </el-col>
          <el-col :span="12">
              <el-form-item label="模板名称" prop="name">
                <el-input
                  v-model="form.name"
                  size="small"
                  class="filter-item"
                  style="width: 280px"
                />
              </el-form-item>
              <el-form-item label="一键报警" prop="oneKeyAlarm">
                <el-switch
                  v-model="form.oneKeyAlarm"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启动"
                  inactive-text="关闭"
                  :active-value="true"
                  :inactive-value="false"
                >
                </el-switch>
              </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col
              :span="3"
              style="
                font-size: 14px;
                color: #606266;
                font-weight: 600;
                text-align: center;
                padding-top: 6px;
              "
            >
              区域报警
            </el-col>
            <el-col :span="21">
                <el-row>
                  <el-col>
                    <el-form-item label="进入报警" prop="crossInAlarm" label-width="80px">
                      <el-select v-model="areacrossInAlarm" clearable multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in areaAlarmConfigList"
                          :key="item.areaId"
                          :label="item.areaName"
                          :value="item.areaId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col>
                    <el-form-item label="离开报警" prop="acrossOutAlarm" label-width="80px">
                      <el-select v-model="areacrossOutAlarm" clearable  multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in areaAlarmConfigList"
                          :key="item.areaId"
                          :label="item.areaName"
                          :value="item.areaId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col>
                    <el-form-item label="滞留报警" prop="overstayAlarm" label-width="80px">
                      <el-select v-model="areaoverstayAlarm" clearable multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in areaAlarmConfigList"
                          :key="item.areaId"
                          :label="item.areaName"
                          :value="item.areaId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col>
                    <el-form-item label="静止报警" prop="actionlessAlarm" label-width="80px">
                      <el-select v-model="areaactionlessAlarm" clearable multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in areaAlarmConfigList"
                          :key="item.areaId"
                          :label="item.areaName"
                          :value="item.areaId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                </el-row>
            </el-col>
        </el-row>
        <el-row>
            <el-col
              :span="3"
              style="
                font-size: 14px;
                color: #606266;
                font-weight: 600;
                text-align: center;
                padding-top: 6px;
              "
            >
              围栏报警
            </el-col>
            <el-col :span="21">
                <el-row>
                  <el-col>
                    <el-form-item label="进入报警" prop="crossInAlarm" label-width="80px" >
                      <el-select v-model="railcrossInAlarm" clearable multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in railAlarmConfigList"
                          :key="item.railId"
                          :label="item.railName"
                          :value="item.railId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col>
                    <el-form-item label="离开报警" prop="acrossOutAlarm" label-width="80px">
                      <el-select v-model="railcrossOutAlarm" clearable multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in railAlarmConfigList"
                          :key="item.railId"
                          :label="item.railName"
                          :value="item.railId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col>
                    <el-form-item label="滞留报警" prop="overstayAlarm" label-width="80px">
                      <el-select v-model="railoverstayAlarm" clearable multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in railAlarmConfigList"
                          :key="item.railId"
                          :label="item.railName"
                          :value="item.railId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col>
                    <el-form-item label="静止报警" prop="actionlessAlarm" label-width="80px">
                      <el-select v-model="railactionlessAlarm" clearable multiple placeholder="请选择" style="width:550px">
                        <el-option
                          v-for="item in railAlarmConfigList"
                          :key="item.railId"
                          :label="item.railName"
                          :value="item.railId">
                        </el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>

                </el-row>
            </el-col>
        </el-row>
      </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
        v-for="item in alarmModelTableOptions"
        :key="item.prop"
        :label="item.label"
        :prop="item.prop"
        :width="item.width"
        :align="item.align"
        :fixed="item.fixed"
        :sortable="item.sortable"
      ></el-table-column>
      <!--   编辑与删除   -->
      <el-table-column
        v-if="checkPer(['admin', 'alarmModel:edit', 'alarmModel: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 crudAlarmModel from '@/api/positioning/alarmModel'
import { getOptions } from '@/api/positioning/public'
import { initData } from '@/api/data'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import { crudOperation, rrOperation, udOperation, pagination } from '@crud'
import crudComps from '@crud'
import { alarmModelTableOptions } from '../tableConfig'


const defaultForm = {
  id: null,
  positioningBuildId: '',
  name: '',
  remark: '',
  alarmConfigList: [],
  oneKeyAlarm:true,
  
  riskLevel: null,
  enableLog: false,
  crossAlarm: false,
  overstayAlarm: false,
  overstayTime: 1,
  overmanAlarm: false,
  overmanCount: 0,
  overmanTime: 0,
  undermanAlarm: false,
  undermanCount: 0,
  undermanTime: 0,
  actionlessAlarm: false,
  actionlessTime: 1,
  effectiveTimeJson: '',
  railIds: []
}

export default {
  name: 'AlarmModel',
  directives: { permission },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  components: { ...crudComps },
  cruds() {
    return CRUD({
      title: '报警模板',
      url: 'positioning/alarmModel/getPageListJson',
      listOrder: [],
      crudMethod: { ...crudAlarmModel },
    })
  },
  computed: {
    crudCU() {
      return this.crud.status.cu > 0
    },
  },
  data() {
    return {
      alarmModelTableOptions,
      permission: {
        add: ['admin', 'alarmModel:add'],
        edit: ['admin', 'alarmModel:edit'],
        del: ['admin', 'alarmModel:del'],
      },
      rules: {
        positioningBuildId: [{ required: true, message: '请选择建筑名称', trigger: 'blur' }],
        areaName: [{ required: true, message: '请输入区域名称', trigger: 'blur' }],
        railIds: [{ required: true, message: '请选择围栏', trigger: 'blur' }],
      },
      optionsObj: { Build: [] },
      railAlarmConfigList: [],
      areaAlarmConfigList: [],
      railcrossInAlarm: [],
      railcrossOutAlarm: [],
      railoverstayAlarm: [],
      railactionlessAlarm: [],
      areacrossInAlarm: [],
      areacrossOutAlarm: [], 
      areaoverstayAlarm: [],
      areaactionlessAlarm: [],
    }
  },
  created() {
    this.getOptions()
    this.getRailData()
    this.getAreaData()
  },
  methods: {
    // 提交前的验证
    [CRUD.HOOK.afterValidateCU]() {
      console.log(this.areacrossInAlarm)

    },
    [CRUD.HOOK.beforeToAdd]() {
      this.getRailData()
      this.getAreaData()
    },
    [CRUD.HOOK.beforeToEdit]() {
      // console.log(this.form)
      if (this.form.alarmConfigList.length) {
        this.form.alarmConfigList.forEach((item) => {
          //围栏报警设置
          if(item.crossInAlarm && item.railId) this.railcrossInAlarm.push(item.railId)
          if(item.crossOutAlarm && item.railId) this.railcrossOutAlarm.push(item.railId)
          if(item.overstayAlarm && item.railId) this.railoverstayAlarm.push(item.railId)
          if(item.actionlessAlarm && item.railId) this.railactionlessAlarm.push(item.railId)
          //区域报警设置
          if(item.crossInAlarm && item.areaId) this.areacrossInAlarm.push(item.areaId)
          if(item.crossOutAlarm && item.areaId) this.areacrossOutAlarm.push(item.areaId)
          if(item.overstayAlarm && item.areaId) this.areaoverstayAlarm.push(item.areaId)
          if(item.actionlessAlarm && item.areaId) this.areaactionlessAlarm.push(item.areaId)
        })        
      } else {
        //this.getRailData()
        //this.getAreaData()
        //围栏报警设置
        this.railcrossInAlarm = []
        this.railcrossOutAlarm = []
        this.railoverstayAlarm = []
        this.railactionlessAlarm = []
        //区域报警设置
        this.areacrossInAlarm = []
        this.areacrossOutAlarm = []
        this.areaoverstayAlarm = []
        this.areaactionlessAlarm = []
      }
    },
    getOptions() {
      Object.keys(this.optionsObj).forEach(async (key) => {
        try {
          let res = await getOptions(key)
          if (res.code === 20000) {
            this.optionsObj[key] = res.data.content
            if (key == 'Build') {
              this.query.buildId = this.optionsObj.Build[0].value
              this.crud.toQuery()
            }
          }
        } catch (error) {
          console.log(error)
        }
      })
    },
    //获取弹窗内的围栏信息
    async getRailData(buildId) {
      try {
        let res = await initData('positioning/rail/getPageListJson', {
          buildId: buildId,
          PageIndex: 0,
          PageSize: 0,
        })
        const data = res.data.content
        this.railAlarmConfigList = data.map((item) => {
          return {
            railId: item.id,
            railName: item.railName,
            crossInAlarm: false,
            crossOutAlarm: false,
            overstayAlarm: false,
            actionlessAlarm: false,
          }
        })
        //console.log(this.railList)
      } catch (error) {
        console.log(error)
      }
    },
    //获取弹窗内的区域信息
    async getAreaData() {
      try {
        const res = await initData('positioning/area/getPageListJson', {
          PageIndex: 0,
          PageSize: 0,
        })
        const data = res.data.content
        this.areaAlarmConfigList = data.map((item) => {
          return {
            areaId: item.id,
            areaName: item.areaName,
            crossInAlarm: false,
            crossOutAlarm: false,
            overstayAlarm: false,
            actionlessAlarm: false,
          }
        })
      } catch (error) {
        console.log(error)
      }
    },
    async handleChangeBuildId(val) {
      this.form.railIds = []
      if (val) await this.getRailData(val)
    }
  },
}
</script>

<style></style>
