<template>
  <ai-list class="AppPartyPayment">
    <template slot="title">
      <ai-title title="党费缴纳" isShowBottomBorder :instance="instance"></ai-title>
    </template>
    <template slot="content">
      <div class="statistics-top">
        <div class="statistics-top__item">
          <span>本月已缴纳党费金额</span>
          <h2 style="color: #2266FF;">{{ topTotal['本月缴纳党费'] || 0 }}</h2>
        </div>
        <div class="statistics-top__item">
          <span>本月已缴纳人数</span>
          <h2 style="color: #22AA99;">{{ topTotal['本月已缴纳人数'] || 0  }}</h2>
        </div>
        <div class="statistics-top__item">
          <span>本月未缴纳党费金额</span>
          <h2 style="color: #F8B425">{{ topTotal['本月未缴纳党费'] || 0 }}</h2>
        </div>
        <div class="statistics-top__item">
          <span>本月未缴纳人数</span>
          <h2 style="color: #999;">{{ topTotal['本月未缴纳人数'] || 0 }}</h2>
        </div>
      </div>
      <div class="content">
        <ai-search-bar class="search-bar">
          <template #left>
            <el-button size="small" type="primary" icon="iconfont iconAdd" @click="toAdd('')">新增</el-button>
            <ai-party :instance="instance" v-model="search.partyOrgId" :topOrgId="topOrgId" :name.sync="search.partyOrgName"
                    style="display:inline-block" @origin="handlePartyOrgSelect" customClicker
                    url="/app/partyOrganization/queryAllChildren">
              <el-input size="small" v-model="search.partyOrgName" readonly placeholder="选择党组织"></el-input>
            </ai-party>
            <el-date-picker v-model="search.ymd" type="month" placeholder="选择月" size="small" value-format="yyyy-MM" @change="getListInit"></el-date-picker>
            <ai-select
              v-model="search.status"
              @change="getListInit()"
              placeholder="状态"
              :selectList="dict.getDict('zhishengPartyFeeRecordStatus')">
            </ai-select>
          </template>
          <template slot="right">
            <ai-import :instance="instance" :dict="dict" type="appconvenientaddressbook" name="党费缴纳"
              @success="getListInit()" importUrl="/app/appdfjnzhisheng/import" url="/app/appdfjnzhisheng/downloadTemplate">
              <el-button icon="iconfont iconImport">导入</el-button>
            </ai-import>
            <ai-download :instance="instance" url="/app/appdfjnzhisheng/export" :params="search" fileName="党费缴纳"
              :disabled="tableData.length == 0">
              <el-button icon="iconfont iconExported" :disabled="tableData.length == 0">导出</el-button>
            </ai-download>
            <el-input size="small" v-model="search.name" placeholder="姓名/身份证"
              suffix-icon="iconfont iconSearch" v-throttle="() => {getListInit()}" clearable @clear="search.name = '', getListInit()"/>
          </template>
        </ai-search-bar>
        <ai-table
          :tableData="tableData"
          :col-configs="colConfigs"
          :total="total"
          :current.sync="search.current"
          :size.sync="search.size"
          @getList="getList">
          <el-table-column slot="option" label="操作" align="center" width="160px" fixed="right">
            <template slot-scope="{ row }">
              <div class="table-options">
                <el-button type="text" title="编辑" @click="edit(row)">编辑</el-button>
                <el-button type="text" title="删除" @click="remove(row)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </ai-table>
      </div>
      <ai-dialog :visible.sync="dialog" :title="dialogTitle" @closed="$refs.form.resetFields()" @onConfirm="handlePayment">
        <el-form ref="form" :rules="rules" size="small" :model="form" label-width="80px">
          <el-form-item label="党员姓名" prop="name">
            <el-autocomplete ref="poiInput" v-model="form.name" size="small" clearable :fetch-suggestions="handleSearch"
              placeholder="请输入党员姓名" @select="handleSelect" :trigger-on-focus="false" style="width:100%;">
              <template slot-scope="{item}">
                <span style="direction: rtl" v-text="`${item.name}-${item.partyOrgName}(${item.idNumber})`"/>
              </template>
            </el-autocomplete> 
          </el-form-item>
          <el-form-item label="身份证号" prop="idNumber">
            <el-input placeholder="自动带入" v-model="form.idNumber" :disabled="true" size="small"></el-input>
          </el-form-item>
          <el-form-item label="缴纳状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio label="1">已缴纳</el-radio>
              <el-radio label="0">未缴纳</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="缴纳月份" prop="ymd">
            <el-date-picker v-model="form.ymd" type="month" placeholder="选择年月" size="small" value-format="yyyy-MM"></el-date-picker>
          </el-form-item>
          <el-form-item label="金额" prop="amount">
            <el-input-number :precision="2" size="small" type="input" v-model="form.amount" clearable placeholder="2位小数"></el-input-number>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-list>
</template>

