<!--
 * @Author: zwq
 * @Date: 2020-12-29 15:41:11
 * @LastEditors: gtz
 * @LastEditTime: 2022-07-25 10:29:20
 * @Description:
-->
<template>
  <div class="app-container">
    <top-title />
    <head-form
      :form-config="headFormConfig"
      @headBtnClick="btnClick"
    />
    <base-table
      :top-btn-config="topBtnConfig"
      :table-config="tableProps"
      :table-data="list"
      :page="listQuery.current"
      :limit="listQuery.size"
      :is-loading="listLoading"
      @clickTopBtn="clickTopBtn"
    >
      <method-btn
        slot="handleBtn"
        :width="trueWidth"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList()"
    />
    <alarmInfo-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>
import TopTitle from '@/components/TopTitle'
import HeadForm from '@/components/basicData/HeadForm'
import i18n from '@/lang'
import { alarmInfoList, alarmInfoDelete } from '@/api/basicData/AlarmManagement/alarmInfo'
import { alarmLevelList } from '@/api/basicData/AlarmManagement/alarmLevel'
import { alarmTypeList } from '@/api/basicData/AlarmManagement/alarmType'
import alarmInfoAdd from './components/alarmInfo-add.vue'
import BaseTable from '@/components/BaseTable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import { timeFormatter } from '@/filters'
// , handleLimit
/**
 * 表格表头配置项 TypeScript接口注释
 * tableConfig<ConfigItem> = []
 *
 * Interface ConfigItem = {
 *  prop: string,
 *  label: string,
 *  width: string,
 *  align: string,
 *  subcomponent: function,
 *  filter: function
 * }
 *
 *
 */
const topBtnConfig = [
  {
    type: 'add',
    btnName: 'btn.add'
  }
]
const tableBtn = [
  {
    type: 'edit',
    btnName: 'btn.edit'
  },
  {
    type: 'delete',
    btnName: 'btn.delete'
  }
]
const tableProps = [
  {
    prop: 'createTime',
    label: i18n.t('module.basicData.factory.createTime'),
    filter: timeFormatter,
    align: 'center'
  },
  {
    prop: 'code',
    label: i18n.t('module.basicData.alarmManagement.AlarmCode'),
    align: 'center'
  },
  {
    prop: 'name',
    label: i18n.t('module.basicData.alarmManagement.AlarmName'),
    align: 'center'
  },
  {
    prop: 'alarmGrade',
    label: i18n.t('module.basicData.alarmManagement.AlarmLevel'),
    align: 'center'
  },
  {
    prop: 'alarmType',
    label: i18n.t('module.basicData.alarmManagement.AlarmType'),
    align: 'center'
  },
  // {
  //   prop: 'alarmContent',
  //   label: i18n.t('module.basicData.alarmManagement.AlarmContent'),
  //   align: 'center',
  //   filter: handleLimit
  // },
  {
    prop: 'remark',
    label: i18n.t('module.basicData.visual.Remarks'),
    align: 'center'
  }
]

export default {
  name: 'AlarmInfo',
  components: { TopTitle, HeadForm, Pagination, BaseTable, MethodBtn, alarmInfoAdd },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      topBtnConfig,
      addOrUpdateVisible: false,
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 10,
        alarmType: '',
        code: '',
        alarmGrade: ''
      },
      headFormConfig: [
        {
          type: 'input',
          label: i18n.t('module.basicData.alarmManagement.AlarmCode'),
          placeholder: this.$t('module.basicData.alarmManagement.AlarmCode'),
          param: 'code'
        },
        {
          type: 'select',
          label: this.$t('module.basicData.alarmManagement.AlarmType'),
          selectOptions: [],
          param: 'alarmType',
          defaultSelect: ''
        },
        {
          type: 'select',
          label: this.$t('module.basicData.alarmManagement.AlarmLevel'),
          selectOptions: [],
          param: 'alarmGrade',
          defaultSelect: ''
        },
        {
          type: 'button',
          btnName: 'btn.search',
          name: 'search',
          color: 'primary'
        }
      ],
      headFormValue: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick(raw) {
      console.log(raw)
      if (raw.type === 'delete') {
        this.$confirm(`${this.$t('module.basicData.visual.TipsBefore')}[${raw.data.name}]?`, this.$t('module.basicData.visual.Tips'), {
          confirmButtonText: this.$t('module.basicData.visual.confirmButtonText'),
          cancelButtonText: this.$t('module.basicData.visual.cancelButtonText'),
          type: 'warning'
        }).then(() => {
          alarmInfoDelete(raw.data.id).then(response => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getList()
              }
            })
          })
        }).catch(() => {})
      } else {
        this.addNew(raw.data.id)
      }
    },
    getList() {
      const lQuery = {
        current: 1,
        size: 999
      }
      alarmTypeList(lQuery).then(response => {
        this.headFormConfig[1].selectOptions = response.data.records.map(item => {
          return { id: item.name, name: item.name }
        })
      })
      alarmLevelList(lQuery).then(response => {
        this.headFormConfig[2].selectOptions = response.data.records.map(item => {
          return { id: item.alarmGrade, name: item.alarmGrade }
        })
      })
      this.listLoading = true
      this.listQuery.code = this.headFormValue.code
      this.listQuery.alarmType = this.headFormValue.alarmType
      this.listQuery.alarmGrade = this.headFormValue.alarmGrade
      alarmInfoList(this.listQuery).then(response => {
        if (response.data.records) {
          this.list = response.data.records
        } else {
          this.list.splice(0, this.list.length)
        }
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    clickTopBtn(val) {
      if (val === 'add') {
        this.addNew()// 新增
      }
    },
    btnClick(val) {
      this.headFormValue = val
      // 如果点击的是搜索栏的其他按钮在这里继续写判断
      if (this.headFormValue.btnName === 'search') {
        this.getList()
      }
    }
  }
}
</script>
