<template>
  <div class="appcarousel">
    <ai-list>
      <template slot="title">
        <ai-title title="首图设置" isShowBottomBorder></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
                      v-throttle="() => {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="220" slot="option">
            <template slot-scope="{ row }">
              <div class="table-options">
                <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>
              </div>
            </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 class="ai-form" ref="form" :model="dialogInfo" :rules="formRules" size="small" label-width="100px">
        <el-form-item style="width: 100%;" label="首页封面" prop="imgUrl">
          <ai-uploader v-model="dialogInfo.imgUrl" @change="change" :instance="instance" :limit="1"></ai-uploader>
        </el-form-item>
        <el-form-item style="width: 100%;" label="图片名称" prop="title">
          <el-input placeholder="请输入图片名称" :maxlength="30" show-word-limit v-model="dialogInfo.title"></el-input>
        </el-form-item>
        <el-form-item style="width: 100%;" label="链接类型" prop="type">
          <ai-select v-model="dialogInfo.type" placeholder="请选择链接类型"
                     :selectList="dict.getDict('bannerType')"></ai-select>
        </el-form-item>
        <el-form-item style="width: 100%;" label="链接" prop="linkUrl">
          <el-input placeholder="请输入链接" v-model="dialogInfo.linkUrl"></el-input>
        </el-form-item>
        <el-form-item style="width: 100%;" label="位置" prop="position">
          <ai-select v-model="dialogInfo.position" placeholder="请选择图片位置"
                     :selectList="dict.getDict('bannerPosition')"></ai-select>
        </el-form-item>
      </el-form>
    </ai-dialog>

    <!-- 详情的模态框 -->
    <ai-dialog title="详情" :visible.sync="detailDialog" customFooter width="800px">
      <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-info-item label="位置">
          <span>{{ this.dict.getLabel('bannerPosition', dialogInfo.position) }}</span>
        </ai-info-item>
      </ai-wrapper>
      <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: '',
        position: ''
      },
      // fileList: '',
      formRules: {
        imgUrl: [{required: true, message: '请添加图片', trigger: 'change'}],
        title: [{required: true, message: '请输入图片名称', trigger: 'blur'}],
        type: [{required: true, message: '请选择链接类型', trigger: 'change'}],
        position: [{required: true, message: '请选择位置', trigger: 'change'}]
      },
      colConfigs: [
        {prop: 'imgUrl', label: '首页封面', slot: 'imgUrl'},
        {
          prop: 'title',
          label: '图片名称',

          'show-overflow-tooltip': true,
        },
        {
          prop: 'type',
          label: '链接类型',
          align: 'center',
          render: (h, {row}) => {
            return h('span', null, this.dict.getLabel('bannerType', row.type))
          },
        },
        {
          prop: 'status',
          label: '发布状态',
          align: 'center',
          render: (h, {row}) => {
            return h('span', null, this.dict.getLabel('bannerSstatus', row.status))
          },
        },
        {
          prop: 'position',
          label: '位置',
          align: 'center',
          render: (h, {row}) => {
            return h('span', null, this.dict.getLabel('bannerPosition', row.position))
          },
        },
        {slot: 'option', label: '操作', width: 280},
      ],
    }
  },
  // 计算
  computed: {
    ...mapState(['user']),
  },
  // 监听
  watch: {},
  created() {
    this.dict.load('bannerType', 'bannerSstatus', 'bannerPosition').then(() => {
      this.getList()
    })
  },
  // 实例渲染后
  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(() => {
      })
    },

    add() {
      this.dialog.title = '添加'
      this.dialogInfo = {}
      this.getList()
      this.visible = true
    },

    data(e) {
      this.files.push(e)
    },
    change(e) {
      this.files = e
    },
    // 确定新增
    addConfirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          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,
            position: this.dialogInfo.position
          }).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>
