<template>
  <div class="account-details">
    <!-- 基础信息 -->
    <base-info
      client-type="B"
      :page-type="$route.query.type"
      :disabled="true"
      :details-info="detailsInfo"
    />
    <!-- 付款方账户信息 | 静默户账户信息 -->
    <payment-account ref="paymentRef" :details-info="paymentList" client-type="B" />

    <div class="submit-btn">
      <el-button
        @click="() => {
          $router.go(-1)
        }"
      >取消</el-button>
      <el-button type="primary" @click="submitClick('submit')">保存</el-button>
    </div>
  </div>
</template>

<script>
import {
  updateMemberRegister,
  getMemberRegister
} from '@/api/cyAccountManage/memberAccountManage/index'

export default {
  name: 'CyAccountaccountDetails',
  components: {
    BaseInfo: () => import('./components/BaseInfo.vue'),
    PaymentAccount: () => import('./components/PaymentAccount.vue')
  },

  data() {
    return {
      detailsInfo: {
        customerNumber: '',
        customerName: '',
        customerType: '',
        accountName: '',
        accountAlias: ''
      },
      paymentList: [],
      operateId: '',
      bocomBasicInfoId: ''
    }
  },
  watch: {
    $route: {
      handler(val) {
        if (val.query.id) {
          getMemberRegister({ id: val.query.id }).then(({ data }) => {
            if (data) {
              Object.keys(this.detailsInfo).forEach(key => {
                this.$set(this.detailsInfo, key, data[key])
              })
              this.paymentList = data.silentAccountInfoList
              this.operateId = data.id
              this.bocomBasicInfoId = data.bocomBasicInfoId
            }
          })
        }
      },
      immediate: true
    }
  },
  methods: {
    async submitClick() {
      const submitArr = this.$refs.paymentRef.tableData.filter(el => !el.id)
      if (submitArr.some(item => item.payerAccountName === '')) {
        this.$message.warning('静默户信息付款方账户名称不能为空')
        return
      }
      const reqParams = {
        silentAccountInfoList: submitArr,
        deleteSilentIds: this.$refs.paymentRef.deleteSilentIds
      }

      if (this.operateId) {
        reqParams.id = this.operateId
      }
      if (this.bocomBasicInfoId) {
        reqParams.bocomBasicInfoId = this.bocomBasicInfoId
      }
      const res = await updateMemberRegister(reqParams)

      console.log('updateMemberRegister----', res)

      if (res.data) {
        Object.keys(this.detailsInfo).forEach((el) => {
          this.$set(this.detailsInfo, el, res.data[el])
        })

        this.paymentList = res.data.silentAccountInfoList

        this.$message.success('静默户修改成功')
        this.$router.go(-1)
      }
    }
  }
}
</script>

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