<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @close="close"
  >
    <el-form ref="form" label-width="160px" :model="form" :rules="rules">
      <el-form-item :label="__('代码名称')" prop="payment_channel_code">
        <el-input
          v-model="form.payment_channel_code"
          :disabled="form.payment_channel_buildin === 1"
          :placeholder="__('代码名称')"
        />
      </el-form-item>
      <el-form-item :label="__('账户名称')" prop="payment_channel_name">
        <el-input
          v-model="form.payment_channel_name"
          :placeholder="__('账户名称')"
        />
      </el-form-item>

      <el-form-item
        :label="__('支付接口配置信息')"
        prop="payment_channel_config"
      >
        <el-input
          v-model="form.payment_channel_config"
          :placeholder="__('支付接口配置信息')"
        />
      </el-form-item>

      <el-form-item
        :label="__('微信中是否可以使用')"
        prop="payment_channel_wechat"
      >
        <el-select
          v-model="form.payment_channel_wechat"
          :placeholder="__('微信中是否可以使用')"
        >
          <el-option
            v-for="item in options"
            :key="item.payment_channel_wechat"
            :label="item.label"
            :value="item.payment_channel_wechat"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('是否启用')" prop="payment_channel_enable">
        <el-radio v-model="form.payment_channel_enable" :label="1">
          启动
        </el-radio>
        <el-radio v-model="form.payment_channel_enable" :label="0">
          禁用
        </el-radio>
      </el-form-item>
      <el-form-item :label="__('支付方式')" prop="payment_type_id">
        <el-select v-model="form.payment_type_id" :placeholder="__('支付方式')">
          <el-option
            v-for="item in optionsId"
            :key="item.payment_type_id"
            :label="item.label"
            :value="item.payment_type_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('排序')" prop="payment_channel_order">
        <el-input
          v-model="form.payment_channel_order"
          :placeholder="__('排序')"
        />
      </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 { doEdit } from '@/api/payment/channel'

  export default {
    name: 'PaymentChannelEdit',
    data() {
      return {
        options: [],
        optionsId: [],
        form: {
          payment_channel_wechat: 0,
          payment_type_id: 1305,
          payment_channel_enable: 0,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      ;(this.options = [
        {
          payment_channel_wechat: 1,
          label: this.__('启动'),
        },
        {
          payment_channel_wechat: 0,
          label: this.__('禁用'),
        },
      ]),
        (this.optionsId = [
          {
            payment_type_id: 1302,
            label: this.__('在线支付【站内支付余额充值卡白条】'),
          },
          {
            payment_type_id: 1305,
            label: this.__('线下支付'),
          },
        ]),
        (this.rules = {
          payment_channel_id: [
            { required: true, trigger: 'blur', message: this.__('编号') },
          ],
          payment_channel_code: [
            { required: true, trigger: 'blur', message: this.__('代码名称') },
          ],
          payment_channel_name: [
            { required: true, trigger: 'blur', message: this.__('账户名称') },
          ],
          payment_channel_img: [
            { required: true, trigger: 'blur', message: this.__('图标') },
          ],
          payment_channel_config: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('支付接口配置信息'),
            },
          ],
          payment_channel_status: [
            { required: true, trigger: 'blur', message: this.__('接口状态') },
          ],
          payment_channel_allow: [
            { required: true, trigger: 'blur', message: this.__('类型') },
          ],
          payment_channel_wechat: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('微信中是否可以使用'),
            },
          ],
          payment_channel_enable: [
            { required: true, trigger: 'blur', message: this.__('是否启用') },
          ],
          payment_type_id: [
            { required: true, trigger: 'blur', message: this.__('支付方式') },
          ],
          payment_channel_order: [
            { required: true, trigger: 'blur', message: this.__('排序') },
          ],
          payment_channel_buildin: [
            { required: true, trigger: 'blur', message: this.__('系统内置') },
          ],
          store_id: [
            { required: true, trigger: 'blur', message: this.__('所属店铺') },
          ],
          chain_id: [
            { required: true, trigger: 'blur', message: this.__('所属门店') },
          ],
          subsite_id: [
            { required: true, trigger: 'blur', message: this.__('所属分站') },
          ],
        })

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
