<template>
  <div>
    <div class="customer-head">
      <div class="customer-back">
        <el-button type="danger" style="border: none;background: none;color: red" icon="el-icon-arrow-left"
          @click="backToList(0)">返回列表
        </el-button>
      </div>
      <div class="add-customer-btn">
        <!--        <el-button class="el-button&#45;&#45;small-yh" style="margin-right: 10px" type="primary" size="small" v-if="!editButton"-->
        <!--                   @click="confirmEditing">编辑-->
        <!--        </el-button>-->
        <el-button class="el-button--small-yh" type="primary" size="small" @click="editCustomer">保存数据
        </el-button>
        <el-button class="el-button--small-yh" type="primary" size="small" @click="enableNot" v-if="form.id">
          {{ form.enableOrNot == 0 ? '启用' : '禁用' }}
        </el-button>
      </div>
    </div>
    <div style="margin-top: 50px">
      <trade-card title="基础信息">
        <avue-form class="trading-form" :option="optionForm" v-model="form" ref="form"></avue-form>
      </trade-card>
    </div>
  </div>
</template>

<script>

import { typeSave, detail, editenable } from "@/api/tirePartsMall/basicData/accountManagement";

export default {
  name: "detailsPage",
  data() {
    return {
      form: {},
      optionForm: {
        menuBtn: false,
        span: 8,
        column: [{
          label: '账户名称',
          prop: "cname",
          rules: [{
            required: true,
            message: " ",
            trigger: "blur"
          }]
        }, {
          label: '账户类型',
          prop: "accountType",
          type: 'select',
          dicUrl: "/api/blade-system/dict-biz/dictionary?code=accountType",
          props: {
            label: "dictValue",
            value: "dictValue"
          }
        }, {
          label: '开户银行',
          prop: "bankDeposit"
        }, {
          label: '银行账号',
          prop: "bankAccount"
        }, {
          label: '开户人',
          prop: "accountHolder"
        }, {
          label: '公司名称',
          prop: "corporateName",
          type: 'select',
          dicUrl: "/api/blade-system/dept/lazy-list?parentId=",
          props: {
            label: "deptName",
            value: "id"
          }
        }, {
          label: '币别',
          prop: "currency",
          span: 8,
          type: 'select',
          dicData: [
            {
              label: "CNY",
              value: "CNY",
            },
            {
              label: "USD",
              value: "USD",
            }
          ],
        }, {
          label: '账户余额',
          prop: "accountBalance",
          disabled: true
        }, {
          label: '备注',
          prop: "remarks",
          type: 'textarea',
          span: 24,
          minRows: 2
        }]
      }
    }
  },
  props: {
    onLoad: {
      type: Object
    },
    detailData: {
      type: Object
    }
  },
  async created() {
    if (this.onLoad.id) {
      this.queryData(this.onLoad.id)
    }
  },
  methods: {
    //启用禁用
    enableNot() {
      let data = this.form
      editenable({ id: data.id, enableOrNot: data.enableOrNot ? 0 : 1 }).then(res => {
        this.$message({
          type: "success",
          message: data.enableOrNot ? "禁用成功!" : "启用成功!"
        });
        this.$set(this.form, 'enableOrNot', data.enableOrNot == 1 ? 0 : 1)
      })
    },
    // 查询
    queryData(id) {
      const loading = this.$loading({
        lock: true,
        text: '加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(255,255,255,0.7)'
      })
      detail({ id }).then(res => {
        this.form = res.data.data;
        loading.close()
      }).finally(() => {
        loading.close()
      });
    },
    editCustomer() {
      this.$refs["form"].validate((valid, done) => {
        done();
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '加载中',
            spinner: 'el-icon-loading',
            background: 'rgba(255,255,255,0.7)'
          })
          if (!this.form.id && !this.form.enableOrNot) {
            this.form.enableOrNot = 1;
          }
          typeSave(this.form).then(res => {
            loading.close()
            this.$message({
              type: "success",
              message: this.form.id ? "修改成功!" : "新增成功!"
            });
            this.queryData(res.data.data.id);
            if (!this.form.id) {
              //添加成功后默认启用
              let data = this.form
              // editenable({ id: res.data.data.id, enableOrNot: data.enableOrNot ? 0 : 1 }).then(res => {
              //   this.$set(this.form, 'enableOrNot', data.enableOrNot == 1 ? 0 : 1)
              // })
            }
          }).finally(() => {
            loading.close()
          });
        }
      })
    },
    backToList(type) {
      this.$emit("backToList", type);
    }
  }
}
</script>

<style lang="scss" scoped>
.trading-form ::v-deep .el-form-item {
  margin-bottom: 8px !important;
}
</style>
