<template>
  <div v-if="pageLoading" class="resi-container">
    <el-card ref="searchCard" class="search-card">
      <div class="">

        <el-form ref="searchForm" :inline="true" :model="searchForm" size="small" label-width="100px" class="demo-form-inline">
          <div>
            <el-form-item label="户主姓名" prop="name">
              <el-input v-model="searchForm.name" placeholder="请输入" class="input-width" clearable></el-input>
            </el-form-item>
          </div>
          <el-form-item>
            <el-button class="diy-button--search" size="small" @click="handleSearch">查询</el-button>
            <el-button class="diy-button--reset" size="small" @click="resetForm('searchForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="resi-card-table">
      <el-button  class="diy-button--search" type="primary" style="margin-bottom: 10px;"  @click="handelClickBatch">批量调整</el-button>
      <el-table
        :data="tableData"
        row-key="id"
        v-loading="tableLoading"
        border
        :height="tableHeight"
        style="width: 100%"
        class="resi-table"
        @select="handleSelection"
      >
        <el-table-column label="序号" type="index" align="center" width="50">
        </el-table-column>
        <el-table-column type="selection"
                         width="55"
                         fixed="left"></el-table-column>
        <el-table-column
          prop="name"
          label="户主姓名"
          align="center"
          width="100"
        />
        <el-table-column
          prop="mobile"
          label="手机号"
          align="center"
          min-width="100"
        />
        <el-table-column
          prop="fullName"
          label="家庭地址"
          align="center"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          prop="total"
          label="积分"
          align="center"
          show-overflow-tooltip
          min-width="100"
        />

        <el-table-column fixed="right" label="操作" align="center" width="150">
          <template slot-scope="scope">
            <el-button
              @click="handleLook(scope.row)"
              type="text"
              size="small"
              class="btn-color-look"
              >积分记录</el-button>
            <el-button
                @click="handlePoint(scope.row)"
                type="text"
                size="small"
                class="btn-color-look"
            >调整积分</el-button>
          </template>
        </el-table-column>
      </el-table>


      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-sizes="[20, 50, 100, 200]"
          :page-size="pageSize"
          layout="sizes, prev, pager, next, total"
          :total="total"
        >
        </el-pagination>
      </div>


    </el-card>

    <el-dialog title="积分记录" :visible.sync="dialogVisible" width="800px">
      <el-table
          :data="detailInfo"
          row-key="id"
          v-loading="tableLoading"
          border
          :height="tableHeight"
          style="width: 100%"
          class="resi-table"
      >
        <el-table-column label="序号" type="index" align="center" width="50">
        </el-table-column>
        <el-table-column
            prop="createdName"
            label="调整人"
            align="center"
            width="150"
        />
        <el-table-column
            prop="createdTime"
            label="调整时间"
            align="center"
            width="150"
        />
        <el-table-column
            prop="statement"
            label="调整原因"
            align="center"
            width="150"
        />
        <el-table-column
            prop="pointValue"
            label="调整积分"
            align="center"
            width="150"
        />
        <el-table-column
            prop="businessCode"
            label="调整方式"
            align="center"
            width="150"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.businessCode === 'point_reward'">积分奖励</span>
            <span v-else-if="scope.row.businessCode === 'point_fine'">积分扣罚</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handlerCancle">关闭</el-button>
      </div>
    </el-dialog>






    <el-dialog title="调整积分" :visible.sync="dialogFormVisiblePoint" width="800px">
        <el-form label-width="100px">
          <el-form-item label="当前积分">
            <div class="info-prop">
              <span>{{ pointTotal}}</span>
            </div>
          </el-form-item>
          <el-form-item label="调整类型" :rules = "[ { required: true, message: '请选择调整类型', trigger: 'change' }]">
            <el-radio v-model="businessCode" label="point_reward">积分奖励</el-radio>
            <el-radio v-model="businessCode" label="point_fine">积分扣罚</el-radio>
          </el-form-item>
          <el-form-item label="调整额度" :rules = "[ { required: true, message: '请选择调整额度', trigger: 'change' }]">
            <el-input-number v-model="pointValue" :min="1" :max="100"></el-input-number>
          </el-form-item>
          <el-form-item label="调整原因">
            <el-input v-model="statement" type="textarea"
                      maxlength="100"
                      :rows="4" style="width: 300px"></el-input>
          </el-form-item>
        </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handlerSubmit">确定</el-button>
        <el-button @click="handlerCancle">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      dialogVisible: false,
      dialogFormVisible: false,
      dialogFormVisiblePoint: false,
      tableLoading: false,
      disabled: false,
      pageLoading: false,
      currentPage: 1,
      pageSize: 20,
      total: null,
      tableData: [],
      searchH: 0,
      addType: 'add',
      optionsG: [],
      zwLists: [
        {
          label: '通过',
          value: '2'
        },
        {
          label: '驳回',
          value: '1'
        }
      ],
      zwList: [
        {
          label: '待审核',
          value: '0'
        },
        {
          label: '已通过',
          value: '2'
        },
        {
          label: '已驳回',
          value: '1'
        }
      ],
      selectionList: [],
      tempList: [],
      partyOrgRange: [],
      timeRange: [],
      customerId: '',
      isBatch: false,
      searchForm: {
        name: ''
      },
      form: {
        ids: [],
        status: '2',
        remark: ''
      },
      detailInfo: [],
      partyProps: {
        label: 'partyOrgName',
        value: 'id',
        checkStrictly: true
      },

      businessCode:"point_reward",
      statement:"",
      pointValue:0,
      houseId:"",
      pointTotal:"",
      seleceIds:[]
    }
  },

