<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('book:user:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('book:user:delete')" type="danger" @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection- change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (pageIndex - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="">
      </el-table-column> -->
      <el-table-column prop="name" header-align="center" align="center" label="姓名">
      </el-table-column>
      <el-table-column prop="age" header-align="center" align="center" label="年龄">
      </el-table-column>
      <el-table-column prop="sex" header-align="center" align="center" label="性别">
        <template slot-scope="scope">
          {{scope.row.sex == 0 ? '女':'男'}}
        </template>
      </el-table-column>
      <el-table-column prop="avatar" header-align="center" align="center" label="头像">
        <template slot-scope="scope">
          <img v-if="scope.row.avatar != ''" :src="scope.row.avatar" width="50" height="50" class="tableImg" />
        </template>
      </el-table-column>
      <el-table-column prop="nickname" header-align="center" align="center" label="昵称">
      </el-table-column>
      <el-table-column prop="tel" header-align="center" align="center" label="电话">
      </el-table-column>
      <!-- <el-table-column
        prop="password"
        header-align="center"
        align="center"
        label="密码">
      </el-table-column> -->
      <el-table-column prop="vip" header-align="center" align="center" label="会员类型">
      </el-table-column>
      <!-- <el-table-column
        prop="vipValidtime"
        header-align="center"
        align="center"
        label="vip 有效期">
      </el-table-column> -->
      <el-table-column prop="peanutCoin" header-align="center" align="center" label="疯币">
      </el-table-column>
      <!-- <el-table-column
        prop="readTime"
        header-align="center"
        align="center"
        label="阅读时间">
      </el-table-column> -->
      <el-table-column prop="lastLoginTime" header-align="center" align="center" label="最后登录时间">
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
      </el-table-column>
      <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间">
      </el-table-column>
      <!-- <el-table-column
        prop="delFlag"
        header-align="center"
        align="center"
        label="删除标记">
      </el-table-column> -->
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="ck(scope.row)">充/扣疯币</el-button>
          <el-button type="text" size="small" @click="youhui(scope.row)">优惠券列表</el-button>
          <br>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current- change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>

    <el-dialog title="充/扣疯币" :close-on-click-modal="false" :visible.sync="adc" append-to-body width="30%">
      <el-form :model="pointForm" label-width="100px">
        <el-form-item label="用户">
          {{pointForm.tel}} <span v-if="pointForm.name!=''">({{pointForm.name}})</span>
        </el-form-item>
        <el-form-item label="充值类型">
          <!-- <el-input v-model="pointForm.pointType" placeholder="0-普通 1-vip"></el-input>
             -->
          <el-select v-model="pointForm.pointType" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="金额">
          <el-input-number v-model="pointForm.pointAmount" placeholder="">
          </el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adc=false">取消</el-button>
        <el-button type="primary" @click="huaSheng">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="优惠券列表" :close-on-click-modal="false" :visible.sync="youVisible" append-to-body width="600px">
      <el-form :model="youForm" label-width="100px">
        <el-form-item label="用户">
          {{youForm.tel}} <span v-if="youForm.name!=''">({{youForm.name}})</span>
        </el-form-item>
        <el-form-item label="赠送优惠券">
          <el-select v-model="youForm.courType" placeholder="请选择">
            <el-option v-for="item in courperList" :key="item.id" :label="item.couponName" :value="item.id">
            </el-option>
          </el-select>
          <el-button type="primary" @click="surYou" :disabled="youForm.courType==undefined">赠送</el-button>
        </el-form-item>
        <el-table :data="courperHistList" border :key='Math.random()'>
          <el-table-column prop="couponName" label="已获得优惠券">
          </el-table-column>
          <el-table-column label="操作" header-align="center" align="center" width="80">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="courDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="youVisible=false">取消</el-button>

      </span>
    </el-dialog>

  </div>
</template>

<script>
  import AddOrUpdate from './user-add-or-update'
  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        options: [{
          value: 0,
          label: '充疯币'
        }, {
          value: 1,
          label: '扣疯币'
        }],
        adc: false,
        pointForm: {
          pointType: 0,
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        youVisible: false,
        youForm: {},
        courperList: [],
        courperHistList: [],
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
      this.getcourpeList()
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/book/user/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 200,
            'key': ''
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
            this.dataListLoading = false
          }
        })
      },
      // 获取优惠券列表
      getcourpeList() {
        this.$http({
          url: this.$http.adornUrl('/book/coupon/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'currentState': '1'
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.courperList = data.page.list
            this.totalPage = data.page.totalCount
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/book/user/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({
            data
          }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      // 充值扣款疯币
      ck(e) {
        this.pointForm=e
        this.adc = true
      },
      huaSheng(){
        console.log(this.pointForm)
        this.$http({
          url: this.$http.adornUrl(`/book/user/pointChange?pointType=${this.pointForm.pointType}&pointAmount=${this.pointForm.pointAmount}&id=${this.pointForm.id}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.adc = false
          }
        })
      },

      // 个人优惠券
      youhui(e) {
        this.youForm = e
        this.youForm.memberId = e.id
        this.$http({
          url: this.$http.adornUrl('/book/couponhistory/list'),
          method: 'post',
          params: this.$http.adornParams({
            'userId': e.id
          })
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.courperHistList = data.page.list
            this.youVisible = true
          }
        })

      },
      surYou() {
        let arrList = {}
        arrList.memberId = this.youForm.memberId
        arrList.couponId = this.youForm.courType
        arrList.memberNickname = this.youForm.nickname
        arrList.useStatus = 0

        this.$http({
          url: this.$http.adornUrl('/book/couponhistory/save'),
          method: 'post',
          data: this.$http.adornData(arrList, false)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: data.msg,
              type: 'success',
              duration: 1500,
            })
            this.youhui(this.youForm)
            this.getDataList()
          }else{
            this.$message({
              message: data.msg,
              type: 'error',
              duration: 1500,
            })
          }
        })
      },
      // 删除优惠券
      courDelete(e) {
        let arrList = []
        arrList.push(e.id)
        this.$http({
          url: this.$http.adornUrl('/book/couponhistory/delete'),
          method: 'post',
          data: this.$http.adornData(arrList, false)
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1500,
            })
            this.youhui(this.youForm)
            this.youVisible = true
          }
        })
      }
    }
  }
</script>
