<!-- 规则列表 -- 表格 -->
<template>
  <div class="layout-pd">
    <el-row>
      <!--操作-->
      <el-col :xs="24">
        <el-card class="mt8" shadow="hover">
          <el-form :model="Data.Filter" @submit.stop.prevent>
            <el-form-item prop="name" style="width: 100%">
              <el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="6" class="mb20">
                <el-form-item label="规则名称">
                  <el-input v-model="Data.time1" placeholder="请输入规则名称" clearable></el-input>
                </el-form-item>
              </el-col>
            </el-form-item>
          </el-form>

          <hr>

          <!-- 按钮 -->
          <el-row justify="space-between" class="submit-button">
            <el-row>
              <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
              <el-button type="primary" icon="ele-RefreshRight" @click="onReset"> 重置 </el-button>
            </el-row>
            <el-row>
              <el-button type="primary" icon="ele-RefreshRight" @click="addAlarmRules"> 添加 </el-button>
            </el-row>
          </el-row>
        </el-card>
      </el-col>
      <!--表格-->
      <el-col :xs="24">
        <el-card style="height: 70vh" class="my-fill mt8" shadow="hover">
          <el-table ref="multipleTableRef" v-loading="Data.loading" stripe :data="Data.tableModel" row-key="id"
            style="width: 100%">
            <el-table-column v-for="column in Data.dynamicColumns" :key="column.prop" :prop="column.prop"
              :label="column.label" />
          </el-table>
          <div class="my-flex my-flex-end" style="margin-top: 20px">
            <el-pagination v-model:currentPage="Data.pageInput.currentPage" v-model:page-size="Data.pageInput.pageSize"
              :total="Data.total" :page-sizes="[5, 10, 20, 50, 100]" small background @size-change="onSizeChange"
              @current-change="onCurrentChange" layout="total, sizes, prev, pager, next, jumper" />
          </div>
        </el-card>
      </el-col>
    </el-row>
    <EditDialog ref="editDialogRef" />

  </div>

</template>

<script setup lang="ts" name="partsManagement/oilGun">
import { defineAsyncComponent, onMounted, reactive, ref, watch } from "vue";
import { alarmRluesFilterModel_SearchFilter, alarmRluesFilterModel, PageInputAlarmRluesFilterModel } from "/@/api/admin/AlarmService/alarmRulesDto";
import { alarmRulesApi } from "/@/api/admin/AlarmService/alarmRulesApi";

/**页面对象 */
const Data = reactive({
  time1: '',
  /** 选择框列表 */
  statusList: [] as any,
  supplierList: [] as any,
  /**加载显示 */
  loading: false,
  /**条件查询模块 */
  Filter: {
    /** 规则名称 */
    ruleName: ""
  } as alarmRluesFilterModel_SearchFilter,
  /**表格信息 */
  tableModel: [] as alarmRluesFilterModel,
  /**动态表头 */
  dynamicColumns: [
    { prop: 'ruleName', label: '规则名称' },
    { prop: 'companyName', label: '角色' },
    { prop: 'pushMethod', label: '推送方式' },
    { prop: 'triggerMethod', label: '触发方式' },
    { prop: 'regular', label: '正则匹配' },
    { prop: 'taskPriority', label: '优先级' },
    { prop: 'isExclusive', label: '是否互斥' },
    { prop: 'stationName', label: '推送方式' },
    { prop: 'pushTemplateMappingID', label: '模板' },
    { prop: 'remark', label: '备注' },

  ],
  /**分页总数 */
  total: 0,
  /**分页标识 */
  pageInput: {
    currentPage: 1,
    pageSize: 10,
  } as PageInputAlarmRluesFilterModel,
})

/**引入组件*/
const EditDialog = defineAsyncComponent(() => import('./components/add-alarmRules.vue'))
const editDialogRef = ref()

onMounted(() => {
  init()
})

// // 条件查询
const init = async () => {
  Data.loading = true
  const res: any = await new alarmRulesApi().getData({ ...Data.pageInput, filter: Data.Filter })
  console.log(res)
  Data.tableModel = res?.data ?? []
  // Data.total = res?.data?.length ?? 0   // 接口中没有总的数据量
  Data.loading = false
  pushMethodChange()
}

const onQuery = () => {
  init()
  console.log("查询")
}

/**重置查询条件 */
const resetQuery = () => {
  Data.Filter.ruleName = ''
}
/**重置 */
const onReset = () => {
  resetQuery()
  init()
}

/** 
 * 添加规则
*/
const addAlarmRules = (() => {
  editDialogRef.value.openDialog()
  init()
})

/**
* 页条变化
* @param val
*/
const onSizeChange = (val: number) => {
  Data.pageInput.pageSize = val
  init()
}

/**
 * 页数 变化
 * @param val
 */
const onCurrentChange = (val: number) => {
  Data.pageInput.currentPage = val
  init()
}

/** 数据转换 */
const pushMethodChange = (() => {
  // 推送方式
  Data.tableModel.forEach((item) => {
    const pushList = item['pushMethod'].split(',')
    for (var i = 0; i < pushList.length; i++) {
      if (pushList[i] == 'wx') {
        pushList[i] = "微信"
      } else if (pushList[i] == 'email') {
        pushList[i] = "电子邮箱"
      }
    }
    item['pushMethod'] = pushList.toString()

    // 是否互斥
    if (item['isExclusive'] == true) {
      item['isExclusive'] = "是"
    } else {
      item['isExclusive'] = "否"
    }

    // 触发方式
    if (item['triggerMethod'] == '0') {
      item['triggerMethod'] = '其中之一条件满足即触发'
    } else if (item['triggerMethod'] == '1') {
      item['triggerMethod'] = '全部满足时触发'
    }
  })
})




</script>

<style scoped lang="scss">
@import '/@/theme/tableStyle.scss';
</style>