<script>
  import {mapState} from 'vuex'
  export default {
    name: 'AppPartyPayment',
    label: '党费缴纳',
    props: {
      instance: Function,
      dict: Object
    },
    data () {
      return {
        search: {
          current: 1,
          size: 10,
          partyOrgId: '',
          partyOrgName: '',
          name: '',
          status: '',
          ymd: ''
        },
        total: 0,
        tableData: [],
        colConfigs: [
          { prop: 'orgName', label: '党组织' },
          { prop: 'name', label: '党员姓名', align: 'center',  },
          { prop: 'idNumber', label: '身份证', align: 'center' },
          { prop: 'ymd', label: '缴纳月份', align: 'center' },
          { prop: 'amount', label: '缴纳党费', align: 'center' },
          { prop: 'status', label: '状态', align: 'center',format: v => this.dict.getLabel('zhishengPartyFeeRecordStatus', v)},
          { slot: 'option'}
        ],
        topOrgId: '',
        dialog: false,
        dialogTitle: '',
        form: {
          name: '',
          idNumber: '',
          status: '0',
          ymd: '',
          amount: '',
          orgId: '',
          orgName: '',
          partyId: ''
        },
        topTotal: {}
      }
    },

    computed: {
      params () {
        return {
          ...this.search,
          startTime: this.search.type === '3' ? this.date[0] : '',
          endTime: this.search.type === '3' ? this.date[1] : ''
        }
      },
      rules() {
        return {
          name: [{ required: true, message: '请输入党员姓名', trigger: 'change'}],
          idNumber: [{ required: true, message: '请选择党员', trigger: 'change'}],
          status: [{ required: true, message: '请选择缴费状态', trigger: 'change'}],
          ymd: [{ required: true, message: '请选择缴纳月份', trigger: 'change'}],
          amount: [{ required: true, message: '请输入金额', trigger: 'blur'}],
        }
      },
      ...mapState(['user']),
    },

    created () {
      this.topOrgId = this.user.info.organizationId
      this.dict.load('zhishengPartyFeeRecordStatus').then(() => {
        this.getList()
      })
      this.getTotal()
    },

    methods: {
      toAdd() {
        this.form =  {
          name: '',
          idNumber: '',
          status: '0',
          ymd: '',
          amount: '',
          orgId: '',
          orgName: '',
          partyId: ''
        }
        this.dialogTitle = '新增党费信息'
        this.dialog = true
      },
      edit(row) {
        this.form = row
        this.dialog = true
        this.dialogTitle = '编辑党费信息'
      },
      handleSearch(e, cb) {
        this.instance.post(`/app/appparty/fuzzyList?name=${e}&size=50`).then((res) => {
          if (res.code == 0) {
            cb(res.data)
          }
        });
      },
      handleSelect(e) {
        this.form.name = e.name
        this.form.idNumber = e.idNumber
        this.form.orgId = e.partyOrgId
        this.form.orgName = e.partyOrgName
        this.form.partyId = e.id
      },
      handlePayment() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appdfjnzhisheng/addOrUpdate`, {...this.form}).then((res) => {
              if (res.code == 0) {
                this.$message.success(this.dialogTitle == '编辑党费信息' ? "编辑成功" : "新增成功");
                this.$refs.form.resetFields()
                this.dialog = false
                this.getListInit()
                this.getTotal()
              }
            });
          }
        });
      },
      handlePartyOrgSelect(e) {
        let {isLeaf, name, id} = e?.[0] || {};
        if (isLeaf == 1) {
          this.search.partyOrgName = name;
          this.search.partyOrgId = id;
        }
        this.getListInit()
      },
      getListInit() {
        this.search.current = 1
        this.getList()
      },
      getList () {
        this.instance.post(`/app/appdfjnzhisheng/list`, null, {
          params: {
            ...this.search,
            orgId: this.search.partyId
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },
      remove(row) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/appdfjnzhisheng/delete?ids=${row.id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },
      getTotal() {
        this.instance.post(`/app/appdfjnzhisheng/statistics`).then(res => {
          if (res.code == 0) {
            this.topTotal = res.data
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .AppPartyPayment {

    .bottom {
      display: flex;
      align-items: center;

      & > .ai-card {
        flex: 1;

        &:last-child {
          margin-left: 20px;
        }
      }
    }

    :deep( .ai-list__content ){
      padding: 0!important;

      .ai-list__content--right-wrapper {
        background: transparent!important;
        box-shadow: none!important;
        margin: 0!important;
        padding: 12px 16px 12px!important;
      }
    }

    :deep( .ai-card) {
      .ai-card__body {
        padding: 12px 16px;
      }
    }

    .statistics-top {
      display: flex;
      align-items: center;
      margin-bottom: 20px;

      & > div {
        flex: 1;
        height: 96px;
        line-height: 1;
        margin-right: 20px;
        padding: 16px 24px;
        background: #FFFFFF;
        box-shadow: 0px 4px 6px -2px rgba(15, 15, 21, 0.15);
        border-radius: 4px;

        &:last-child {
          margin-right: 0;
        }

        h3 {
          font-size: 24px;
        }

        span {
          display: block;
          margin-bottom: 16px;
          color: #888888;
          font-size: 16px;
        }
      }
    }

    .content {
      padding: 16px;
      background: #FFFFFF;
      box-shadow: 0px 4px 6px -2px rgba(15, 15, 21, 0.15);
    }
  }
</style>
