<template>
  <el-dialog
    :visible.sync="visible"
    title="哨卡管理"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-input v-model="dataForm.sentryPostCode" placeholder="哨卡编码" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('query') }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="initDeptMaCodeHandle()">批量生成功能</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="$hasPermission('news:epidemicsentrypost:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
      <!-- <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column> -->
      <el-table-column prop="sentryPostCode" label="哨卡编码" header-align="center" align="center"></el-table-column>
      <el-table-column prop="sentryPostName" label="哨卡名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="thirdSentryPostId" label="第三方哨卡id" header-align="center" align="center"></el-table-column>
      <el-table-column prop="maCodeUrl" label="哨卡小程序码访问地址" header-align="center" align="center"></el-table-column>
      <el-table-column align="center" header-align="center"
                         label="小程序码"
                         :show-overflow-tooltip="true"
                         prop="maCodeUrl">
          <template slot-scope="scope">
            <el-popover placement="right"
                        title=""
                        trigger="click"
                        class="big_image">
              <el-image slot="reference"
                        min-width="70"
                        height="70"
                        v-if="scope.row.maCodeUrl"
                        :src="scope.row.maCodeUrl"
                        :alt="scope.row.maCodeUrl"></el-image>
              <img class="big_image"
                   :src="scope.row.maCodeUrl" />
            </el-popover>
          </template>
        </el-table-column>
      <el-table-column prop="createdTime" label="创建时间" header-align="center" align="center" width="160px"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150px">
        <template slot-scope="scope">
          <el-button type="danger"
                     size="mini"
                     @click="downloadHandle(scope.row.maCodeUrl)">下载</el-button>
          <el-button type="danger"
                     size="mini"
                     @click="singleMaCodeHandle(scope.row.id)">生成</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="pageSizeChangeHandle"
      @current-change="pageCurrentChangeHandle">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </el-dialog>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './epidemicsentrypost-add-or-update'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/custom/epidemicSentryPost/page',
        getDataListIsPage: true
      },
      visible: false,
      dataForm: {
        id: '',
        gridId: '',
        sentryPostCode: ''
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.dataForm.sentryPostCode = ''
      this.getDataList(this.dataForm.gridId)
    },
    // 新增 修改
    addOrUpdateHandle () {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.dataForm.gridId = this.dataForm.gridId
        this.$refs.addOrUpdate.init()
      })
    },
    // 下载
    downloadHandle (maCodeUrl) {
      window.location.href = `${window.SITE_CONFIG['apiURL']}/oss/file/download?fileUrl=${maCodeUrl}`
    },
    // 批量生成功能
    initDeptMaCodeHandle () {
      this.$http
        .post(`custom/epidemicSentryPost/createCodes`, { gridId: this.dataForm.gridId })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
        })
        .catch(() => {})
      this.getDataList(this.dataForm.gridId)
    },
    // 单独生成哨卡码
    singleMaCodeHandle (id) {
      this.$http
        .post(`custom/epidemicSentryPost/createCode`, { postId: id })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
        })
        .catch(() => {})
      this.getDataList(this.dataForm.gridId)
    }
  },
  components: {
    AddOrUpdate
  }
}
</script>
<style scoped>
.big_image {
  width: 300px;
  height: 300px;
}
</style>
