<template>
  <el-dialog
    append-to-body
    title="图片分类管理"
    top="10px"
    :visible.sync="dialogFormVisible"
    width="80%"
    @close="close"
  >
    <div class="store-media-gallery-container">
      <vab-query-form>
        <vab-query-form-left-panel :span="24">
          <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
            {{ __('新增') }}
          </el-button>
        </vab-query-form-left-panel>
      </vab-query-form>
      <el-table
        ref="table"
        v-loading="listLoading"
        border
        :data="list"
        height="700px"
      >
        <el-table-column align="center" :label="__('操作')" width="120">
          <template #default="{ row }">
            <el-button type="text" @click="handleEdit(row)">
              {{ __('编辑') }}
            </el-button>
            <el-button type="text" @click="delGallery(row)">
              {{ __('删除') }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="__('商品图片ID')"
          prop="gallery_id"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('相册名称')"
          prop="gallery_name"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('相册描述')"
          prop="gallery_desc"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('内容数量')"
          prop="gallery_num"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('默认相册')"
          prop="gallery_is_default"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('排序')"
          prop="gallery_order"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('附件册')"
          prop="gallery_type"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('封面')"
          prop="gallery_cover"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-image
              v-show="getUrl(scope.row.gallery_cover) !== ''"
              popper-class="image"
              :preview-src-list="[getUrl(scope.row.gallery_cover)]"
              :src="getUrl(scope.row.gallery_cover)"
              style="width: 35px; height: 35px"
            />
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="queryForm.pageNum"
        :layout="layout"
        :page-size="queryForm.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
    <el-dialog
      append-to-body
      :title="editDialogTitle"
      :visible.sync="editVisible"
      width="35%"
      @close="closeEditDialog"
    >
      <el-form
        ref="galleryForm"
        label-position="left"
        label-width="100px"
        :model="galleryForm"
        :rules="rules"
      >
        <el-form-item label="相册名称" prop="gallery_name">
          <el-input
            v-model="galleryForm.gallery_name"
            clearable
            placeholder="请输入相册名称"
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="相册描述" prop="gallery_desc">
          <el-input
            v-model="galleryForm.gallery_desc"
            clearable
            placeholder="请输入相册描述"
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="排序" prop="gallery_order">
          <el-input
            v-model="galleryForm.gallery_order"
            clearable
            placeholder="请输入排序"
            :style="{ width: '100%' }"
          />
        </el-form-item>
        <el-form-item label="类型" prop="gallery_type">
          <el-radio-group v-model="galleryForm.gallery_type">
            <el-radio
              v-for="(item, index) in gallery_typeOptions"
              :key="index"
              :disabled="item.disabled"
              :label="item.value"
            >
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="封面" prop="gallery_cover">
          <Upload
            height="100px"
            :image="galleryForm.gallery_cover"
            width="100px"
            @upImage="
              (image) => {
                galleryForm.gallery_cover = image
              }
            "
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">{{ __('取消') }}</el-button>
        <el-button type="primary" @click="saveGallery">
          {{ __('确定') }}
        </el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doDelete, doEdit } from '@/api/store/media/gallery'
  import Upload from '@/components/upload'

  export default {
    name: 'MediaGallery',
    components: { Upload },
    data() {
      return {
        list: [],
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
        },
        dialogFormVisible: false,
        editVisible: false,
        editDialogTitle: '',
        galleryForm: {
          gallery_name: undefined,
          gallery_desc: undefined,
          gallery_order: 0,
          gallery_type: 'image',
          gallery_cover: null,
        },
        rules: {
          gallery_name: [
            {
              required: true,
              message: '请输入相册名称',
              trigger: 'blur',
            },
          ],
          gallery_desc: [],
          gallery_order: [],
          gallery_type: [
            {
              required: true,
              message: '类型不能为空',
              trigger: 'change',
            },
          ],
        },
        gallery_typeOptions: [
          {
            label: '图片',
            value: 'image',
          },
          {
            label: '视频',
            value: 'video',
          },
          // {
          //   label: '音频',
          //   value: 'audio',
          // },
          // {
          //   label: '文档',
          //   value: 'document',
          // },
          {
            label: '其它',
            value: 'other',
          },
        ],
      }
    },
    computed: {
      getUrl() {
        return function (url) {
          if (!url) return ''
          return url
        }
      },
    },
    methods: {
      __,
      showEdit() {
        this.fetchData()
        this.dialogFormVisible = true
      },
      close() {
        this.dialogFormVisible = false
      },
      closeEditDialog() {
        this.$refs['galleryForm'].resetFields()
        this.galleryForm = {
          gallery_order: 0,
          gallery_type: 'image',
          gallery_cover: '',
        }
        this.editVisible = false
      },
      handleEdit(row) {
        if (!row.gallery_id) {
          this.editDialogTitle = this.__('添加')
        } else {
          this.editDialogTitle = this.__('编辑')
          const gallery = Object.assign({}, row)
          this.galleryForm = {
            gallery_id: gallery.gallery_id,
            gallery_name: gallery.gallery_name,
            gallery_desc: gallery.gallery_desc,
            gallery_order: gallery.gallery_order,
            gallery_type: gallery.gallery_type,
            gallery_cover: gallery.gallery_cover,
          }
        }
        this.editVisible = true
      },
      saveGallery() {
        this.$refs['galleryForm'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.galleryForm)
            this.$baseMessage(msg, 'success')
            await this.fetchData()
            this.closeEditDialog()
          } else {
            return false
          }
        })
      },
      delGallery(row) {
        if (row.gallery_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg } = await doDelete({ gallery_id: row.gallery_id })
            this.$baseMessage(msg, 'success')
            await this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