/*   rules:{

    businessCode: [
      { required: true, message: '请选择调整类型', trigger: 'change' }
    ],

  }, */

   computed: {
    ...mapGetters(['clientHeight', 'iframeHeight']),
    tableHeight() {
      const h = this.clientHeight - this.searchH - 280 + this.iframeHeigh
      const _h = this.clientHeight - 280 - this.searchH
      return this.$store.state.inIframe ? h : _h
    }
  },
  async created() {
    this.customerId = localStorage.getItem('customerId')
    this.getTableData()
    this.pageLoading = true

  },

  mounted() {

    this.$nextTick(() => {
      this.searchH = this.$refs.searchCard.$el.offsetHeight
      console.log('tableHeight', this.tableHeight)
    })

  },
  methods: {
    handelClickBatch(){
      if(!this.selectionList.length != 0){
        this.$message.error('请选择需要操作的房屋')
      }else{
       this.dialogFormVisiblePoint = true
      this.seleceIds = this.selectionList.map(item=>item.subjectId)
      }
    },
    handleSelection(val) {
      this.selectionList = [...val]
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      if (this.selectionList.length > 0 && val !== 1) {
        this.tempList = [...this.tempList, ...this.selectionList]
      }
      this.getTableData()
    },
    handlePartyChange(val) {
      console.log('val------pp', val)
      this.searchForm.partyOrgId = val[val.length - 1]
    },
    handleFilterSpan(row, item) {
      let _val = ''
      if (item.options && item.options.length > 0) {
        item.options.forEach((n) => {
          if (n.value === row[item.columnName]) _val = n.label
        })
      }
      return _val || row[item.columnName]
    },
    handleTimeChange (val) {
      if (val.length > 0) {
        this.searchForm.startTime = val[0]
        this.searchForm.endTime = val[1]
      } else {
        this.searchForm.startTime = ''
        this.searchForm.endTime = ''
      }

    },

    handleDelimg(item, index) {
      this.form.imageList.splice(index, 1)
    },
    handleSearch(val) {
      console.log('searchhh--', val)
      this.currentPage = 1
      this.getTableData()
    },
    resetForm(formName) {
      this.timeRange = []
      this.searchForm.startTime = ''
      this.searchForm.endTime = ''
      this.$refs[formName].resetFields()
      this.handleSearch()
    },
    handleAdd() {
      if (this.selectionList.length == 0) return this.$message.warning('请选择数据')
      this.isBatch = true
      this.dialogFormVisible = true
    },

    handlerCancle() {
      this.detailInfo = []
      this.selectionList = []
      this.tempList = []
      this.disabled = false
      this.isBatch = false
      this.form = {
        ids: [],
        status: '2',
        remark: ''
      }
      this.businessCode = ""
      this.statement = ""
      this.pointValue = 0
      this.dialogFormVisible = false
      this.dialogFormVisiblePoint = false
      this.dialogVisible = false
    },
    async handleLook(row) {
      this.disabled = true
      this.isBatch = false
      await this.getDetail(row.subjectId)
      this.dialogFormVisible = true
    },

    async handlePoint(row) {

      this.dialogFormVisiblePoint = true
      this.houseId = row.subjectId
      this.pointTotal = row.total

    },

    async handleEdit(row, addType) {
      this.form.ids = [row.id]
      this.isBatch = false
      // this.detailInfo = { ...row }
      await this.getDetail(row.id)
      this.dialogFormVisible = true
    },

    handleDel(row) {
      let params = {
        ids: [row.id]
      }
      console.log('row1', row)
      this.$http
        .post('/resi/partymember/icPartyMember/delete', [row.id])
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.$message.success('删除成功')
            this.getTableData()
          }
        })
        .catch((err) => {
          return this.$message.error('网络错误')
        })
    },
    async handlerSubmit() {

      if (this.businessCode === 'point_fine' && this.pointTotal <  this.pointValue){
        return this.$message.error('扣罚奖励不得大于当前积分')
      }

      let params = {
        houseId: this.houseId,
        businessCode: this.businessCode,
        statement: this.statement,
        pointValue: this.pointValue,
        ids:this.seleceIds
      }
      // console.log(params)
      this.$http
        .post('/point/reward/adjustPoints', params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
            // this.dialogFormVisiblePoint = false
          } else {
            this.$message.success('操作成功')
            this.handlerCancle()
            this.getTableData()
          }
        })
        .catch((err) => {
          return this.$message.error('网络错误')
        })

    },
    async getDetail(id) {
      let params = {
        houseId: id,
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }
      this.$http
        .post(`/point/reward/getPointsRecord`,params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            // this.detailInfo = { ...res.data.list }
            this.detailInfo = res.data.list
            // console.log(this.detailInfo)
            if (this.disabled) {
              this.form.status = res.data.status
              this.form.remark = res.data.remark
            }
            this.dialogVisible = true
          }
        })
        .catch((err) => {
          console.log('row4', err)
          return this.$message.error('网络错误')
        })
    },
    deepArrTOnull(arr) {
      let a = []
      a = arr.map(item => {
        return {
          ...item,
          children: (item.children.length > 0 && this.deepArrTOnull(item.children) )|| null
        }
      })
      return a
    },
    async getTableData() {
      this.tableLoading = true
      // const { user } = await this.$store.state
      let params = {
        ...this.searchForm,
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }
      await this.$http
        .post('/point/reward/getHomeList', params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.tableData = res.data.list
            this.total = res.data.total
          }
        })
        .catch(() => {
          return this.$message.error('网络错误')
        })
      this.tableLoading = false
    }

  }
}
</script>

