<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="queryParams.model.templateId" :placeholder="$t('table.smsTask.templateId')"
        class="filter-item search-item"
      />
      <el-input
        v-model="queryParams.model.topic" :placeholder="$t('table.smsTask.topic')"
        class="filter-item search-item"
      />
      <el-input
        v-model="queryParams.model.content" :placeholder="$t('table.smsTask.content')"
        class="filter-item search-item"
      />
      <el-date-picker
        v-model="queryParams.timeRange"
        :range-separator="null"
        class="filter-item search-item date-range-item"
        end-placeholder="结束日期"
        format="yyyy-MM-dd HH:mm:ss"
        start-placeholder="开始日期"
        type="daterange"
        value-format="yyyy-MM-dd HH:mm:ss"
      />
      <el-button class="filter-item" plain type="primary" @click="search">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" plain type="warning" @click="reset">{{ $t('table.reset') }}</el-button>
      <router-link :to="{path:'/sms/manage/edit',query: {type: 'add'}}">
        <el-button v-has-permission="['sms:manage:add']" class="filter-item" plain type="danger">
          {{ $t("table.add") }}
        </el-button>
      </router-link>
      <el-dropdown v-has-any-permission="['sms:manage:delete','sms:manage:export']" class="filter-item" trigger="click">
        <el-button>
          {{ $t('table.more') }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-has-permission="['sms:manage:delete']" @click.native="batchDelete">{{ $t('table.delete')
          }}
          </el-dropdown-item>
          <el-dropdown-item v-has-permission="['sms:manage:export']" @click.native="exportExcel">
            {{ $t("table.export") }}
          </el-dropdown-item>
          <el-dropdown-item v-has-permission="['sms:manage:export']" @click.native="exportExcelPreview">
            {{ $t("table.exportPreview") }}
          </el-dropdown-item>
          <el-dropdown-item v-has-permission="['sms:manage:import']" @click.native="importExcel">
            {{ $t("table.import") }}
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-table
      :key="tableKey"
      ref="table"
      v-loading="loading"
      :data="tableData.records"
      border
      fit
      row-key="id" style="width: 100%;" @filter-change="filterChange"
      @selection-change="onSelectChange"
      @sort-change="sortChange"
      @cell-click="cellClick"
    >
      <el-table-column align="center" type="selection" width="40px" :reserve-selection="true" />
      <el-table-column
        :label="$t('table.smsTask.templateId')" :show-overflow-tooltip="true" align="center"
        prop="templateId" width="100px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.templateId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.smsTask.receiver')" :show-overflow-tooltip="true" align="center"
        prop="receiver"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.receiver }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.smsTask.topic')" :show-overflow-tooltip="true" align="center" width="100px">
        <template slot-scope="scope">
          <span>{{ scope.row.topic }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.smsTask.content')" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :filter-multiple="false" :filters="statusFilters" :label="$t('table.smsTask.status')"
        :show-overflow-tooltip="true" class-name="status-col" column-key="status"
        width="100px"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.sendTime">
            <el-tooltip :content="'发送时间: '+ scope.row.sendTime" class="item" effect="dark" placement="top">
              <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status.desc }}</el-tag>
            </el-tooltip>
          </span>
          <span v-else>
            <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status.desc }}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        :filter-multiple="false"
        :filters="[{ text: $t('common.yes'), value: 'true' }, { text: $t('common.no'), value: 'false' }]"
        :label="$t('table.smsTask.draft')"
        align="center"
        column-key="draft"
        prop="draft"
        width="100px"
      >
        <template slot-scope="scope">
          <span>
            <el-tag slot :type="scope.row.draft?'danger':'success'">{{ scope.row.draft ? '是' : '否' }}</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.createTime')" align="center" sortable="custom"
        prop="createTime"
        width="170px"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('table.operation')" align="center" column-key="operation"
        class-name="small-padding fixed-width" width="100px"
      >
        <template slot-scope="{row}">
          <i
            v-hasPermission="['sms:manage:view']" class="el-icon-view table-operation" style="color: #2db7f5;"
            @click="view(row)"
          ></i>
          <i
            v-show="row.draft" v-hasPermission="['sms:manage:update']" class="el-icon-edit table-operation"
            style="color: #2db7f5;" @click="edit(row)"
          ></i>
          <i
            v-hasPermission="['sms:manage:add']" class="el-icon-copy-document table-operation" style="color: #909399;"
            @click="copy(row)"
          ></i>
          <i
            v-hasPermission="['sms:manage:delete']" class="el-icon-delete table-operation" style="color: #f50;"
            @click="singleDelete(row)"
          ></i>
          <el-link
            v-has-no-permission="['sms:manage:update','sms:manage:delete','sms:manage:add','sms:manage:view']"
            class="no-perm"
          >
            {{ $t('tips.noPermission') }}
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="tableData.total>0" :limit.sync="queryParams.size" :page.sync="queryParams.current"
      :total="Number(tableData.total)" @pagination="fetch"
    />
    <file-import
      ref="import"
      :dialog-visible="fileImport.isVisible"
      :type="fileImport.type" :action="fileImport.action"
      accept=".xls,.xlsx"
      @close="importClose"
      @success="importSuccess"
    />
    <el-dialog
      v-el-drag-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="true"
      title="预览"
      width="80%"
      top="50px"
      :visible.sync="preview.isVisible"
    >
      <el-scrollbar>
        <div v-html="preview.context"></div>
      </el-scrollbar>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import smsTaskApi from '@/api/SmsTask.js'
