<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form
      ref="form"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('渠道名称')" prop="user_channel_name">
        <el-input
          v-model="form.user_channel_name"
          clearable
          :placeholder="__('请输入渠道名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('渠道前缀')" prop="user_channel_prefix">
        <el-input
          v-model="form.user_channel_prefix"
          clearable
          :placeholder="__('请输入渠道前缀')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item label="" prop="user_channel_enable">
        <el-switch
          v-model="form.user_channel_enable"
          :active-text="__('启用')"
          :active-value="1"
          :inactive-text="__('禁用')"
          :inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doEditChannel } from '@/api/user/channel'

  export default {
    name: 'BaseBankEdit',
    data() {
      return {
        form: {
          user_channel_enable: 1,
        },
        rules: {
          user_channel_name: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入渠道名称'),
            },
          ],
          user_channel_prefix: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入渠道前缀'),
            },
          ],
          user_channel_enable: [
            {
              required: true,
              trigger: 'change',
              message: this.__('请选择是否启用'),
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          user_channel_enable: 1,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEditChannel(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
