<template>
  <div class="table-container">
    <vab-query-form>
      <el-form
        ref="searchForm"
        :model="searchForm"
        :inline="true"
        @submit.native.prevent
      >
      <vab-query-form-left-panel>

          <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
              v-model="searchForm.stationCode"
              clearable
              placeholder="场站"
            >
              <el-option
                v-for="item in stations"
                :key="item.stationCode"
                :label="item.name"
                :value="item.stationCode"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.inCode" clearable placeholder="公司">
              <el-option
                v-for="item in companys"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              ></el-option>
            </el-select>
          </el-form-item>


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


      <vab-query-form-right-panel :span="1">
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </vab-query-form-right-panel>

      </el-form>
    </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="inCode"
        :formatter="formatCompany"
      />

      <el-table-column
        show-overflow-tooltip
        label="场站名称"
        prop="stationCode"
        :formatter="formatStation"
      />
      <el-table-column show-overflow-tooltip label="日志类型" prop="type" :formatter="formatType"/>

      <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="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'
  import { getCompanyAll } from '@/api/integrationCompany'
  import { getStationAll } from '@/api/station'


  export default {
    name: 'Record',

    data() {
      return {
        tableData: [],
        companys: [],
        types: [{label:"拉取原始数据",value:"PULL_INIT"},
          {label:"推送原始数据",value:"PUSH_INIT"},
          {label:"拉取修正数据",value:"PULL_CORRECT"},
          {label:"交互权限",value:"COM_PERMISSON"},],
        stations: [],
        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.getCompany()
    },
    methods: {
      getCompany() {
        getCompanyAll()
          .then((response) => {
            this.companys = response.data
            this.getStation()
            this.listLoading = false
          })
          .catch(() => {
            this.listLoading = false
          })
      },

      getStation() {
        getStationAll()
          .then((response) => {
            this.stations = response.data
            this.fetchData()
            this.listLoading = false
          })
          .catch(() => {
            this.listLoading = false
          })
      },
      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()
        })
      },
      formatCompany(row, column) {
        for (let i = 0; i < this.companys.length; i++) {
          if (row.inCode == this.companys[i].code) {
            return this.companys[i].name
          }
        }
      },
      formatType(row, column) {
        for (let i = 0; i < this.types.length; i++) {
          if (row.type == this.types[i].value) {
            return this.types[i].label
          }
        }
      },
      formatStation(row, column) {
        const selectedItem = this.stations.find((item) => {
          return item.stationCode === row.stationCode
        })
        return selectedItem.name
      },
      formatDate(row, column) {
        let pro = column.property
        console.log(row[pro])
        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>
