<template>
  <div class="create-team-drawer">
    <el-drawer
      :visible="true"
      :before-close="handleClose"
      :close-on-press-escape="false"
      :wrapper-closable="false"
      :modal-append-to-body="true"
      :append-to-body="true"
      custom-class="drawer-auto-layout"
    >
      <div slot="title" class="flex-l">
        <sys-origin-icon :icon="sysOrigin" size="18px" />&nbsp;{{ $t('pages.teamCreate.dialog.title') }}
      </div>
      <div class="create-team ">
        <div class="drawer-form">
          <el-form ref="form" :model="form" :rules="formRules" label-width="80px">
            <el-form-item v-if="bindBd==='no'" prop="region" :label="$t('pages.teamCreate.form.region')">
              <select-system-region
                ref="regionSelectPolicy"
                v-model="form.region"
                :sys-origin="sysOrigin"
                clearable
                :placeholder="$t('pages.teamCreate.placeholder.region')"
              />
            </el-form-item>
            <el-form-item prop="ownUserId" :label="$t('pages.teamCreate.form.agent')">
              <account-input v-model="form.ownUserId" :sys-origin="sysOrigin" :placeholder="$t('pages.teamCreate.placeholder.agentId')" />
            </el-form-item>

            <el-form-item :label="$t('pages.teamCreate.form.bindBd')">
              <el-radio-group v-model="bindBd" :disabled="bdOpsTrue">
                <el-radio label="yes">{{ $t('pages.teamCreate.word.yes') }}</el-radio>
                <el-radio label="no">{{ $t('pages.teamCreate.word.no') }}</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item v-if="bindBd==='yes'" prop="bdUserId" :label="$t('pages.teamCreate.form.bd')">
              <account-input v-model="form.bdUserId" :sys-origin="sysOrigin" :default-select-type="bdAccountDefaultSelectType" :placeholder="$t('pages.teamCreate.placeholder.bdId')" />
            </el-form-item>

            <el-form-item prop="remarks" :label="$t('pages.teamCreate.form.remark')">
              <el-input
                v-model="form.remarks"
                type="textarea"
                :placeholder="$t('pages.teamCreate.placeholder.remark')"
                maxlength="100"
                show-word-limit
                resize="none"
                rows="5"
              />
            </el-form-item>

            <el-form-item label="">
              <el-checkbox v-model="formSettingDefault">{{ $t('pages.teamCreate.form.setting') }}</el-checkbox>
              <el-checkbox v-model="formContactAdd">{{ $t('pages.teamCreate.form.contactInfo') }}</el-checkbox>
            </el-form-item>

            <div v-if="formSettingDefault" style="padding: 10px 0px;">
              <el-divider content-position="left">{{ $t('pages.teamCreate.section.settingInfo') }}</el-divider>
              <el-form-item prop="setting.maxMember" :label="$t('pages.teamCreate.form.memberQuantity')">
                <el-input v-model="form.setting.maxMember" :placeholder="$t('pages.teamCreate.placeholder.maxMember')" maxlength="100" show-word-limit />
              </el-form-item>
            </div>

            <div v-if="formContactAdd" style="padding: 10px 0px;">
              <el-divider content-position="left">{{ $t('pages.teamCreate.section.contactInfo') }}</el-divider>
              <el-form-item prop="contact.type" :label="$t('pages.teamCreate.form.type')">
                <el-select v-model="form.contact.type" :placeholder="$t('pages.teamCreate.placeholder.selectType')" style="width: 100%;">
                  <el-option v-for="(item, index) in contactTypes" :key="index" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
              <el-form-item prop="contact.contact" :label="$t('pages.teamCreate.form.contact')">
                <el-input v-model="form.contact.contact" :placeholder="$t('pages.teamCreate.placeholder.contact')" maxlength="100" show-word-limit />
              </el-form-item>
              <el-form-item prop="contact.remarks" :label="$t('pages.teamCreate.form.remark')">
                <el-input
                  v-model="form.contact.remarks"
                  type="textarea"
                  :placeholder="$t('pages.teamCreate.placeholder.remark')"
                  maxlength="100"
                  show-word-limit
                  resize="none"
                  rows="5"
                />
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div v-if="sysOrigin" class="drawer-footer">
          <el-button :disabled="submitLoading" @click="handleClose()">{{ $t('pages.teamCreate.action.cancel') }}</el-button>
          <el-button type="primary" :disabled="submitLoading" :loading="submitLoading" @click="submitForm()">{{ $t('pages.teamCreate.action.save') }}</el-button>
        </div>
      </div>
    </el-drawer>

  </div>
</template>
<script>
import { contactTypes, billCyclePolicyTypes } from '@/constant/team-type'
import { createTeam } from '@/api/team'
import { deepClone } from '@/utils'

export default {
  name: 'TeamCreate',
  props: {
    sysOrigin: {
      type: String,
      require: true,
      default: ''
    },
    bdOps: {
      type: Object,
      require: false,
      default: () => {}
    }
  },
  data() {
    return {
      bdAccountDefaultSelectType: '',
      bindBd: 'no',
      billCyclePolicyTypes,
      contactTypes,
      submitLoading: false,
      formSettingDefault: false,
      formContactAdd: false,
      form: {
        sysOrigin: '',
        region: '',
        account: '',
        ownUserId: '',
        remarks: '',
        setting: {
          maxMember: '1000'
        },
        contact: {
          type: '',
          contact: '',
          remarks: ''
        },
        bdUserId: ''
      },
      bdOpsTrue: false
    }
  },
  computed: {
    formRules() {
      const commonRules = [
        { required: true, message: this.$t('pages.teamCreate.validation.requiredField'), trigger: 'blur' }
      ]
      return {
        sysOrigin: commonRules,
        region: commonRules,
        ownUserId: commonRules,
        bdUserId: commonRules,
        'contact.type': commonRules,
        'contact.contact': commonRules,
        'setting.maxMember': commonRules
      }
    }
  },
  watch: {
    bdOps: {
      handler(newVal) {
        if (newVal) {
          this.bindBd = 'yes'
          this.bdOpsTrue = true
          this.bdAccountDefaultSelectType = 'LONG'
          this.form.bdUserId = newVal.userId
          this.form.region = newVal.region
        }
      },
      immediate: true
    }
  },
  methods: {
    handleClose() {
      if (this.submitLoading) {
        this.$opsMessage.warn(this.$t('pages.teamCreate.message.submitting'))
        return
      }
      this.$emit('close')
    },
    submitForm() {
      const that = this
      that.$refs.form.validate(valid => {
        if (!valid) {
          console.error('error submit!!')
          return false
        }
        that.submitLoading = true

        const submitForm = deepClone(that.form)

        if (!that.formSettingDefault) {
          submitForm.setting = null
        }
        if (!that.formContactAdd) {
          submitForm.contact = null
        }
        submitForm.sysOrigin = that.sysOrigin

        if (that.bindBd === 'no') {
          submitForm.bdUserId = ''
        }

        createTeam(submitForm).then(res => {
          that.submitLoading = false
          that.$emit('success', Object.assign({}, submitForm))
          that.handleClose()
        }).catch(err => {
          that.submitLoading = false
          that.$emit('fial', err)
        })
      })
    }
  }
}
</script>

