<template>
  <div class="table-container">
    <vab-query-form>

      <vab-query-form-left-panel>
        <el-form
          ref="searchForm"
          :model="searchForm"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-date-picker
              v-model="times"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
            ></el-date-picker>
          </el-form-item>


          <el-form-item>
            <el-select clearable v-model="searchForm.type" placeholder="请选择">
              <el-option
                v-for="item in types"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              native-type="submit"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>

        </el-form>

      </vab-query-form-left-panel>


      <vab-query-form-right-panel :span="1">

      </vab-query-form-right-panel>


    </vab-query-form>

    <el-table
      ref="table"
      v-loading="listLoading"
      :data="tableData"
      :element-loading-text="elementLoadingText"
      :height="height"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column show-overflow-tooltip label="序号" width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column show-overflow-tooltip label="日志类型" prop="type" :formatter="formatType"/>
      <el-table-column show-overflow-tooltip label="内容" prop="content" :formatter="formatContent"/>

      <el-table-column show-overflow-tooltip label="状态" prop="state" />
      <el-table-column show-overflow-tooltip label="状态内容" prop="stateContent" />
      <el-table-column show-overflow-tooltip label="场站编码" prop="stationCode" />
      <el-table-column
        show-overflow-tooltip
        label="标记时间"
        prop="time"
        :formatter="formatDate"
      />
      <el-table-column
        show-overflow-tooltip
        label="创建时间"
        prop="createTime"
        :formatter="formatDate"
      />

      <el-table-column show-overflow-tooltip label="操作" width="180px">
        <template #default="{ row }">

          <el-button type="text" @click="handleDelete(row)">
            <el-tag type="danger">删除</el-tag>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="page.currentPage"
      :layout="layout"
      :page-size="page.pageSize"
      :total="page.total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
  import { fetchList ,delObj} from '@/api/record'

  export default {
    name: 'Record',

    data() {
      return {
        tableData: [],
        types: [
          {label:"修正DQ数据",value:"PULL_CORRECT"},
          {label:"交互权限",value:"COM_PERMISSON"},
          {label:"修正DQ文件",value:"PULL_CORRECT_JY"},
          {label:"修正数据",value:"CORRECT_DATA"},
          {label:"回传数据",value:"BACK_DATA"},
          {label:"回传文件解析",value:"BACK_DATA_FILE"},
        ],
        contents:[
          {label:"回传预测数据",value:"BACK_FORE_ALL"},
          {label:"回传统计数据",value:"BACK_STAT_ALL"},
          {label:"下发超短期修正",value:"CORRULTRSHOR"},
          {label:"下发检修计划",value:"REPAPLAN"},
          {label:"文件解析",value:"BACK_FILE_ANALYSIS"},
        ],
        searchForm: {},
        times: [
          this.dateFormat("yyyy-MM-dd HH:mm:ss",new Date(new Date().setHours(0, 0, 0, 0))),
          this.dateFormat("yyyy-MM-dd HH:mm:ss",new Date(new Date().setHours(23, 59, 59, 59))),
        ],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        elementLoadingText: '正在加载...',
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      async fetchData() {
        this.listLoading = true
        this.searchForm.startTime = null
        this.searchForm.endTime = null
        if(this.times !=null){
          this.searchForm.startTime = this.times[0]
          this.searchForm.endTime = this.times[1]
        }
        fetchList(
          Object.assign(
            {
              current: this.page.currentPage,
              size: this.page.pageSize,
            },
            this.searchForm
          )
        )
          .then((response) => {
            this.tableData = response.data.records
            this.page.total = response.data.total
            this.listLoading = false
          })
          .catch(() => {
            this.listLoading = false
          })
      },

      handleSizeChange(val) {
        this.page.pageSize = val
        this.page.currentPage = 1
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.page.currentPage = val
        this.fetchData()
      },
      handleQuery() {
        for (var v in this.searchForm) {
          if (this.searchForm[v] == '') {
            delete this.searchForm[v]
          }
        }

        this.page.currentPage = 1
        this.fetchData()
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          await delObj(row.id)
          this.$baseMessage('删除成功', 'success')
          this.fetchData()
        })
      },

      formatType(row, column) {
        for (let i = 0; i < this.types.length; i++) {
          if (row.type == this.types[i].value) {
            return this.types[i].label
          }
        }
      },

      formatContent(row, column) {
        for (let i = 0; i < this.contents.length; i++) {
          if (row.content == this.contents[i].value) {
            return this.contents[i].label
          }
        }
      },

      formatDate(row, column) {
        let pro = column.property
        return row[pro]
      },
      dateFormat(fmt, date) {
        let ret
        const opt = {
          'y+': date.getFullYear().toString(), // 年
          'M+': (date.getMonth() + 1).toString(), // 月
          'd+': date.getDate().toString(), // 日
          'H+': date.getHours().toString(), // 时
          'm+': date.getMinutes().toString(), // 分
          's+': date.getSeconds().toString(), // 秒
          // 有其他格式化字符需求可以继续添加，必须转化成字符串
        }
        for (let k in opt) {
          ret = new RegExp('(' + k + ')').exec(fmt)
          if (ret) {
            fmt = fmt.replace(
              ret[1],
              ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
            )
          }
        }
        return fmt
      },
    },
  }
</script>

<style>
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
