<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true" size="small">
        <!-- 文件类型 -->
        <el-form-item :label="$t('dictionary:agreement:table:UserAgreementTypeEnum')">
          <el-select v-model="searchData.UserAgreementTypeEnum" style="width: 150px">
            <el-option v-for="item of $d.UserAgreementType" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <!-- 文件名称 -->
        <el-form-item :label="$t('dictionary:agreement:table:FileName')">
          <el-input v-model="searchData.FileName" style="width: 130px" clearable />
        </el-form-item>
        <!-- 版本 -->
        <el-form-item :label="$t('dictionary:agreement:table:FileVersion')">
          <el-input v-model="searchData.FileVersion" style="width: 130px" clearable />
        </el-form-item>
        <!--更新日期-->
        <el-form-item :label="$t('dictionary:agreement:table:UpdateDate')">
          <el-date-picker v-model="UpdateDate" @change="changeTimeList('UpdateDate')" value-format="yyyy-MM-dd"
            type="daterange">
          </el-date-picker>
        </el-form-item>
        <!--生效日期-->
        <el-form-item :label="$t('dictionary:agreement:table:EffectiveDate')">
          <el-date-picker v-model="EffectiveDate" @change="changeTimeList('EffectiveDate')" value-format="yyyy-MM-dd"
            type="daterange">
          </el-date-picker>
        </el-form-item>
        <!-- 当前版本 -->
        <el-form-item :label="$t('dictionary:agreement:table:IsCurrentVersion')">
          <el-select v-model="searchData.IsCurrentVersion" style="width: 150px">
            <el-option v-for="item of $d.YesOrNo" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <!-- 查询 -->
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <!-- 重置 -->
          <el-button type="primary" icon="el-icon-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
        <el-button type="primary" style="float: right" size="small" @click="handleAdd">
          {{ $t('common:button:new') }}
        </el-button>
      </el-form>
    </template>
    <template slot="main-container">
      <el-table v-adaptive="{ bottomOffset: 60 }" v-loading="loading" :data="list" stripe height="100"
        @sort-change="handleSortChange">
        <el-table-column type="index" width="40" />
        <!-- 文件类型 -->
        <el-table-column prop="UserAgreementTypeEnum" :label="$t('dictionary:agreement:table:UserAgreementTypeEnum')"
          show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            {{
              $fd(
                'UserAgreementType',
                scope.row.UserAgreementTypeEnum
              )
            }}
          </template>
        </el-table-column>
        <!-- 文件名称 -->
        <el-table-column prop="FileName" :label="$t('dictionary:agreement:table:FileName')" show-overflow-tooltip
          sortable="custom" />
        <!-- 版本 -->
        <el-table-column prop="FileVersion" :label="$t('dictionary:agreement:table:FileVersion')" show-overflow-tooltip
          sortable="custom" />
        <!--内容-->
        <el-table-column prop="FileContent" :label="$t('dictionary:agreement:table:FileContent')">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="view(scope.row)">{{ $t('dictionary:agreement:button:view')
            }}</el-button>
          </template>
        </el-table-column>
        <!-- 更新日期 -->
        <el-table-column prop="UpdateDate" :label="$t('dictionary:agreement:table:UpdateDate')" show-overflow-tooltip
          sortable="custom">
          <template slot-scope="scope">
            <span>{{ scope.row.UpdateDate ? scope.row.UpdateDate.split(' ')[0] : '' }}</span>
          </template>
        </el-table-column>
        <!-- 生效日期 -->
        <el-table-column prop="EffectiveDate" :label="$t('dictionary:agreement:table:EffectiveDate')"
          show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            <span>{{ scope.row.EffectiveDate ? scope.row.EffectiveDate.split(' ')[0] : '' }}</span>
          </template>
        </el-table-column>
        <!-- 当前版本 -->
        <el-table-column prop="IsCurrentVersion" :label="$t('dictionary:agreement:table:IsCurrentVersion')"
          show-overflow-tooltip sortable="custom">
          <template slot-scope="scope">
            {{
              $fd(
                'YesOrNo',
                scope.row.IsCurrentVersion
              )
            }}
          </template>
        </el-table-column>
        <!-- 创建日期 -->
        <el-table-column prop="CreateTime" :label="$t('dictionary:agreement:table:CreateTime')" show-overflow-tooltip
          sortable="custom" />
        <!-- 修改日期 -->
        <el-table-column prop="UpdateTime" :label="$t('dictionary:agreement:table:UpdateTime')" show-overflow-tooltip
          sortable="custom" />

        <el-table-column :label="$t('common:action:action')" width="300">
          <template slot-scope="scope">
            <el-button type="text" :disabled="scope.row.IsCurrentVersion" @click="setCurrentVersion(scope.row)">
              {{ $t('dictionary:agreement:button:setCurrentVersion') }}
            </el-button>
            <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>
      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize"
        @pagination="getList" />

      <!-- 新增/编辑 -->
      <el-dialog v-if="editDialog.visible" :visible.sync="editDialog.visible" :close-on-click-modal="false"
        :title="editDialog.title" custom-class="base-dialog-wrapper" :fullscreen="true">
        <TemplateForm :data="currentRow" @closeDialog="closeDialog" @getList="getList" />
      </el-dialog>
    </template>
  </BaseContainer>
