<template>
  <section class="psfList">
    <ai-list>
      <ai-title slot="title" title="党员积分明细" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="dialog=true">添加</el-button>
            <el-date-picker v-model="search.createTime" type="daterange" placeholder="日期" size="small" clearable
                            @change="handleSearchTime" start-placeholder="开始时间" end-placeholder="结束时间"/>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索党员" v-model="search.partyName" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="options" label="操作" fixed="right" align="center">
            <template slot-scope="{row}">
              <el-button type="text" @click="showDetail(row)">详情</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
    <ai-dialog :visible.sync="dialog" title="积分对象" width="600px" @close="form={}" @onConfirm="submit"
               :customFooter="!isAdd">
      <el-form v-if="isAdd" :model="form" size="small" ref="DialogForm" :rules="rules" label-width="80px">
        <el-form-item label="选择人员" prop="partyId">
          <ai-select v-model="form.partyId" action="/app/appparty/list" :instance="instance"
                     :prop="{label:'name'}"/>
        </el-form-item>
        <el-form-item label="调整说明" prop="remark">
          <el-input type="textarea" placeholder="请输入" v-model="form.remark" maxlength="100" show-word-limit rows="3"
                    clearable/>
        </el-form-item>
        <el-form-item label="类型" prop="integralType">
          <ai-select v-model="form.integralType" :selectList="dict.getDict('partyIntegralType')"/>
        </el-form-item>
        <el-form-item label="积分" prop="integral">
          <el-input v-model.number="form.integral" placeholder="请输入正整数" clearable/>
        </el-form-item>
      </el-form>
      <ai-wrapper v-else>
        <ai-info-item label="对象" :value="form.partyName"/>
        <ai-info-item label="调整说明" :value="form.remark" isLine/>
        <ai-info-item label="类型" :value="dict.getLabel('partyIntegralType',form.integralType)"/>
        <ai-info-item label="积分" :value="form.integral"/>
      </ai-wrapper>
    </ai-dialog>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "psfList",
  props: {
    instance: Function,
    dict: Object,
  },
  computed: {
    ...mapState(['user']),
    colConfigs() {
      return [
        {label: "对象", prop: "partyName"},
        {label: "调整说明", prop: "remark", align: "center"},
        {label: "事件", prop: "createTime"},
        {label: "类型", prop: "integralType", align: "center", dict: "partyIntegralType"},
        {label: "积分", prop: "integral", align: "center"},
        {slot: "options"}
      ]
    },
    isAdd() {
      return !this.form.id
    }
  },
  data() {
    return {
      search: {},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      dialog: false,
      form: {},
      rules: {
        partyId: {required: true, message: "请选择人员"},
        remark: {required: true, message: "请输入调整说明"},
        integralType: {required: true, message: "请选择类型"},
        integral: [
          {required: true, message: "请输入分数"},
          {pattern: /^\d+$/g, message: "请输入正整数"}
        ],
      }
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/app/apppartyintegralinfo/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    showDetail(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialog = true
    },
    submit() {
      this.$refs.DialogForm.validate(v => {
        if (v) {
          let loading = this.$loading({text: "提交中..."})
          this.instance.post("/app/apppartyintegralinfo/addOrUpdate", this.form).then(res => {
            loading.close()
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.dialog = false
              this.getTableData()
            }
          }).catch(() => loading.close())
        }
      })
    },
    handleSearchTime(v) {
      this.page.current = 1
      this.search.startTime = v?.[0]
      this.search.endTime = v?.[1]
      this.getTableData()
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

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