<template>
  <div class="log">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <el-form-item label="国际化类型">
          <el-select
            v-model="searchData.InternationalizationType"
            clearable
            filterable
            style="width:130px;"
          >
            <el-option
              v-for="item of $d.InternationalizationType"
              :key="'InternationalizationType'+item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="功能模块/服务名">
          <el-input
            v-model="searchData.Description"
            size="small"
            clearable
            style="width:130px;"
          />
        </el-form-item>
        <el-form-item label="标识">
          <el-input
            v-model="searchData.Code"
            size="small"
            clearable
            style="width:130px;"
          />
        </el-form-item>
        <el-form-item label="中文值">
          <el-input
            v-model="searchData.ValueCN"
            size="small"
            clearable
            style="width:130px;"
          />
        </el-form-item>
        <el-form-item label="英文值">
          <el-input
            v-model="searchData.Value"
            size="small"
            style="width:130px;"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchData.State" clearable filterable style="width:130px;">
            <el-option
              v-for="item of $d.InternationalizationKeyState"
              :key="'InternationalizationKeyState'+item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="getList"
          >
            查询
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >
            新增
          </el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleBatchAdd"
          >
            批量新增
          </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"
      >
        <el-table-column
          type="index"
          width="50"
        />
        <el-table-column
          label="国际化类型"
          prop="InternationalizationType"
          width="120"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.InternationalizationType === 0" type="primary">
              {{ $fd('InternationalizationType', scope.row.InternationalizationType) }}
            </el-tag>
            <el-tag v-else-if="scope.row.InternationalizationType === 1" type="warning">
              {{ $fd('InternationalizationType', scope.row.InternationalizationType) }}
            </el-tag>
            <el-tag v-else>
              {{ $fd('InternationalizationType', scope.row.InternationalizationType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="功能模块/服务名"
          prop="Description"
          min-width="100"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          label="标识"
          prop="Code"
          min-width="150"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          label="中文值"
          prop="ValueCN"
          min-width="150"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          label="英文值"
          prop="Value"
          min-width="150"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          label="状态"
          prop="State"
          width="100"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.State === 0" type="danger">
              {{ $fd('InternationalizationKeyState', scope.row.State) }}
            </el-tag>
            <el-tag v-else-if="scope.row.State === 1" type="primary">
              {{ $fd('InternationalizationKeyState', scope.row.State) }}
            </el-tag>
            <el-tag v-else>
              {{ $fd('InternationalizationKeyState', scope.row.State) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="更新时间"
          prop="UpdateTime"
          width="150"
          show-overflow-tooltip
          sortable="custom"
        />

        <el-table-column
          label="操作"
          fixed="right"
          prop=""
          min-width="120"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit-outline"
              @click="handleEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              :disabled="scope.row.State === 1"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >
              删除
            </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>
      <i18n-form
        ref="i18nForm"
        @getList="getList"
      />
      <BatchAddForm ref="batcnAddForm" @getList="getList" />
    </div>
  </div>
</template>
<script>
import { getInternationalizationList, deleteInternationalization } from '@/api/admin'
import Pagination from '@/components/Pagination'
import I18nForm from './components/I18nForm.vue'
import BatchAddForm from './components/BatchAddForm.vue'
import moment from 'moment'
const searchDataDefault = () => {
  return {
    Description: null,
    Value: null,
    ValueCN: null,
    InternationalizationType: null,
    State: null,
    Asc: true,
    SortField: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'I18n',
  components: { Pagination, I18nForm, BatchAddForm },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getInternationalizationList(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['i18nForm'].openDialog('新增', {})
      })
    },
    handleBatchAdd() {
      this.$nextTick(() => {
        this.$refs['batcnAddForm'].openDialog('批量新增', {})
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['i18nForm'].openDialog('编辑', row)
      })
    },
    // 重置列表查询
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除？', {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          this.loading = true
          deleteInternationalization(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>
