<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-sys__deptmacode}">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataList()">

        <el-form-item label="小程序码所属模块">
          <el-select v-model="dataForm.moduleMaCode" placeholder="小程序码所属模块" clearable>
            <el-option
                v-for="item in maCodeList"
                :key="item.dictName"
                :label="item.remark"
                :value="item.dictName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button v-if="$hasPermission('sys:modulemacode:create')"
                     type="primary"
                     @click="createModuleMaCodeHandle()">生成模块二维码</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading"
                :data="dataList"
                border
                @selection-change="dataListSelectionChangeHandle"
                style="width: 100%;">
        <el-table-column prop="moduleMaCode"
                         label="所属模块"
                         header-align="center"
                         align="center"
                         show-overflow-tooltip :formatter="showModuleMaCodeFormatter"></el-table-column>
        <el-table-column align="center"
                         label="小程序码"
                         :show-overflow-tooltip="true"
                         prop="codeUrl">
          <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.codeImageUrl"
                        :src="scope.row.codeImageUrl"
                        :alt="scope.row.codeImageUrl"></el-image>
              <img class="big_image"
                   :src="scope.row.codeImageUrl" />
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime"
                         label="创建时间"
                         header-align="center"
                         align="center"></el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="70">
          <template slot-scope="scope">

            <el-button v-if="scope.row.codeImageUrl"
                       type="text"
                       size="small"
                       @click="downloadHandle(scope.row.codeImageUrl)">下载</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>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/sys/modulemacode/page',
        getDataListIsPage: true,
        deleteURL: '/sys/modulemacode',
        deleteIsBatch: true
      },
      dataForm: {
        moduleMaCode: '',
        codeAppUrl: ''
      },
      maCodeList: []
    }
  },
  components: {},
  created () {
    this.getModuleMaCodeList()
  },
  methods: {
    getModuleMaCodeList () {
      this.$http
        .get(`/sys/dict/listSimple/module_ma_code`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.maCodeList = res.data
        })
        .catch(() => {})
    },
    showModuleMaCodeFormatter: function (row, column) {
      if (row.moduleMaCode) {
        let dict = this.maCodeList.filter(item => item.dictName === row.moduleMaCode)[0]
        if (dict) {
          return dict.remark
        }
      }
      return ''
    },
    createModuleMaCodeHandle () {
      if (this.dataForm.moduleMaCode === undefined || this.dataForm.moduleMaCode === '') {
        return this.$message.error('请选择小程序码所属模块')
      }
      if (this.dataForm.moduleMaCode) {
        let dict = this.maCodeList.filter(item => item.dictName === this.dataForm.moduleMaCode)[0]
        if (dict) {
          this.dataForm.codeAppUrl = dict.dictValue
        } else {
          return this.$message.error('无法匹配正确小程序URL,请检查字典配置！')
        }
      }
      this.$confirm(
        this.$t('prompt.info', { handle: '生成小程序码' }),
        this.$t('生成'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      )
        .then(() => {
          this.$http
            .post(`/sys/modulemacode/create/`, this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            })
            .catch(() => { })
        })
        .catch(() => { })
    },
    initModuleMaCodeHandle () {
      this.$confirm('初始化所有网格的小程序码', this.$t('初始化'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      })
        .then(() => {
          let loading = this.$loading({
            lock: true,
            text: '正在进行初始化...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          this.$http
            .post(`/sys/deptmacode/init`)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              loading.close()
              this.$message({
                message: this.$t('prompt.success'),
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            })
            .catch(() => { })
        })
        .catch(() => { })
    },
    downloadHandle (codeImageUrl) {
      window.location.href = `${window.SITE_CONFIG['apiURL']}/oss/file/download?fileUrl=${codeImageUrl}`
    }
  }
}
</script>
<style scoped>
.big_image {
  width: 300px;
  height: 300px;
}
</style>
