<template>
  <section class="AppUserList">
    <ai-list>
      <ai-title slot="title" title="用户管理" isShowBottomBorder />
      <template #content>
        <ai-search-bar>
          <template #right>
            <el-input v-model="search.title" class="search-input" size="small" v-throttle="() => {(page.current = 1), getList()} " placeholder="请输入手机号" clearable @change="getList" @clear="page.current = 1, (search.title = ''), getList()" suffix-icon="iconfont iconSearch">
            </el-input>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size" @getList="getList" :col-configs="colConfigs" 
          :dict="dict" @sort-change="sortChange">
          <el-table-column slot="chooseNumber" label="积分数量" align="center" sortable>
          </el-table-column>
          <el-table-column slot="options" label="操作" fixed="right" align="center">
            <template slot-scope="{ row }">
              <el-button type="text" @click.native="changeIntegral(row)">调整积分</el-button>
            </template>
          </el-table-column>
        </ai-table>
        <ai-dialog
          title="调整积分"
          :visible.sync="dialog"
          :destroyOnClose="true"
          width="720px"
          @onConfirm="onConfirm"
          @closed="form={}">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="类型" prop="integralCalcType">
            <ai-select v-model="form.integralCalcType" :selectList="dict.getDict('integralCalcType')"/>
          </el-form-item>
          <el-form-item label="积分" prop="integral">
            <el-input v-model.trim="form.integral" placeholder="请输入正数" size="small"></el-input>
          </el-form-item>
        </el-form>
      </ai-dialog>
      </template>
    </ai-list>
  </section>
</template>

<script>
export default {
  name: "AppUserList",
  label: "用户管理",
  props: {
    instance: Function,
    dict: Object,
  },
  data () {
    return {
      search: {
        title: '',
      },
      page: {
        current: 1,
        size: 10,
        total: 0,
      },
      tableData: [],
      dialog: false,
      form: {
        integralCalcType: "",
        integral: '',
      },
    }
  },
  created () {
    this.$dict.load('integralCalcType', 'electionMethod').then(()=> {
      this.getList()
    })
  },
  computed: {
    colConfigs() {
      return [
        {prop: "title", label: "用户", align: "left"},
        {prop: "organizationName", label: "手机号", align: "center"},
        {prop: "electionMethod", label: "等级", align: "center",dict:"electionMethod"},
        {prop: "chooseNumber", label: "积分数量", align: "center", sortable: "custom"},
        {slot: "options", },
      ]
    },
    rules() {
      return {
        integralCalcType: [{required: true, message: '请选择类型', trigger: 'change'}],
        integral: [{required: true, message: '请输入积分', trigger: 'blur' },
        {pattern: /^([1-9]\d*|0)(\.\d{1,2})?$/, message: '请输入正数且最多只能保留两位小数'}],
      }
    },
  },
  methods: {
    sortChange(col) {
      console.log(col.order)
      if(col.prop === 'chooseNumber') { // 剩余积分
        // this.search.sortFiled = 0
        // if(col.order === 'ascending') {
        //   this.search.sortRule = true
        // } else if(col.order === 'descending') {
        //   this.search.sortRule = false
        // } else if(col.order === null) {
        //   this.search.sortRule = ''
        // }
      }
    },
    changeIntegral(row) {
      this.dialog = true
    },
    onConfirm() {
      this.$refs.form.validate((valid)=> {
        if(valid) {
          this.flag = true
          this.instance.post(`/app/appintegraluser/changeIntegral`,{
            ids: this.form.ids,
            eventDesc: this.form.eventDesc,
            enclosure: this.form.enclosure,   // 附件
            integralCalcType: this.form.integralCalcType,
            integral: this.form.integral,
          }).then(res => {
            if(res?.code == 0) {
              this.$message.success('调整积分成功')
              setTimeout(() =>{
                this.dialog = false
                this.getTableData()
                this.flag = false
              }, 600)
            } else {
              this.flag = false
            }
          })
        }
      })

    },
    getList() {
      this.instance.post(`/app/appgeneralelectioninfo/list`,null,{
        params: {
          ...this.page,
          ...this.search,
        }
      }).then(res=> {
        if(res?.data) {
          this.tableData = res.data.records
          this.page.total = res.data.total
        }
      })
    },
    toAdd(id) {
      this.$emit('change', {
        type: 'electionAdd',
        params: {
          id: id || '',
        }
      })
    },
    handleDelete(id) {
      this.$confirm('确定删除该数据？').then(() => {
        this.instance.post(`/app/appgeneralelectioninfo/delete?ids=${id}`).then(res=>{
          if(res.code == 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },
    reset() {
      this.search = {
        status: '',
        title: '',
      }
      this.getList()
    },
    // 开启、结束
    startEnd(id, status) {
      let title = ''
      let bool = null
      let tips = ''
      if(status == 0) {
        title = '未到投票开始时间，确定要提前开始吗？'
        bool = true
        tips = '开启成功'
      } else if(status == 1) {
        title = '投票正在进行中，确定要提前结束吗？'
        bool = false
        tips = '结束成功'
      }
      this.$confirm(title).then(() => {
        this.instance.post(`/app/appgeneralelectioninfo/start-end?id=${id}&start=${bool}`).then(res=>{
          if(res.code == 0) {
            this.$message.success(tips)
            this.getList()
          }
        })
      })
    },
    // 统计
    toStatistics(id) {
      this.$emit('change', {
        type: 'Statistics',
        params: {
          id: id || '',
        }
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.AppUserList {
  height: 100%;
}
</style>
