<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="50%"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item :label="__('预付款支付')" prop="pm_money">
            <el-input
              v-model="form.pm_money"
              clearable
              :placeholder="__('请输入预付款支付')"
              :style="{ width: '90%' }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="__('充值卡支付')" prop="pm_recharge_card">
            <el-input
              v-model="form.pm_recharge_card"
              clearable
              :placeholder="__('请输入充值卡支付')"
              :style="{ width: '90%' }"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="__('付款金额')" prop="deposit_total_fee">
            <el-input
              v-model="form.deposit_total_fee"
              clearable
              :placeholder="__('请输入付款金额')"
              :style="{ width: '90%' }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="__('付款日期')" prop="deposit_notify_time">
            <el-date-picker
              v-model="form.deposit_notify_time"
              clearable
              :placeholder="__('请输入付款日期')"
              :style="{ width: '90%' }"
              type="datetime"
              value-format=" yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="__('支付凭证号')" prop="deposit_trade_no">
            <el-input
              v-model="form.deposit_trade_no"
              clearable
              :placeholder="__('请输入支付凭证号')"
              :style="{ width: '90%' }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="__('收款账户')" prop="payment_channel_id">
            <el-select
              v-model="form.payment_channel_id"
              clearable
              :placeholder="__('请选择收款账户')"
              :style="{ width: '90%' }"
            >
              <el-option
                v-for="(item, index) in payment_channel_idOptions"
                :key="index"
                :disabled="item.disabled"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item :label="__('备注')" prop="trade_remark">
          <el-input
            v-model="form.trade_remark"
            clearable
            :placeholder="__('请输入备注')"
            :style="{ width: '90%' }"
          />
        </el-form-item>
      </el-row>
    </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 { offline } from '@/api/consume/trade'

  export default {
    name: 'PaymentEdit',
    data() {
      return {
        form: {
          deposit_notify_time: '',
          deposit_total_fee: undefined,
        },
        rules: {
          deposit_trade_no: [
            {
              required: true,
              message: '请输入支付凭证号',
              trigger: 'blur',
            },
          ],
        },
        payment_channel_idOptions: [
          {
            label: this.__('支付宝'),
            value: 1410,
          },
          {
            label: this.__('微信'),
            value: 1411,
          },
        ],
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form.order_id = row.order_id
          this.form.deposit_total_fee = row.deposit_total_fee
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          deposit_notify_time: '',
          deposit_total_fee: undefined,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await offline(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
