<template>
  <ai-list class="notice">
    <ai-title slot="title" title="居民积分" v-model="search.areaId" isShowBottomBorder isShowArea :hideLevel="hideLevel - 1" @change="search.current = 1, getList()"></ai-title>
    <template slot="content">
      <ai-search-bar class="search-bar">
        <template #left>
          <el-button type="primary" size="small" icon="iconfont iconAdd" @click="changeIntegral('',0)">&nbsp;批量调整积分</el-button>
          <ai-download
            :instance="instance"
            url="/app/appwechatuserqujing/export"
            :params="params"
            v-if="permissions('app_appwechatuserqujing_export')"
            fileName="居民积分"
             :disabled="tableData.length == 0">
            <el-button icon="iconfont iconExported" :disabled="tableData.length == 0">导出</el-button>
          </ai-download>
        </template>
        <template #right>
          <el-input
            v-model="search.idNumber"
            class="search-input"
            size="small"
            v-throttle="() => {search.current = 1, getList()}"
            placeholder="姓名/身份证/手机号"
            clearable
            @clear="search.current = 1, search.idNumber = '', getList()"
            suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        style="margin-top: 6px;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="options" width="120px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="toDetail(row.integralUserId)">详情</el-button>
              <el-button type="text" @click="changeIntegral(row,1)">调整积分</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog
        title="调整积分"
        :visible.sync="dialog"
        :destroyOnClose="true"
        width="720px"
        @onConfirm="onConfirm"
        @closed="form={},chooseUserList=[]">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="选择人员" prop="ids" v-if="!isEdit">
            <ai-person-select :instance="instance" keys="openId" customRightText :customClicker="true" :chooseUserList="chooseUserList"
                :url="`/app/appwechatuserqujing/listByFdAppletUser?areaId=${search.areaId}`" headerTitle="用户列表"
                :isMultiple="true" dialogTitle="选择" @selectPerson="selectPerson" class="aipersonselect">
              <template name="option" v-slot:option="{ item }">
                <span class="iconfont iconProlife">{{ item.realName }}</span>
                <!-- <ai-id mode="show" :show-eyes="false" :value="item.idNumber"/> -->
                <span>{{ item.phone }}</span>
              </template>
            </ai-person-select>
          </el-form-item>
          <el-form-item label="调整说明" prop="eventDesc">
            <el-input v-model="form.eventDesc" placeholder="请输入..." type="textarea" :rows="4" show-word-limit
                      maxlength="100"></el-input>
          </el-form-item>
          <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>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'List',

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

    data() {
      return {
        search: {
          current: 1,
          size: 10,
          idNumber: '',
          areaId: ''
        },
        form: {
          ids: [],
          eventDesc: "",
          enclosure: "",
          integralCalcType: "",
          integral: ''
        },
        dialog: false,
        chooseUserList: [],
        total: 0,
        isEdit: false,
        colConfigs: [
          { prop: 'realName',  label: '姓名', align: 'left', width: '200px' },
          { prop: 'phone',  label: '手机号', align: 'center' },
          { prop: 'idNumber', label: '身份证号', align: 'center' },
          { prop: 'areaName', label: '所属地区', align: 'center' },
          { prop: 'girdName', label: '所属网格', align: 'center' },
          { prop: 'integral', label: '剩余积分', align: 'center' },
          { prop: 'usedIntegral', label: '已用积分', align: 'center' }
        ],
        tableData: []
      }
    },

    computed: {
      ...mapState(['user']),

      hideLevel () {
        return this.user.info.areaList.length || 0
      },

      params () {
        return {
          ...this.search,
          size: 1000
        }
      },

      rules() {
        return {
          ids: [{required: true, message: '请选择人员', trigger: 'blur'}],
          eventDesc: [{required: true, message: '请输入调整说明', trigger: 'blur'}],
          integralCalcType: [{required: true, message: '请选择类型', trigger: 'blur'}],
          integral: [{required: true, message: '请输入积分', trigger: 'blur' },
          {pattern: /^([1-9]\d*|0)(\.\d{1,2})?$/, message: '请输入正数且最多只能保留两位小数'}],
        }
      }
    },

    created() {
      this.search.areaId = this.user.info.areaId
      this.dict.load('integralCalcType').then(() => {
        this.getList()
      })
    },

    methods: {
      getList () {
        this.instance.post(`/app/appwechatuserqujing/listByFdAppletUser`, null, {
          params: {
            ...this.search,
            areaId: this.search.areaId
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      selectPerson(val) {
        if (val) {
          this.personList = val
          this.form.ids = [...this.personList.map(e => e.openId)]
        } else {
          this.form.ids = this.chooseUserList.map(e => e.openId)
        }
      },

      changeIntegral(row,type) {
        this.isEdit = false
        if(type==0) {
          this.dialog = true
        } else if(type ==1) {
          this.isEdit = true
          this.chooseUserList = [{
            openId: row.openId,
            name: row.realName
          }]
          this.form = {
            ids: this.chooseUserList.map(e => e.openId)
          }

          this.dialog = true
        }
      },

      changeTableSort(col) {
        if(col.prop === 'integral') { // 剩余积分
          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 = ''
          }
        } else if(col.prop === 'totalIntegral') {  // 累计积分
          this.search.sortFiled = 1
          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 = ''
          }
        } else if(col.prop === 'usedIntegral') {  // 已用积分
          this.search.sortFiled = 2
          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 = ''
          }
        }
        this.getList()
      },

      onConfirm() {
        if(this.flag) return

        if(this.form.file?.length) {
          this.form.enclosure = this.form.file[0].url
        }
        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,
              integralUserType: 3
            }).then(res => {
              if(res?.code == 0) {
                this.$message.success('调整积分成功')
                setTimeout(() =>{
                  this.dialog = false
                  this.getList()
                  this.flag = false
                }, 600)
              } else {
                this.flag = false
              }
            })
          }
        })

      },

      toDetail(id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id: id || ''
          }
        })
      }
    }
  }
</script>

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