<template>
  <div class="log">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <el-form-item :label="$t('system:log:table:version')" prop="Version">
          <el-input v-model="searchData.Version" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:log:table:versionUS')" prop="Version">
          <el-input v-model="searchData.Version_US" clearable />
        </el-form-item>
        <el-form-item
          :label="$t('system:log:table:UpdateContent')"
          prop="UpdateContent"
        >
          <el-input v-model="searchData.UpdateContent" clearable />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getList"
          >
            {{ $t('common:button:search') }}
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
          >
            {{ $t('common:button:new') }}
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        v-adaptive="{ bottomOffset: 45 }"
        height="100"
        :data="list"
        class="table"
        @sort-change="handleSortByColumn"
        :default-sort="{ prop: 'PublishTime', order: 'descending' }"
      >
        <el-table-column type="index" width="50" />
        <el-table-column
          :label="$t('system:log:table:version')"
          prop="Version"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('system:log:table:versionUS')"
          prop="Version_US"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('system:log:table:UpdateContent')"
          prop="UpdateContent"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('system:log:table:State')"
          prop="State"
          min-width="150"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ $fd('PublishStatus', scope.row.State) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:log:table:PublishTime')"
          prop="PublishTime"
          min-width="90"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{
              scope.row.PublishTime
                ? moment(scope.row.PublishTime).format('YYYY-MM-DD')
                : ''
            }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:log:table:IsCurrentVersion')"
          prop="IsCurrentVersion"
          min-width="150"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ $fd('YesOrNo', scope.row.IsCurrentVersion) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('common:action:action')"
          fixed="right"
          prop=""
          min-width="150"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleEdit(scope.row)"
            >
              {{ $t('common:button:edit') }}
            </el-button>
            <el-button
              type="text"
              @click="handleDelete(scope.row)"
            >
              {{ $t('common:button:delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="text-align: right; margin-top: 5px">
        <pagination
          :total="total"
          :page.sync="searchData.PageIndex"
          :limit.sync="searchData.PageSize"
          @pagination="getList"
        />
      </div>
      <log-form ref="LogForm" @getList="getList" />
    </div>
  </div>
</template>
<script>
import { getPublishLogList, deletePublishLog } from '@/api/admin'
import Pagination from '@/components/Pagination'
import LogForm from './components/LogForm.vue'
import moment from 'moment'
const searchDataDefault = () => {
  return {
    Version: null,
    Version_US: null,
    UpdateContent: null,
    Asc: false,
    SortField: 'PublishTime',
    PageIndex: 1,
    PageSize: 20,
  }
}
export default {
  components: { Pagination, LogForm },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getPublishLogList(this.searchData)
        .then((res) => {
          this.loading = false
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          this.loading = false
        })
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['LogForm'].openDialog('新增', {})
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['LogForm'].openDialog('编辑', row)
      })
    },
    // 重置列表查询
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除？', {
        type: 'warning',
        distinguishCancelAndClose: true,
      }).then(() => {
        this.loading = true
        deletePublishLog(row.Id)
          .then((res) => {
            this.loading = false
            if (res.IsSuccess) {
              this.getList()
              this.$message.success(
                this.$t('common:message:deletedSuccessfully')
              )
            }
          })
          .catch(() => {
            this.loading = false
          })
      })
    },
    // 排序
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss">
.log {
  height: 100%;
  box-sizing: border-box;
  display: flex;
  padding: 10px;
  border-radius: 5px;
  .left {
    display: flex;
    flex-direction: column;
    width: 0;
    flex-grow: 4;
    // border-right: 1px solid #ccc;
    .filter-container {
      display: flex;
      align-items: center;
      margin: 5px;
    }
    .data-table {
      flex: 1;
      padding: 5px 0px;
    }
    .pagination-container {
      text-align: right;
    }
  }
}
</style>
