<template>
  <BoxContent>
    <box-content>
      <!-- 搜索框 -->
      <div class="search">
        <el-form :inline="true" size="mini" class="base-search-form">
          <el-form-item :label="$t('dictionary:file:search:Name')">
            <el-input v-model="searchData.Name" style="width: 100px" />
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:search:NameCN')">
            <el-input v-model="searchData.NameCN" style="width: 100px" />
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:search:archiveTypeEnum')">
            <el-select
              v-model="searchData.ArchiveTypeEnum"
              clearable
              placeholder=""
              style="width: 100px"
            >
              <el-option
                v-for="item in $d.ArchiveType"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            :label="$t('dictionary:file:search:subIdentificationEnum')"
          >
            <el-select
              v-model="searchData.SubIdentificationEnum"
              clearable
              placeholder=""
              style="width: 100px"
            >
              <el-option
                v-for="item in $d.SubIdentification"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:search:isEnable')">
            <el-select
              v-model="searchData.IsEnable"
              clearable
              placeholder=""
              style="width: 100px"
            >
              <el-option
                v-for="item in $d.YesOrNo"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('dictionary:file:search:isConfirmRecord')">
            <el-select
              v-model="searchData.IsConfirmRecord"
              clearable
              placeholder=""
              style="width: 100px"
            >
              <el-option
                v-for="item in $d.YesOrNo"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </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-form>
        <span style="margin-left: auto">
          <el-button type="primary" size="mini" @click="handleAdd">
            {{ $t('dictionary:browser:button:add') }}
          </el-button>
        </span>
      </div>

      <el-table
        v-loading="loading"
        v-adaptive="{ bottomOffset: 45 }"
        :data="list"
        stripe
        height="100"
        style="width: 100%"
        @sort-change="handleSortByColumn"
      >
        <el-table-column type="index" width="40" />

        <el-table-column
          prop="Name"
          :label="$t('dictionary:file:table:Name')"
          sortable="custom"
          show-overflow-tooltip
        />
        <el-table-column
          prop="NameCN"
          :label="$t('dictionary:file:table:NameCN')"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          prop="ArchiveTypeEnum"
          :label="$t('dictionary:file:table:archiveTypeEnum')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $fd('ArchiveType', scope.row.ArchiveTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="SubIdentificationEnum"
          :label="$t('dictionary:file:table:subIdentificationEnum')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            {{ $fd('SubIdentification', scope.row.SubIdentificationEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('dictionary:file:table:IsEnable')"
          prop="IsEnable"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag :type="!scope.row.IsEnable ? 'info' : ''">
              {{ $fd('YesOrNo', scope.row.IsEnable) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('dictionary:file:table:isConfirmRecord')"
          prop="IsConfirmRecord"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-tag :type="!scope.row.IsEnable ? 'info' : ''">
              {{ $fd('YesOrNo', scope.row.IsConfirmRecord) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="CreateTime"
          :label="$t('dictionary:file:table:createTime')"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          prop="UpdateTime"
          :label="$t('dictionary:file:table:updateTime')"
          show-overflow-tooltip
          sortable="custom"
        />
        <el-table-column
          :label="$t('dictionary:file:table:action')"
          width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row)">
              {{ $t('dictionary:file:button:edit') }}
            </el-button>
            <el-button
              type="text"
              @click="handleDelete(scope.row)"
              v-if="hasPermi(['dictionary:template:file:del'])"
            >
              {{ $t('dictionary:file: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"
      />
      <File-form
        :config="config"
        :data="rowData"
        v-if="config.visible"
        @close="close"
        @getList="getList"
      />
    </box-content>
  </BoxContent>
</template>
<script>
import { getSysFileTypeList, deleteSysFileType } from '@/api/dictionary'
import BoxContent from '@/components/BoxContent'
import Pagination from '@/components/Pagination'
import FileForm from './fileForm'
const searchDataDefault = () => {
  return {
    ArchiveTypeEnum: null,
    IsConfirmRecord: null,
    IsEnable: null,
    Name: null,
    NameCN: null,
    SubIdentificationEnum: null,
    PageIndex: 1,
    PageSize: 20,
    asc: false,
    sortField: 'CreateTime',
  }
}
export default {
  name: 'fileRecord',
  components: { Pagination, FileForm, BoxContent },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      rowData: {},
      config: {
        visible: false,
        showClose: true,
        width: '400px',
        title: '',
        appendToBody: true,
        status: 'add',
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      try {
        this.loading = true
        let res = await getSysFileTypeList(this.searchData)
        this.loading = false
        if (res.IsSuccess) {
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        }
      } catch (err) {
        this.loading = false
        console.log(err)
      }
    },
    // 新增
    handleAdd() {
      this.rowData = {}
      this.config.title = this.$t('dictionary:file:form:title:add')
      this.config.status = 'add'
      this.config.visible = true
    },
    // 编辑
    handleEdit(row) {
      this.rowData = { ...row }
      this.config.title = this.$t('dictionary:file:form:title:edit')
      this.config.status = 'edit'
      this.config.visible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm(this.$t('dictionary:file:message:deleteMessage'), {
        type: 'warning',
        distinguishCancelAndClose: true,
      })
        .then(() => {
          deleteSysFileType(row.Id).then((res) => {
            if (res.IsSuccess) {
              this.getList()
              this.$message.success(
                this.$t('common:message:deletedSuccessfully')
              )
            }
          })
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 查询
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    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()
    },
    close() {
      this.config.visible = false
    },
  },
}
</script>
<style lang="scss" scoped>
</style>