<template>
  <div class="log">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <el-form-item :label="$t('system:l18n:search:InternationalizationType')">
          <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="$t('system:l18n:search:Module')">
          <el-input v-model="searchData.Module" clearable style="width: 130px" />
        </el-form-item>
        <el-form-item :label="$t('system:l18n:search:Code')">
          <el-input v-model="searchData.Code" clearable style="width: 130px" />
        </el-form-item>
        <el-form-item :label="$t('system:l18n:search:ValueCN')">
          <el-input v-model="searchData.ValueCN" clearable style="width: 130px" />
        </el-form-item>
        <el-form-item :label="$t('system:l18n:search:Value')">
          <el-input v-model="searchData.Value" style="width: 130px" clearable />
        </el-form-item>
        <el-form-item :label="$t('system:l18n:search:State')">
          <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 :label="$t('system:l18n:search:Description')">
          <el-input v-model="searchData.Description" clearable style="width: 130px" />
        </el-form-item>
        <el-form-item :label="$t('system:l18n:search:PublishVersion')">
          <el-select v-model="searchData.PublishLogId" clearable filterable style="width: 130px">
            <el-option v-for="item in PublishVersionList" :key="item.Id" :label="item.Version" :value="item.Id" />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('system:l18n:form:createTime')">
          <el-date-picker v-model="datetimerange_createTime" type="datetimerange"
            :default-time="['00:00:00', '23:59:59']" :start-placeholder="$t('system:l18n:search:beginTime')"
            :end-placeholder="$t('system:l18n:search:endTime')" value-format="yyyy-MM-dd HH:mm:ss"
            @change="(val) => handleDatetimeChange(val, 'createTime')" style="width: 250px" />
        </el-form-item>
        <el-form-item :label="$t('system:l18n:form:uploadTime')">
          <el-date-picker v-model="datetimerange_updateTime" type="datetimerange"
            :default-time="['00:00:00', '23:59:59']" :start-placeholder="$t('system:l18n:search:beginTime')"
            :end-placeholder="$t('system:l18n:search:endTime')" value-format="yyyy-MM-dd HH:mm:ss"
            @change="(val) => handleDatetimeChange(val, 'updateTime')" style="width: 250px" />
        </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-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
          <el-button type="primary" @click="handleAdd">
            {{ $t('common:button:new') }}
          </el-button>
          <el-button type="primary" @click="handleBatchAdd">
            {{ $t('system:l18n:button:batchAdd') }}
          </el-button>
          <el-button type="primary" :disabled="selectTableList.length <= 0" @click="handleBatchUpdate">
            {{ $t('system:l18n:button:batchUpdate') }}
          </el-button>
          <el-button type="primary" :disabled="selectTableList.length <= 0" @click="handleBatchUpdateToChange">
            {{ $t('system:l18n:button:batchEdit') }}
          </el-button>
          <el-button type="primary" @click="exportTable">
            {{ $t('common:button:export') }}
          </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" @selection-change="handleSelectionChange"><el-table-column type="selection"
          width="50" />
        <el-table-column type="index" width="50" />
        <el-table-column :label="$t('system:l18n:search:InternationalizationType')" 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="$t('system:l18n:search:Module')" prop="Module" min-width="140" show-overflow-tooltip
          sortable="custom" />
        <el-table-column :label="$t('system:l18n:search:Code')" prop="Code" min-width="150" show-overflow-tooltip
          sortable="custom" />
        <el-table-column :label="$t('system:l18n:search:ValueCN')" prop="ValueCN" min-width="150" show-overflow-tooltip
          sortable="custom" />
        <el-table-column :label="$t('system:l18n:search:Value')" prop="Value" min-width="150" show-overflow-tooltip
          sortable="custom" />
        <el-table-column :label="$t('system:l18n:search:State')" 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="$t('system:l18n:search:Description')" prop="Description" min-width="110"
          show-overflow-tooltip sortable="custom" />
        <el-table-column :label="$t('system:l18n:search:PublishVersion')" prop="Version" min-width="100"
          show-overflow-tooltip sortable="custom" />
        <el-table-column :label="$t('system:l18n:form:uploadTime')" prop="UpdateTime" width="150" show-overflow-tooltip
          sortable="custom" />
        <el-table-column :label="$t('system:l18n:form:createTime')" prop="CreateTime" width="150" show-overflow-tooltip
          sortable="custom" />
        <el-table-column :label="$t('common:action:action')" fixed="right" prop="" min-width="120"
          show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">
              {{ $t('common:button:edit') }}
            </el-button>
            <el-button :disabled="scope.row.State === 1" 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>
      <i18n-form ref="i18nForm" @getList="getList" @batch="batch" />
      <BatchAddForm ref="batcnAddForm" @getList="getList" />
    </div>
  </div>
