<template>
  <a-form v-bind="formLayout" :form="form">
    <a-form-item label="平台支持币种">
      <a-select
        placeholder="请选择平台支持币种"
        mode="multiple"
        v-model="platform_currency_list"
        v-decorator="[
          'platform_currency_list',
          {
            initialValue: platform_currency_list,
            rules: [{ required: true, message: '请选择平台支持币种' }]
          }
        ]"
        :options="property_coin_type_cas.map((v) => ({ label: v.name, value: v.code }))"
      />
    </a-form-item>
    <a-form-item label="接口支持币种">
      <a-select
        placeholder="请选择接口支持币种"
        mode="multiple"
        v-model="api_currency_list"
        v-decorator="[
          'api_currency_list',
          {
            initialValue: api_currency_list,
            rules: [{ required: true, message: '请选择接口支持币种' }]
          }
        ]"
        :options="property_coin_type_cas.map((v) => ({ label: v.name, value: v.code }))"
      />
    </a-form-item>
    <a-form-item label="默认接口币种">
      <a-select
        placeholder="请选择默认接口币种"
        v-model="default_property_coin_type"
        v-decorator="[
          'default_property_coin_type',
          {
            initialValue: default_property_coin_type,
            rules: [{ required: true, message: '请选择默认接口币种' }]
          }
        ]"
        :options="default_property_coin_type_list"
      />
    </a-form-item>
  </a-form>
</template>
<script>
import mix from './mixins'
import common from '../mixins/common'

import store from '../store'
export default {
  mixins: [mix, common],
  data () {
    return {
      api_currency_list: [],
      platform_currency_list: [],
      default_property_coin_type: this.modalData.default_property_coin_type || undefined,
      formLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      }
    }
  },
  computed: {
    successMessage() {
      return '操作成功'
    },
		property_type_cas() {
			return store.state.property_type_cas || []
		},
    property_coin_type_cas() {
      return this.property_type_cas.map(v => v.children).flat(Infinity)
    },
		property_coin_type_cas_obj() {
      let obj = {}
      this.property_coin_type_cas.forEach(v => {
				obj[v.code] = v.name
      })
			return obj
		},
    default_property_coin_type_list() {
      return this.api_currency_list.map(v => ({ label: this.property_coin_type_cas_obj[v] || v, value: v }))
    },
    property_coin_type_cas_resolve() {
      let obj = {}
      for (let i = 0; i < this.property_type_cas.length; i++) {
        const v = this.property_type_cas[i]
        for (let j = 0; j < v.children.length; j++) {
          const vv = v.children[j]
          obj[vv.code] = v.code
        }
      }
			return obj
    }
  },
  watch: {
    api_currency_list(newVal) {
      newVal = newVal || []
      if (this.default_property_coin_type && !newVal.includes(this.default_property_coin_type)) {
        this.form.setFieldsValue({ default_property_coin_type: undefined })
        this.default_property_coin_type = undefined
      }
    }
  },
  mounted () {
		if (!this.property_type_cas || !this.property_type_cas.length) store.dispatch('getPropertyType')
    this.getCurrency()
  },
  methods: {
		async getCurrency() {
			const res = await this.request_api.finance_recharge_method.get_currency({
				recharge_method_uuid: this.modalData.uuid
			})
			if (res.err) return res.showError()
      const api_currency_list = res.api_currency_list || []
      const platform_currency_list = res.platform_currency_list || []
      this.api_currency_list = api_currency_list.map(v => v.property_coin_type)
      this.platform_currency_list = platform_currency_list.map(v => v.property_coin_type)
		},
    async submit(values) {
      const default_property_type = this.property_coin_type_cas_resolve[values.default_property_coin_type]
      const api_currency_list = values.api_currency_list.map(v => ({ property_coin_type: v, property_type: this.property_coin_type_cas_resolve[v] }))
      const platform_currency_list = values.platform_currency_list.map(v => ({ property_coin_type: v, property_type: this.property_coin_type_cas_resolve[v] }))
      const res = await this.request_api.finance_recharge_method.currency({
        uuid: this.modalData.uuid,
        version: this.modalData.version,
        api_currency_list,
        platform_currency_list,
        default_property_coin_type: values.default_property_coin_type,
        default_property_type: default_property_type
      })
      return res
    }
  }
}
</script>