</template>
<script>
import {
  getUserAgreementList,
  deleteUserAgreement,
  setCurrentVersion
} from '@/api/dictionary'
import BaseContainer from '@/components/BaseContainer'
import Pagination from '@/components/Pagination'
import TemplateForm from './TemplateForm'
const searchDataDefault = () => {
  return {
    StartEffectiveDate: null,
    EndEffectiveDate: null,
    FileName: null,
    FileVersion: null,
    IsCurrentVersion: null,
    StartUpdateDate: null,
    EndUpdateDate: null,
    UserAgreementTypeEnum: null,
    Asc: false,
    SortField: "",
    PageIndex: 1,
    PageSize: 20,
  }
}
export default {
  name: 'Agreement',
  components: { BaseContainer, Pagination, TemplateForm },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      currentRow: {},
      editDialog: { title: '', visible: false },
      loading: false,
      UpdateDate: [],
      EffectiveDate: []
    }
  },
  mounted() {
    this.getList()
  },
  computed: {
    isEN() {
      return this.$i18n.locale !== 'zh'
    },
  },
  methods: {
    async setCurrentVersion(row) {
      try {
        let data = {
          Id: row.Id
        }
        this.loading = true
        let res = await setCurrentVersion(data)
        this.loading = false
        if (res.IsSuccess) {
          this.getList()
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    view(row) {
      this.$AGR({
        Id: row.Id,
        IsEn_Us: this.isEN
      })
    },
    changeTimeList(key) {
      if (this[key]) {
        this.searchData[`Start${key}`] = this[key][0]
        this.searchData[`End${key}`] = this[key][1]
      } else {
        this.searchData.BeginCreateTime = null
        this.searchData.EndCreateTime = null
      }
    },
    handleDelete(row) {
      // 确定删除该模版？
      this.$confirm(this.$t('dictionary:agreement:message:msg1')).then(() => {
        deleteUserAgreement(row.Id).then(() => {
          this.$message.success(this.$t('common:message:deletedSuccessfully'))
          this.getList()
        })
      }).catch(() => { })
    },
    PreviewFile(row) {
      let basePath = window.location.origin
      if (window.location.protocol !== 'https:') {
        basePath = 'https://irc.test.extimaging.com'
      }
      let data = {
        name: row.NameCN,
        path: basePath + row.Path,
      }
      this.$emit('PreviewFile', data)
    },
    getList() {
      this.loading = true
      getUserAgreementList(this.searchData)
        .then((res) => {
          this.loading = false
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          this.loading = false
        })
    },
    // 新增
    handleAdd() {
      this.editDialog.title = this.$t('common:button:new')
      this.currentRow = {}
      this.editDialog.visible = true
    },
    // 下载
    async handleDownload(row) {
      try {
        this.loading = true
        let fileName = this.isEN ? row.Name : row.NameCN;
        let type = fileName
          .substring(fileName.lastIndexOf('.'))
          .toLocaleLowerCase()
        if (!type) {
          let extendName = row.Path
            .substring(row.Path.lastIndexOf('.'))
            .toLocaleLowerCase()
          fileName += extendName
        }
        let res = await downLoadFile(this.OSSclientConfig.basePath + row.Path, fileName)
        this.loading = false
      } catch (err) {
        this.loading = false
      }
    },
    // 编辑
    handleEdit(row) {
      this.editDialog.title = this.$t('common:button:edit')
      this.currentRow = { ...row }
      this.editDialog.visible = true
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    closeDialog() {
      this.editDialog.visible = false
    },
    // 排序
    handleSortChange(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" scoped>
::v-deep .search {
  display: block;
}
</style>
