<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <!-- <el-form-item label="" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="卡名称" prop="cartName">
        <el-input
          v-model="queryParams.cartName"
          placeholder="请输入卡名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="卡号" prop="cartNo">
        <el-input
          v-model="queryParams.cartNo"
          placeholder="请输入卡号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="卡到期时间" prop="cartEndTime">
        <el-date-picker
          v-model="queryParams.cartEndTime"
          clearable
          size="small"
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择卡到期时间"/>
      </el-form-item>
      <el-form-item label="金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期" prop="cartTime">
        <el-date-picker
          v-model="queryParams.cartTime"
          clearable
          size="small"
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"/>
      </el-form-item>
      <el-form-item label="累计充值" prop="leiChong">
        <el-input
          v-model="queryParams.leiChong"
          placeholder="请输入累计充值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计消费" prop="leiXiao">
        <el-input
          v-model="queryParams.leiXiao"
          placeholder="请输入累计消费"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="0：否" prop="deleteFlag">
        <el-input
          v-model="queryParams.deleteFlag"
          placeholder="请输入0：否"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="" prop="gmtCreate">
        <el-date-picker
          v-model="queryParams.gmtCreate"
          clearable
          size="small"
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择"/>
      </el-form-item>
      <el-form-item label="" prop="gmtUpdate">
        <el-date-picker
          v-model="queryParams.gmtUpdate"
          clearable
          size="small"
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择"/>
      </el-form-item>
      <el-form-item label="" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="adminId">
        <el-input
          v-model="queryParams.adminId"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-permission="['unimall:management:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-permission="['unimall:management:edit']"
          :disabled="single"
          type="success"
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-permission="['unimall:management:remove']"
          :disabled="multiple"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
        >删除</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-permission="['unimall:management:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="id" />
      <el-table-column label="" align="center" prop="companyId" /> -->
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="卡名称" align="center" prop="cartName" />
      <el-table-column label="卡号" align="center" prop="cartNo" />
      <el-table-column label="卡到期时间" align="center" prop="cartEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cartEndTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="金额" align="center" prop="money" /> -->
      <el-table-column label="余额" align="center" prop="money" />
      <!-- <el-table-column label="日期" align="center" prop="cartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cartTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="累计充值" align="center" prop="leiChong" />
      <el-table-column label="累计消费" align="center" prop="leiXiao" />
      <el-table-column label="状态" align="center" prop="status" />
      <!--  <el-table-column label="备注2" align="center" prop="remark2" />
      <el-table-column label="备注3" align="center" prop="remark3" /> -->
      <!-- <el-table-column label="0：否" align="center" prop="deleteFlag" /> -->
      <!-- <el-table-column label="" align="center" prop="gmtCreate" width="180"> -->
      <!-- <template slot-scope="scope">
        <span>{{ parseTime(scope.row.gmtCreate) }}</span>
      </template>
      </el-table-column> -->
      <!-- <el-table-column label="" align="center" prop="gmtUpdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtUpdate) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="" align="center" prop="userId" />
      <el-table-column label="" align="center" prop="adminId" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="400px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,1)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,2)"
          >充值</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
          >账单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete1(scope.row)"
          >注销</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />
    <el-dialog :visible.sync="open1" title="账单" width="500px">
      <div v-for="(item,index) in billList" :key="index">
        <div class="row" style="margin-bottom: 10px;">
          <span>{{ item.gmtCreate }}</span>
          <span>{{ item.serviceObject==1?"充值":"消费" }}</span>
          <span>{{ item.money }}元</span>
        </div>
      </div>
    </el-dialog>
    <!-- 添加或修改会员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- <el-form-item label="" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入" />
        </el-form-item> -->
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="form.userName" :disabled="isCZ" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" :disabled="isCZ" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="卡名称" prop="cartName">
          <el-input v-model="form.cartName" :disabled="isCZ" placeholder="请输入卡名称"/>
        </el-form-item>
        <el-form-item label="卡号" prop="cartNo">
          <el-input v-model="form.cartNo" :disabled="isCZ" placeholder="请输入卡号"/>
        </el-form-item>
        <el-form-item v-if="isCZ" label="余额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="卡到期时间" prop="cartEndTime">
          <el-date-picker
            v-model="form.cartEndTime"
            :disabled="isCZ"
            clearable
            size="small"
            style="width: 100%"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择卡到期时间"/>
        </el-form-item>
        <!-- <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item> -->
        <!-- <el-form-item label="日期" prop="cartTime">
          <el-date-picker
            v-model="form.cartTime"
            clearable
            size="small"
            style="width: 200px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"/>
        </el-form-item> -->
        <!-- <el-form-item label="累计充值" prop="leiChong">
          <el-input v-model="form.leiChong" placeholder="请输入累计充值" />
        </el-form-item>
        <el-form-item label="累计消费" prop="leiXiao">
          <el-input v-model="form.leiXiao" placeholder="请输入累计消费" />
        </el-form-item> -->
        <!-- <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="备注1" prop="remark1">
          <el-input v-model="form.remark1" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注2" prop="remark2">
          <el-input v-model="form.remark2" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注3" prop="remark3">
          <el-input v-model="form.remark3" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="0：否" prop="deleteFlag">
          <el-input v-model="form.deleteFlag" placeholder="请输入0：否" />
        </el-form-item> -->
        <el-form-item label="" prop="gmtCreate">
        <!-- <el-date-picker
            v-model="form.gmtCreate"
            clearable
            size="small"
            style="width: 200px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择"/>
        </el-form-item>
        <el-form-item label="" prop="gmtUpdate">
          <el-date-picker
            v-model="form.gmtUpdate"
            clearable
            size="small"
            style="width: 200px"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择"/>
        </el-form-item>
        <el-form-item label="" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="adminId">
          <el-input v-model="form.adminId" placeholder="请输入" />
        </el-form-item> -->
      </el-form-item></el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManagement, getManagement, delManagement, addManagement, updateManagement, exportManagement, chongzhi } from '@/api/unimall/management'
