<template>
  <div class="account-details">
    <!-- 基础信息 -->
    <base-info
      ref="baseInfoRef"
      :details-info="baseInfo"
      client-type="C"
      :disabled="true"
      :page-type="$route.query.type"
    />

    <!-- 付款方账户信息 | 静默户账户信息 -->
    <payment-account
      ref="paymentRef"
      :disabled="$route.query.type==='see' ? true : false"
      :details-info="silentAccountInfoList"
      client-type="C"
    />

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

<script>
import {
  updateMemberRegister,
  queryMemberDetails
} from '@/api/accountCenter/cyAccountManage'

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

  data() {
    return {
      baseInfo: {
        customerNumber: '',
        customerName: '',
        customerType: '',
        accountName: '',
        accountAlias: '',
        socialCreditCode: '',
        accountStatus: '',
        authorizationStatus: '',
        channel: '',
        customerSource: ''
      },
      operateId: '',
      accountNumber: '', // 登记薄编号
      silentAccountInfoList: []
    }
  },
  watch: {
    $route: {
      handler(val) {
        if (val.query.id) {
          queryMemberDetails({ id: val.query.id }).then(({ data }) => {
            if (data) {
              Object.keys(this.baseInfo).forEach(key => {
                this.$set(this.baseInfo, key, data[key])
              })
              this.operateId = data.id
              this.accountNumber = data.accountNumber
              this.silentAccountInfoList = data.silentAccountInfoList
            }
          })
        }
      },
      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 res = await updateMemberRegister({
        ...this.$refs.baseInfoRef.ruleForm,
        silentAccountInfoList: submitArr,
        deleteSilentIds: this.$refs.paymentRef.deleteSilentIds,
        id: this.operateId,
        accountNumber: this.accountNumber,
        bocomBasicInfoId: this.$route.query.editId,
        Loading: true
      })

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

      if (res.success) {
        this.$message.success('账户信息修改成功')
        this.$router.go(-1)
      }
    }
  }
}
</script>

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