<template>
  <div class="AppPageSet">
    <ai-list class="list">
      <ai-title slot="title" title="引导页设置" isShowBottomBorder :instance="instance"></ai-title>
      <template slot="content">
        <ai-search-bar bottomBorder>
          <template #left>
            <el-button type="primary" @click="isShow = true" icon="iconfont iconAdd">添加</el-button>
          </template>
          <template #right>
            <span class="text">是否开启引导页</span> <el-switch v-model="isStart" @change="changeStatus"></el-switch>
          </template>
        </ai-search-bar>
        <div class="content">
          <ai-table
            :tableData="tableData"
            :col-configs="colConfigs"
            :total="total"
            style="margin-top: 16px;"
            :current.sync="search.current"
            :size.sync="search.size"
            @getList="getList">
            <el-table-column
              slot="index"
              type="index"
              width="100px"
              label="序号"
              align="center">
            </el-table-column>
            <el-table-column slot="urlList" label="缩略图" align="center">
              <template slot-scope="{ row }">
                <ai-uploader
                  :instance="instance"
                  disabled
                  v-model="row.urlList"
                  :limit="1">
                </ai-uploader>
              </template>
            </el-table-column>
            <el-table-column slot="options" width="140px" fixed="right" label="操作" align="center">
              <template slot-scope="{ row }">
                <div class="table-options">
                  <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>
        </div>
        <ai-dialog
          :visible.sync="isShow"
          width="890px"
          :title="form.id ? '编辑引导页' : '添加引导页'"
          @close="onClose"
          @onConfirm="onConfirm">
          <el-form ref="form" class="ai-form" :model="form" label-width="110px" label-position="right">
            <el-form-item label="缩略图" style="width: 100%;" prop="urlList" :rules="[{required: true, message: '请上传缩略图', trigger: 'change'}]">
              <ai-uploader
                :instance="instance"
                isShowTip
                v-model="form.urlList"
                :limit="1">
              </ai-uploader>
            </el-form-item>
            <el-form-item label="引导页名称" style="width: 100%" prop="title" :rules="[{required: true, message: '请输入引导页名称', trigger: 'blur'}]">
              <el-input v-model="form.title" size="small" placeholder="请输入引导页名称" maxlength="20"></el-input>
            </el-form-item>
          </el-form>
        </ai-dialog>
      </template>
    </ai-list>
  </div>
</template>

<script>
  export default {
    name: 'AppPageSet',
    label: '引导页设置',

    props: {
      instance: Function,
      dict: Object
    },

    data () {
      return {
        search: {
          current: 1,
          size: 10,
        },
        colConfigs: [
          { slot: 'index', label: '序号', align: 'center'},
          { slot: 'urlList' },
          { prop: 'title', label: '引导页名称', align: 'center' },
          { prop: 'createTime', label: '创建时间', width: '200', align: 'center' },
          { prop: 'createUserName', label: '创建人', width: '200', align: 'center' },
          { slot: 'option'}
        ],
        tableData: [],
        total: 0,
        isStart: true,
        isFlag: false,
        isShow: false,
        form: {
          urlList: [],
          picUrl: '',
          title: ''
        }
      }
    },

    created () {
      this.getList()
      this.getStatus()
    },

    methods: {
      getList () {
        this.instance.post(`/app/appwechatguidepage/list`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            res.data.records.map((item) => {
              item.urlList = [{url: item.picUrl}]
            })
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },
      remove (id) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/appwechatguidepage/delete?ids=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },
      edit (e) {
        this.form = {...e}
        this.form.urlList = [{
          url: e.picUrl
        }]
        this.isShow = true
      },
      onClose () {
        this.form.id = ''
        this.form.urlList = []
        this.form.title = ''
        this.isShow = false
      },
      onConfirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appwechatguidepage/addOrUpdate`, {
              ...this.form,
              picUrl: this.form.urlList[0].url
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功！')
                this.onClose()
                this.getList()
              }
            })
          }
        })
      },
      getStatus() {

        this.instance.post(`/app/appwechatguidepage/enableStatus`).then(res => {
          if (res.code == 0) {
            this.isStart = res.data
            this.isFlag = false
          }
        })
      },
      changeStatus() {
        if(this.isFlag == true) return
        this.isFlag = true
        this.instance.post(`/app/appwechatguidepage/enable`).then(res => {
          if (res.code == 0) {
            this.$message.success(this.isStart == 1 ? '引导页已开启' : '引导页已关闭')
            this.getStatus()
          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .AppPageSet {
    .text {
      color: #666;
      font-size: 14px;
      margin-right: 8px;
    }
  }
</style>
