<template>
  <div class="appxsbanner">
    <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>
        </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="100" show-word-limit v-model="dialogInfo.title"></el-input>
        </el-form-item>
        <!-- 链接 -->
        <el-form-item label="链接地址" prop="linkUrl">
          <el-input placeholder="请输入链接地址" :maxlength="255" v-model="dialogInfo.linkUrl"></el-input>
        </el-form-item>
        <!-- 排序 -->
        <el-form-item label="显示顺序" prop="showIndex">
          <el-input type="number" placeholder="请输入显示顺序" v-model="dialogInfo.showIndex"></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>{{ dialogInfo.linkUrl }}</span>
          </ai-info-item>
          <ai-info-item label="状态：">
            <span>{{ dict.getLabel("bannerSstatus", dialogInfo.status) }}</span>
          </ai-info-item>
          <ai-info-item label="显示顺序：">
            <span>{{ dialogInfo.showIndex }}</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: 'AppXsBanner',
  // 组件
  components: {},
  props: {
    instance: Function,
    dict: Object,
    permissions: Function,
  },
  data() {
    return {
      visible: false,
      detailDialog: false,
      // status: 0,
      // id:'',
      tableData: [],
      page: {
        size: 10,
        current: 1,
        total: 0,
      },
      dialog: {
        // title: '',
        visible: false,
      },
      dialogInfo: {
        id: '',
        title: '',
        imgUrl: [],
        status: '',
        linkUrl: '',
        showIndex: ''
      },
      // fileList: '',
      formRules: {
        imgUrl: [{required: true, message: '请添加图片', trigger: 'blur'}],
        title: [{required: true, message: '请输入活动名称', trigger: 'blur'}],
        showIndex: [{required: true, message: '请输入显示顺序', trigger: 'blur'}]
      },
      colConfigs: [
        {prop: 'imgUrl', label: '首页封面', slot: 'imgUrl'},
        {
          prop: 'title',
          label: '活动名称',

          'show-overflow-tooltip': true,
        },
        {
          prop: 'linkUrl',
          label: '链接地址',

          'show-overflow-tooltip': true,
        },
        {
          prop: 'status',
          label: '发布状态',
          width: 400,
          render: (h, {row}) => {
            return h('span', null, this.dict.getLabel('bannerSstatus', row.status))
          },
        },
        {
          prop: 'createTime',
          label: '创建时间',
          'show-overflow-tooltip': true
        },

        {slot: 'option', label: '操作', width: 280},
      ],
    }
  },
  // 计算
  computed: {
    ...mapState(['user']),
  },
  // 监听
  watch: {},
  // 实例创建后
  created() {
    this.dict.load('bannerSstatus').then(() => {
      this.getList()
    })
  },
  // 实例渲染后
  mounted() {
  },
  // 方法
  methods: {
    getList() {
      this.instance
      .post(`/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(() => {
      })
    },
    add() {
      this.dialog.title = '添加'
      this.dialogInfo = {}
      this.visible = true
    },

    data(e) {
      this.files.push(e)
    },
    change(e) {
      this.files = e
    },
    // 确定新增
    addConfirm() {
      // console.log('确定')
      this.instance.post(`/appbanner/addOrUpdate`, {
        imgUrl: this.dialogInfo.imgUrl?.[0]?.url,
        linkUrl: this.dialogInfo.linkUrl,
        status: this.status,
        title: this.dialogInfo.title,
        showIndex: this.dialogInfo.showIndex,
        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(`/appbanner/setStatus?id=${row.id}&status=${status}`)
        .then((res) => {
          if (res?.code == 0) {
            this.getList()
          }
        })
      })
    },
    // 详情
    detail(row) {
      this.detailDialog = true
      this.instance.post(`/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(`/appbanner/delete?ids=${id}`).then((res) => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },
  },
}
</script>

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