<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.Module"
            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 label="说明">
          <el-input
            v-model="searchData.Description"
            size="small"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="迭代">
          <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>
          <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-button
            type="primary"
            icon="el-icon-edit-outline"
            size="mini"
            :disabled="selectTableList.length <= 0"
            @click="handleBatchUpdate"
          >
            批量更新
          </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="国际化类型"
          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="Module"
          min-width="120"
          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="Description"
          min-width="100"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          label="迭代"
          prop="Version"
          min-width="100"
          show-overflow-tooltip
          sortable="custom"
        />
        <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" @batch="batch" />
      <BatchAddForm ref="batcnAddForm" @getList="getList" />
    </div>
  </div>
</template>
<script>
import {
  getInternationalizationList,
  deleteInternationalization,
  getPublishVersionSelect,
  batchUpdateInternationalInfo,
} 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,
    Module: null,
    PublishLogId: 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,
      PublishVersionList: [],
      selectTableList: [],
    };
  },
  mounted() {
    this.getList();
    this.getPublishVersionSelect();
  },
  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.PublishVersionList,
          "batch"
        );
      });
    },
    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.PublishVersionList,
          "add"
        );
      });
    },
    handleBatchAdd() {
      this.$nextTick(() => {
        this.$refs["batcnAddForm"].openDialog(
          "批量新增",
          {},
          this.PublishVersionList
        );
      });
    },
    handleEdit(row) {
      this.$nextTick(() => {
        this.$refs["i18nForm"].openDialog(
          "编辑",
          row,
          this.PublishVersionList,
          "update"
        );
      });
    },
    // 重置列表查询
    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();
    },
    // 选择
    handleSelectionChange(val) {
      this.selectTableList = val;
    },
  },
};
</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>
