<template>
  <ai-list class="Learning">
    <ai-title
      slot="title"
      title="消息推送"
      isShowBottomBorder>
    </ai-title>
    <template slot="content">
      <ai-search-bar>
        <template #left>
          <el-button type="primary" @click="getQRCodeUrl(), isShow = true">绑定公众号</el-button>
        </template>
        <template #right>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        :current.sync="search.current"
        :size.sync="search.size"
        style="margin-top: 8px;"
        @getList="getList">
        <el-table-column slot="send" label="是否发送通知" align="center">
          <template v-slot="{ row }">
            <el-switch
              active-value="1"
              inactive-value="0"
              @change="e => onChange(row.id, e)"
              v-model="row.isSend">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column slot="options" label="操作" align="center" fixed="right" width="140px">
          <template v-slot="{ row }">
            <div class="table-options">
              <el-button type="text" @click="edit(row)">修改</el-button>
              <el-button type="text" @click="remove(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <AiDialog
        title="扫码绑定微信公众号"
        :visible.sync="isShow"
        :close-on-click-modal="false"
        customFooter
        width="380px">
        <img style="width: 300px" :src="qrcode" />
        <div class="dialog-footer" slot="footer">
          <el-button @click="isShow = false">取消</el-button>
          <el-button @click="getList(), isShow = false" type="primary">我已扫码</el-button>
        </div>
       </AiDialog>
      <AiDialog
        title="修改信息"
        :visible.sync="isShowForm"
        :close-on-click-modal="false"
        width="580px"
        @close="form.name = '', id = ''"
        @confirm="onConfirm">
        <el-form :model="form" ref="form" label-width="100px">
          <el-form-item label="姓名" prop="name" :rules="[{required: true, message: '请输入姓名', trigger: 'blur'}]">
            <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="店铺" style="width: 100%;" prop="mallId">
            <el-select style="width: 380px" v-model="form.mallId" clearable placeholder="请选择" @change="mallChange">
              <el-option
                v-for="item in $store.state.mallList"
                :key="item.mallId"
                :label="item.mallName"
                :value="item.mallId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
       </AiDialog>
    </template>
  </ai-list>
</template>

<script>
  export default {
    data () {
      return {
        colConfigs: [
          { prop: 'name', label: '姓名', align: 'left' },
          { prop: 'mallName', label: '店铺名称', align: 'left', format: v =>  !v ? '所有店铺': v  },
          { slot: 'send' },
          { prop: 'createTime', label: '绑定时间', align: 'center' },
        ],
        tableData: [],
        total: 0,
        qrcode: '',
        isShow: false,
        search: {
          current: 1,
          size: 10
        },
        form: {
          name: '',
          mallId: '',
          mallName: ''
        },
        isShowForm: false,
        sence: '',
        id: ''
      }
    },

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

    methods: {
      onChange (id, isSend) {
        console.log(isSend)
        this.$http.post(`/api/malluser/updateWxuserStatus?id=${id}&isSend=${isSend}`).then(res => {
          if (res.code === 0) {
            this.$message.success('操作成功')
            this.getList()
          }
        })
      },

      onConfirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$http.post('/api/malluser/updateWxuserInfo', {
              id: this.id,
              name: this.form.name,
              mallId: this.form.mallId,
              mallName: this.form.mallName
            }).then(res => {
              if (res.code === 0) {
                this.isShowForm = false
                this.getList()
                this.$message.success('编辑成功')
              }
            })
          }
        })
      },

      edit (e) {
        this.id = e.id
        this.form.name = e.name
        this.isShowForm = true
      },

      remove (e) {
        this.$confirm('确定要删除该条数据？', '温馨提示', {
          type: 'warning'
        }).then(() => {
           this.$http.post(`/api/malluser/delWxuser?id=${e.id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },

      getQRCodeUrl () {
        this.$http.post('/api/malluser/getQRCodeUrl').then(res => {
          if (res.code === 0) {
            this.qrcode = res.data.picUrl
            this.sence = res.data.sence
          }
        })
      },

      getList () {
          this.$http.post('/api/malluser/getWxuserList', null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code === 0) {
            this.tableData = res.data
            this.total = res.data.length
          }
        })
      },
      mallChange() {
        console.log(this.form.mallId)
        if (!this.form.mallId) {
          this.form.mallName = ''
          return
        }
        let mallInfo = this.$store.state.mallList.filter(item => {
          return item.mallId == this.form.mallId
        })
        this.form.mallName = mallInfo[0].mallName
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
