<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="500px" 
    append-to-body 
    :close-on-click-modal="false"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <form-col :span="span"  label="所属用户" prop="userId" v-if="isSysAdmin()">
          <user-input v-model="form.userId" :text.sync="form.userName"/>
        </form-col>
        <form-col :span="span"  label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" style="width: 100%;">
            <el-option
              v-for="dict in dict.type.account_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </form-col>
        <form-col :span="span"  label="微信OpenId" prop="no" label-width="7em" v-if="AccountType.WX == form.type">
          <el-input v-model="form.no" placeholder="请输入微信OpenId" />
        </form-col>
        <form-col :span="span"  label="支付宝OpenId" prop="no" label-width="8em" v-if="AccountType.ALI == form.type">
          <el-input v-model="form.no" placeholder="请输入支付宝OpenId" />
        </form-col>
        <form-col :span="span"  label="银行卡号" prop="no" label-width="6em" v-if="AccountType.BANK == form.type">
          <el-input v-model="form.no" placeholder="请输入银行卡号" />
        </form-col>
        <form-col :span="span"  label="收款二维码" prop="no" label-width="7em" v-if="AccountType.QR == form.type">
          <image-upload v-model="form.no" :limit="1"/>
        </form-col>
        <form-col :span="span"  label="收款姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入收款人姓名" />
        </form-col>
        <form-col :span="span"  label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </form-col>
        <form-col :span="span"  label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getAccount, addAccount, updateAccount } from "@/api/bst/account";
import FormCol from "@/components/FormCol/index.vue";
import UserInput from '@/components/Business/User/UserInput.vue';
import {AccountType} from '@/utils/enums'

export default {
  name: 'AccountEditDialog',
  components: { FormCol, UserInput },
  dicts: ['account_type'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: [String, Number],
      default: null
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      AccountType,
      span: 24,
      title: '',
      form: {},
      rules: {
        userId: [
          { required: true, message: "所属用户不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        no: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "收款人姓名不能为空", trigger: "blur" }
        ],
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ]
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    }
  },
  methods: {
    handleOpen() {
      if (this.id == null) {
        this.reset();
        this.title = "添加收款账户";
      } else {
        this.getDetail();
        this.title = "修改收款账户";
      }
    },
    reset() {
      this.form = {
        id: null,
        userId: null,
        type: null,
        no: null,
        name: null,
        idCard: null,
        mobile: null,
        bankIcon: null,
        bankName: null,
        bankCardName: null,
        bankType: null,
        ...this.initData
      };
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate();
      });
    },
    getDetail() {
      getAccount(this.id).then(response => {
        this.form = response.data;
      });
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const promise = this.form.id != null ? updateAccount(this.form) : addAccount(this.form);
          promise.then(response => {
            this.$modal.msgSuccess(this.form.id != null ? "修改成功" : "新增成功");
            this.dialogVisible = false;
            this.$emit('success');
          });
        }
      });
    },
    cancel() {
      this.dialogVisible = false;
      this.reset();
    }
  }
}
</script>

<style lang="scss" scoped>

</style> 