import { convertEnum } from '@/utils/utils'
import elDragDialog from '@/directive/el-drag-dialog'
import FileImport from "@/components/ceres/Import"
import { downloadFile, initMsgsEnums, initQueryParams } from '@/utils/commons'

export default {
  name: 'StationManage',
  directives: { elDragDialog },
  components: { Pagination, FileImport },
  filters: {
    statusFilter(status) {
      const map = {
        WAITING: 'danger',
        SUCCESS: 'success',
        FAIL: 'error'
      }
      return map[status] || 'success'
    }
  },
  data() {
    return {
      dialog: {
        isVisible: false,
        type: 'add'
      },
      preview: {
        isVisible: false,
        context: ''
      },
      fileImport: {
        isVisible: false,
        type: "import",
        action: `${process.env.VUE_APP_DEV_REQUEST_DOMAIN_PREFIX}/smsTask/import`
      },
      tableKey: 0,
      queryParams: initQueryParams({}),
      selection: [],
      // 以下已修改
      loading: false,
      tableData: {
        total: 0
      },
      enums: {
        TaskStatus: {}
      }
    }
  },
  computed: {
    statusFilters() {
      return convertEnum(this.enums.TaskStatus)
    }
  },
  watch: {
    '$route'(to) {
      if (to.path === '/sms/manage') {
        this.fetch()
      }
    }
  },
  mounted() {
    initMsgsEnums(['TaskStatus'], this.enums)
    this.fetch()
  },
  methods: {
    onSelectChange(selection) {
      this.selection = selection
    },
    search() {
      this.fetch({
        ...this.queryParams
      })
    },
    reset() {
      this.queryParams = initQueryParams({})
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    exportExcelPreview() {
      if (this.queryParams.timeRange) {
        this.queryParams.map.createTime_st = this.queryParams.timeRange[0]
        this.queryParams.map.createTime_ed = this.queryParams.timeRange[1]
      }
      this.queryParams.map.fileName = '导出用户数据'
      smsTaskApi.preview(this.queryParams).then(response => {
        const res = response.data
        this.preview.isVisible = true
        this.preview.context = res.data
      })
    },
    exportExcel() {
      if (this.queryParams.timeRange) {
        this.queryParams.map.createTime_st = this.queryParams.timeRange[0]
        this.queryParams.map.createTime_ed = this.queryParams.timeRange[1]
      }
      this.queryParams.map.fileName = '导出用户数据'
      smsTaskApi.export(this.queryParams).then(response => {
        downloadFile(response)
      })
    },
    importExcel() {
      this.fileImport.type = "upload"
      this.fileImport.isVisible = true
      this.$refs.import.setModel(false)
    },
    importSuccess() {
      this.search()
    },
    importClose() {
      this.fileImport.isVisible = false
    },
    singleDelete(row) {
      this.$refs.table.clearSelection()
      this.$refs.table.toggleRowSelection(row, true)
      this.batchDelete()
    },
    batchDelete() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t('tips.noDataSelected'),
          type: 'warning'
        })
        return
      }
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        const ids = []
        this.selection.forEach((u) => {
          ids.push(u.id)
        })
        this.delete(ids)
      }).catch(() => {
        this.clearSelections()
      })
    },
    clearSelections() {
      this.$refs.table.clearSelection()
    },
    delete(ids) {
      smsTaskApi.delete({ 'ids': ids })
        .then((response) => {
          const res = response.data
          if (res.isSuccess) {
            this.$message({
              message: this.$t('tips.deleteSuccess'),
              type: 'success'
            })
          }
          this.search()
        })
    },
    copy(row) {
      this.$router.push({
        path: '/sms/manage/edit',
        query: {
          id: row.id,
          type: 'copy'
        }
      })
    },
    view(row) {
      this.$router.push({
        path: '/sms/manage/edit',
        query: {
          id: row.id,
          type: 'view'
        }
      })
    },
    edit(row) {
      this.$router.push({
        path: '/sms/manage/edit',
        query: {
          id: row.id,
          type: 'edit'
        }
      })
    },
    fetch(params = {}) {
      this.loading = true
      if (this.queryParams.timeRange) {
        this.queryParams.map.createTime_st = this.queryParams.timeRange[0]
        this.queryParams.map.createTime_ed = this.queryParams.timeRange[1]
      }

      this.queryParams.current = params.current ? params.current : this.queryParams.current
      this.queryParams.size = params.size ? params.size : this.queryParams.size

      smsTaskApi.page(this.queryParams).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.tableData = res.data
        }
      }).finally(() => this.loading = false)
    },
    sortChange(val) {
      this.queryParams.sort = val.prop
      this.queryParams.order = val.order
      if (this.queryParams.sort) {
        this.search()
      }
    },
    filterChange(filters) {
      for (const key in filters) {
        if (key.includes('.')) {
          const val = {}
          val[key.split('.')[1]] = filters[key][0]
          this.queryParams.model[key.split('.')[0]] = val
        } else {
          this.queryParams.model[key] = filters[key][0]
        }
      }
      this.search()
    },
    cellClick (row, column) {
      if (column['columnKey'] === "operation") {
        return
      }
      let flag = false
      this.selection.forEach((item) => {
        if (item.id === row.id) {
          flag = true
          this.$refs.table.toggleRowSelection(row)
        }
      })
      if (!flag) {
        this.$refs.table.toggleRowSelection(row, true)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