<style lang="scss" scoped>
@import "@/assets/scss/buttonstyle.scss";

.resi-container .resi-card-table {
  ::v-deep .el-table th {
    color: #fff;
    background-color: rgba(33, 149, 254, 1);
    // border-right: 1px solid rgba(33, 149, 254, 1);
  }
}
.resi-table {
  ::v-deep .el-button--text {
    text-decoration: underline;
  }
  ::v-deep .btn-color-del {
    margin-left: 10px;
    color: rgba(213, 16, 16, 1);
  }
  ::v-deep .btn-color-edit {
    color: rgba(0, 167, 169, 1);
  }
}
.input-width {
  width: 200px;

}
.form-wr {
  .input-width {
    width: 260px;

  }
  .input-width-textarea {
    width: 500px;
  }
  .imsg-list {
    display: flex;
    align-items: center;
    .imgs-item {
      position: relative;
      margin-right: 10px;
      .el-icon-delete {
        position: absolute;
        top: 0;
        right: 0;
        font-size: 18px;
        color: red;
        z-index: 3;
        cursor: pointer;
      }
    }
  }
}
.div-content {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.d-title {
  text-align: center;
  font-size: 18px;
}
.d-desc {
  margin: 10px 0;
  // font-size: ;
  text-indent: 2em;
  line-height: 16px;
}
.d-tips {
  color: #999;
  margin-top: 10px;
}
</style>

<style lang="scss" scoped>

.resi-row-btn {
  display: flex;
  margin-bottom: 13px;
  ::v-deep .el-button {
    // margin-left: 10px;
    border: 0;
  }
  ::v-deep .el-select {
    margin-right: 10px;
  }
  .el-button--success {
    background: rgba(34, 193, 195, 1);
  }
  .el-button {
    margin-left: 10px;
    border: 0;
  }
  .el-button--success {
    background: rgba(34, 193, 195, 1);
  }
  .el-button--warning {
    background: rgba(254, 179, 73, 1);
  }
  .el-button--danger {
    background: rgba(254, 98, 82, 1);
  }
}
.avatar-uploader {
  ::v-deep
  .el-upload {
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .avatar-uploader-icon {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
}



.resi-btns {
  margin-top: 20px;
  text-align: center;
}
.input-width1 {
  width: 250px;
}
</style>
