<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
    append-to-body
    @close="handleClose"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="客户方子订单编号" prop="orderFormId">
        <el-input
          v-model="formData.orderFormId" maxlength="20" placeholder="客户方子订单编号" disabled
        />
      </el-form-item>
      <el-form-item label="号码类型" prop="numberType">
        <el-radio-group
          v-model="formData.numberType"
          @input="(e) => e === 1 ? (formData.transactionId = '') : e === 2 ? (formData.outTradeNO = '') : ''"
        >
          <el-radio :label="1">客户方支付主订单编号</el-radio>
          <el-radio :label="2">三方流水号</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.numberType === 1" label="客户方支付主订单编号" prop="outTradeNO">
        <el-input v-model="formData.outTradeNO" placeholder="请输入客户方支付主订单编号" />
      </el-form-item>
      <el-form-item v-else-if="formData.numberType === 2" label="三方流水号" prop="transactionId">
        <el-input v-model="formData.transactionId" placeholder="请输入三方流水号" />
      </el-form-item>
      <el-form-item label="支付方式" prop="paymentMode">
        <el-select
          v-model="formData.paymentMode" size="mini"
          style="width: 240px;" placeholder="请选择支付方式（三方流水号共存）"
        >
          <el-option label="惠市宝" :value="9" />
          <el-option label="通联" :value="4" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { syncLocalOrderStatusPaid } from '@/api/order'

export default {
  name: 'PaymentStatusUpdate',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '620px',
        title: ''
      },
      visible: false,
      formData: {
        orderFormId: '',
        numberType: 2, // 后端无需
        outTradeNO: '',
        transactionId: '',
        paymentMode: ''
      },
      formRules: {
        orderFormId: [
          { required: true, message: '缺少客户方子订单编号' }
        ],
        numberType: [
          { required: true, message: '缺少号码类型' }
        ],
        outTradeNO: [],
        transactionId: [
          { required: true, message: '请输入三方流水号' }
        ]
      }
    }
  },
  watch: {
    'formData.numberType': {
      deep: true,
      handler(val) {
        if (val === 1) {
          this.formRules.outTradeNO = [
            { required: true, message: '请输入客户方支付主订单编号' }
          ]
          this.formRules.transactionId = []
        } else if (val === 2) {
          this.formRules.outTradeNO = []
          this.formRules.transactionId = [
            { required: true, message: '请输入三方流水号' }
          ]
        }
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = '同步支付状态'
      this.$refs.formData && this.$refs.formData.resetFields()
      this.formData.orderFormId = params.orderFormid
      // this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            await syncLocalOrderStatusPaid(params)
            loading.close()
            this.$message({ message: `操作成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