import { listMoneyHis, getMoneyHis, delMoneyHis, addMoneyHis, updateMoneyHis, exportMoneyHis } from '@/api/unimall/moneyHis'
export default {
  data() {
    return {
      billList: [],
      isCZ: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 会员管理表格数据
      managementList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        companyId: undefined,
        userName: undefined,
        phone: undefined,
        cartName: undefined,
        cartNo: undefined,
        cartEndTime: undefined,
        money: undefined,
        cartTime: undefined,
        leiChong: undefined,
        leiXiao: undefined,
        status: undefined,
        remark1: undefined,
        remark2: undefined,
        remark3: undefined,
        deleteFlag: undefined,
        gmtCreate: undefined,
        gmtUpdate: undefined,
        userId: undefined,
        adminId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deleteFlag: [
          { required: true, message: '0：否（默认） 1：是不能为空', trigger: 'blur' }
        ] }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询会员管理列表 */
    getList() {
      this.loading = true
      listManagement(this.queryParams).then(response => {
        this.managementList = response.data.data.items
        this.total = response.data.data.count
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        companyId: undefined,
        userName: undefined,
        phone: undefined,
        cartName: undefined,
        cartNo: undefined,
        cartEndTime: undefined,
        money: undefined,
        cartTime: undefined,
        leiChong: undefined,
        leiXiao: undefined,
        status: '0',
        remark1: undefined,
        remark2: undefined,
        remark3: undefined,
        deleteFlag: undefined,
        gmtCreate: undefined,
        gmtUpdate: undefined,
        userId: undefined,
        adminId: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加会员管理'
    },
    // 查账单
    handleUpdate1(row, type) {
      const id = row.id || this.ids.join(',')
      listMoneyHis({
        xiaofeiId: id
      }).then(response => { debugger
        this.form = response.data.data
        this.open1 = true
        this.billList = response.data.data.items
        for (let i = 0; i<this.billList.length; i++) {
          this.billList[i].gmtCreate = this.parseTime(this.billList[i].gmtCreate, '{y}-{m}-{d} {h}:{i}:{s}')
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      // 1修改2充值
      this.reset()
      const id = row.id || this.ids.join(',')
      getManagement(id).then(response => {
        this.form = response.data.data
        this.open = true
        this.form.cartEndTime = this.parseTime(this.form.cartEndTime, '{y}-{m}-{d}')
      })
      if (type==2) {
        this.isCZ = true
        this.title = '充值'
      } else {
        this.isCZ = false
        this.title = '修改会员管理'
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!this.form.userName) {
            this.msgError('姓名不能为空！')
            return
          }
          if (!this.form.phone) {
            this.msgError('手机号不能为空！')
            return
          }
          if (!this.form.cartNo) {
            this.msgError('卡号不能为空！')
            return
          }
          if (!this.form.cartName) {
            this.msgError('卡名称不能为空！')
            return
          }
          if (this.form.id != undefined) {
            if (this.isCZ) {
              chongzhi({
                id: this.form.id,
                money: this.form.money
              }).then(response => {
                if (response.data) {
                  this.msgSuccess('充值成功')
                  addMoneyHis({
                    xiaofeiId: this.form.id,
                    serviceObject: 1,
                    money: this.form.money,
                    remark1: this.form.userName,
                    remark2: this.form.phone,
                    remark3: this.form.cartNo

                  }).then(response => {
                    if (response.data) {
                      this.msgSuccess('充值记录添加成功')
                      this.open = false
                      this.getList()
                    } else {
                      this.msgError(response.msg)
                    }
                  })
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              this.form.status = '正常'
              updateManagement(this.form).then(response => {
                if (response.data) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          } else {
            this.form.remark1 = this.form.money
            this.form.status = '正常'
            addManagement(this.form).then(response => { debugger
              if (response.data) {
                if (!response.data.data) {
                  this.msgError('手机号或卡号重复')
                  return
                }
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids.join(',')
      this.$confirm('是否确认删除会员管理编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delManagement(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {})
    },
    handleDelete1(row) {
      const ids = row.id || this.ids.join(',')
      this.$confirm('是否确认注销会员管理编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return updateManagement({ id: ids, status: '已注销' })
      }).then(() => {
        this.getList()
        this.msgSuccess('注销成功')
      }).catch(function() {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有会员管理数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportManagement(queryParams)
      }).then(response => {
        this.download(response.data.data)
      }).catch(function() {})
    }
  }
}
</script>
