<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog :visible.sync="showDialog" :title="title" :modal-append-to-body="false" style="text-align:left;" @close="closeDialog" :close-on-click-modal="false">
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="类型" prop="type">
          <el-select v-model="formModel.type" filterable style="width:300px">
            <el-option label="收款" value="1"></el-option>
            <el-option label="冲账" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户编号" prop="customerId">
          <el-select v-model="formModel.customerId" filterable placeholder="请选择客户" style="width:300px">
            <el-option v-for="customer in customerList" :key="customer.id" :label="customer.company" :value="customer.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="账单金额" prop="totalAmount">
          <el-input-number v-model="formModel.totalAmount" :min="0" :precision="2" style="width:300px"></el-input-number>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import accountApi from "@/api/base/account";
import customerApi from "@/api/base/customer";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        customerId: [{ required: true, message: '客户编号不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
        totalAmount: [{ required: true, message: '账单金额不能为空', trigger: 'blur' }]
      },
      showDialog: true,
      loading: false,
      submitting: false,
      customerList: [],
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate(valid => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return accountApi.add(self.formModel);
            }
            else {
              return accountApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success"
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning"
              });

              self.$emit("close", false);
            }
          });
        }
      });
    }
  },
  mounted: function () {
    var self = this;

    customerApi.findList().then(response => {
      this.customerList = response.data.data;
    });

    (function () {
      if (self.businessKey.length == 0) {
        return accountApi.create()
      }
      else {
        return accountApi.edit(self.businessKey)
      }
    })().then(response => {
      var jsonData = response.data;
      self.loading = false;

      if (jsonData.result) {
        self.formModel = jsonData.data;
      } else {
        self.$message.error(jsonData.message + "");
      }
    }).catch(error => {
      self.$message.error(error + "");
    });
  }
};
</script>