</template>
<script>
import {
  getInternationalizationList,
  deleteInternationalization,
  getPublishVersionSelect,
  batchUpdateInternationalInfo,
} from '@/api/admin'
import { GetInternationalizationList_Export } from '@/api/export'
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,
    Module: null,
    PublishLogId: null,
    Asc: true,
    SortField: '',
    PageIndex: 1,
    PageSize: 20,
    BeginCreateTime: null,
    EndCreatTime: null,
    BeginUpdateTime: null,
    EndUpdateTime: null,
  }
}
export default {
  name: 'I18n',
  components: { Pagination, I18nForm, BatchAddForm },
  data() {
    return {
      moment,
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      PublishVersionList: [],
      selectTableList: [],
      datetimerange_createTime: [],
      datetimerange_updateTime: [],
    }
  },
  mounted() {
    this.getList()
    this.getPublishVersionSelect()
    this.$EventBus.$on('il8nUpdate', (data) => {
      this.getList()
    })
  },
  methods: {
    // 获取迭代
    async getPublishVersionSelect() {
      try {
        let res = await getPublishVersionSelect()
        if (res.IsSuccess) {
          this.PublishVersionList = res.Result
        }
      } catch (err) {
        console.log(err)
      }
    },
    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
        })
    },
    // 批量更新
    handleBatchUpdate() {
      this.$nextTick(() => {
        this.$refs['i18nForm'].openDialog(
          this.$t('system:l18n:button:batchUpdate'),
          {},
          this.PublishVersionList,
          'batch'
        )
      })
    },
    // 批量编辑国际化内容
    handleBatchUpdateToChange() {
      this.$openI18n(this.selectTableList)
    },
    async batch(row) {
      let { PublishLogId, State } = row
      let data = {
        PublishLogId,
        State,
        IdList: this.selectTableList.map((item) => item.Id),
      }
      try {
        let res = await batchUpdateInternationalInfo(data)
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:updatedSuccessfully'))
          this.getList()
          this.$nextTick(() => {
            this.$refs['i18nForm'].handleCancle()
          })
        }
      } catch (err) {
        console.log(err)
      }
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs['i18nForm'].openDialog(
          this.$t("common:button:new"),
          {},
          this.PublishVersionList,
          'add'
        )
      })
    },
    handleBatchAdd() {
      this.$nextTick(() => {
        this.$refs['batcnAddForm'].openDialog(
          this.$t("system:l18n:button:batchAdd"),
          {},
          this.PublishVersionList
        )
      })
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs['i18nForm'].openDialog(
          this.$t("common:button:edit"),
          row,
          this.PublishVersionList,
          'update'
        )
      })
    },
    // 重置列表查询
    handleReset() {
      this.searchData = searchDataDefault()
      this.datetimerange_createTime = []
      this.datetimerange_updateTime = []
      this.getList()
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t("system:l18n:confirm:delete"), {
        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()
    },
    // 选择
    handleSelectionChange(val) {
      this.selectTableList = val
    },
    handleDatetimeChange(val, key) {
      if (val) {
        if (key === 'createTime') {
          this.searchData.BeginCreateTime = val[0]
          this.searchData.EndCreatTime = val[1]
        }
        if (key === 'updateTime') {
          this.searchData.BeginUpdateTime = val[0]
          this.searchData.EndUpdateTime = val[1]
        }
      } else {
        if (key === 'createTime') {
          this.searchData.BeginCreateTime = ''
          this.searchData.EndCreatTime = ''
        }
        if (key === 'updateTime') {
          this.searchData.BeginUpdateTime = ''
          this.searchData.EndUpdateTime = ''
        }
      }
    },
    exportTable() {
      return GetInternationalizationList_Export(this.searchData)
    },
  },
}
</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>
