<template>
  <div class="appcarousel">
    <ai-list>
      <template slot="title">
        <ai-title title="轮播图片" :isShowBottomBorder="false" :isShowArea="false"></ai-title>
      </template>

      <template slot="content">
        <ai-search-bar>
          <template slot="left">
            <el-button type="primary" icon="iconfont iconAdd" size="small" @click="add">添加</el-button>
          </template>

          <template slot="right">
            <el-input v-model="search.title" size="small" placeholder="搜索标题" clearable
                      @keyup.enter.native=";(page.current = 1), getList()"
                      @clear=";(page.current = 1), (search.title = ''), getList()" suffix-icon="iconfont iconSearch"/>
          </template>
        </ai-search-bar>

        <ai-table :tableData="tableData" :colConfigs="colConfigs" :total="page.total" :current.sync="page.current"
                  :size.sync="page.size" @getList="getList" class="ai-table">
          <!-- 首页封面 -->
          <el-table-column label="首页封面" align="left" width="150" slot="imgUrl">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" alt="" class="banner-img" v-if="row.imgUrl"/>
            </template>
          </el-table-column>

          <!-- 操作 -->
          <el-table-column label="操作" align="center" width="300" slot="option">
            <template slot-scope="{ row }">
              <el-button type="text" @click="release(row)" v-if="row.status == 1">取消发布</el-button>
              <el-button type="text" @click="release(row)" v-else>发布</el-button>

              <el-button type="text" @click="detail(row)">详情</el-button>
              <el-button type="text" @click="edit(row)">编辑</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>

    <!-- 添加的模态框 -->
    <ai-dialog :title="dialog.title" :visible.sync="visible" @onCancel="visible = false" @onConfirm="addConfirm"
               width="800px">
      <el-form ref="ruleForm" :model="dialogInfo" :rules="formRules" size="small" label-width="100px">
        <!-- 首页封面 -->
        <el-form-item label="首页封面" prop="imgUrl">
          <ai-uploader v-model="dialogInfo.imgUrl" @change="change" :instance="instance" :limit="1"></ai-uploader>
        </el-form-item>
        <!-- 活动名称 -->
        <el-form-item label="活动名称" prop="title">
          <el-input placeholder="请输入活动名称" :maxlength="30" show-word-limit v-model="dialogInfo.title"></el-input>
        </el-form-item>
        <!-- 连接类型 -->
        <el-form-item label="链接类型" prop="type">
          <ai-select v-model="dialogInfo.type" placeholder="请选择链接类型"
                     :selectList="$dict.getDict('bannerType')"></ai-select>
        </el-form-item>
        <!-- 链接 -->
        <el-form-item label="链接" prop="linkUrl">
          <el-input placeholder="请输入链接" v-model="dialogInfo.linkUrl"></el-input>
        </el-form-item>
      </el-form>
    </ai-dialog>

    <!-- 详情的模态框 -->
    <ai-dialog title="详情" :visible.sync="detailDialog" customFooter width="800px">
      <!--  -->
      <el-form ref="ruleForm" :model="dialogInfo" :rules="formRules" size="small" label-width="100px">
        <ai-wrapper label-width="80px" :columnsNumber="1">
          <ai-info-item label="首页封面">
            <span><img :src="dialogInfo.imgUrl" alt="" style="width:100px;height:100px;"/></span>
          </ai-info-item>
          <ai-info-item label="活动名称：">
            <span>{{ dialogInfo.title }}</span>
          </ai-info-item>
          <ai-info-item label="链接类型：">
            <span>{{ this.dict.getLabel('bannerType', dialogInfo.type) }}</span>
          </ai-info-item>
          <ai-info-item label="链接：">
            <span>{{ dialogInfo.linkUrl }}</span>
          </ai-info-item>
        </ai-wrapper>
      </el-form>

      <div class="dialog-footer" slot="footer">
        <el-button @click="detailDialog=false">关闭</el-button>
      </div>
    </ai-dialog>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  label: '轮播图片',
  name: 'AppCarousel',
  // 组件
  components: {},
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
  },
  data() {
    return {
      visible: false,
      detailDialog: false,
      // status: 0,
      // id:'',
      search: {
        title: '',
      },
      tableData: [],
      page: {
        size: 10,
        current: 1,
        total: 0,
      },
      dialog: {
        // title: '',
        visible: false,
      },
      dialogInfo: {
        id: '',
        title: '',
        imgUrl: [],
        type: '',
        linkUrl: '',
      },
      // fileList: '',
      formRules: {
        imgUrl: [{required: true, message: '请添加图片', trigger: 'blur'}],
        title: [{required: true, message: '请输入活动名称', trigger: 'blur'}],
        type: [{required: true, trigger: 'blur'}],
        // linkUrl: [{ required: true, message: '请输入链接', trigger: 'blur' }]
      },
      colConfigs: [
        {prop: 'imgUrl', label: '首页封面', slot: 'imgUrl'},
        {
          prop: 'title',
          label: '活动名称',

          'show-overflow-tooltip': true,
        },
        {
          prop: 'type',
          label: '链接类型',
          render: (h, {row}) => {
            return h('span', null, this.dict.getLabel('bannerType', row.type))
          },
        },
        {
          prop: 'status',
          label: '发布状态',
          width: 400,
          render: (h, {row}) => {
            return h('span', null, this.dict.getLabel('bannerSstatus', row.status))
          },
        },

        {slot: 'option', label: '操作', width: 280},
      ],
    }
  },
  // 计算
  computed: {
    ...mapState(['user']),
  },
  // 监听
  watch: {},
  // 实例创建后
  created() {
    this.dict.load('bannerType', 'bannerSstatus').then(() => {
      this.getList()
    })
    // this.getList()
    this.getShopList()
  },
  // 实例渲染后
  mounted() {
  },
  // 方法
  methods: {
    getList() {
      this.instance
      .post(`/app/appbanner/list`, null, {
        params: {
          ...this.search,
          ...this.page,
        },
      })
      .then((res) => {
        if (res.code == 0) {
          // console.log(res.data.records)
          this.tableData = res.data.records

          this.page.total = res.data.total
        }
      })
      .catch(() => {
      })
    },
    getShopList() {
    },
    add() {
      this.dialog.title = '添加'
      this.dialogInfo = {}
      this.getList()
      this.visible = true
    },

    data(e) {
      this.files.push(e)
    },
    change(e) {
      this.files = e
    },
    // 确定新增
    addConfirm() {
      // console.log('确定')
      this.instance
      .post(`/app/appbanner/addOrUpdate`, {
        imgUrl: this.dialogInfo.imgUrl[0].url,
        linkUrl: this.dialogInfo.linkUrl,
        status: this.status,
        title: this.dialogInfo.title,
        type: this.dialogInfo.type,
        id: this.dialogInfo.id,
      })
      .then(res => {
        if (res?.code == 0) {
          this.$message.success('新增成功')
          this.visible = false
          this.getList()
        }
      })
    },
    // 发布/取消发布
    release(row) {
      this.$confirm('确定此操作？').then(() => {
        var status = row.status == 1 ? '0' : '1'
        this.instance.post(`/app/appbanner/setStatus?id=${row.id}&status=${status}`)
        .then((res) => {
          if (res?.code == 0) {
            this.getList()
          }
        })
      })
    },
    // 详情
    detail(row) {
      this.detailDialog = true
      this.instance.post(`/app/appbanner/detail?id=${row.id}`, {
        // imgUrl: this.dialogInfo.imgUrl[0].url
      })
      .then((res) => {
        if (res?.data) {
          this.dialogInfo = {...row}
        }
      })
    },
    // 编辑
    edit(row) {
      this.dialog.title = '编辑'
      this.visible = true
      this.dialogInfo = {...row}
      this.dialogInfo.imgUrl = [{url: ''}]
      this.dialogInfo.imgUrl[0].url = row.imgUrl
      // console.log(this.dialogInfo)
    },
    // 删除
    remove(id) {
      this.$confirm('删除后不可恢复，是否要删除该事项？', {
        type: 'error',
      }).then(() => {
        this.instance.post(`/app/appbanner/delete?ids=${id}`).then((res) => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },
  },
}
</script>

<style lang="scss">
.appcarousel {
  height: 100%;
  // padding: 15px 10px;
  .banner-img {
    width: 50px;
    height: 50px;
  }
}
</style>
