<script>
import { CreateDialog } from "@/utils/createDialog";
import { resolveFormInfo } from '@/utils/createTable'
import { save } from '@/api/system/baseForm'
export default {
  data() {
    return {
      leaseId: "",
      columns: 1,
      row: {},
      formInfo: [],
      id: "",
      type: "add",
      form: {},
      originalForm: {},
      rules: {},
      file: {},
      dialogVisible: false,
    }
  },
  methods: {
    show () {
      this.dialogVisible = true;
    },
    hidden () {
      this.dialogVisible = false;
    },
    init() {
      for (let key in this.form) {
        if (this.form[key] instanceof Array) {
          this.form[key] = [];
        } else {
          this.form[key] = "";
        }
      }
      this.$refs["elForm"].clearValidate();
    },
    setLeaseId(id) {
      this.leaseId = id;
    },


    submit() {
      if (this.type === "add") {
        if (this.form.hasOwnProperty("id")) {
          delete this.form.id;
        }
      }
      if (this.type === "editor") {
        Object.defineProperty(this.form, "id", {
          value: this.id,
          enumerable: true,
          configurable: true,
          writable: true,
        });
      }

      this.form.lease_id = this.leaseId;
      save(Object.assign(this.form, { table_name: 'lease_pays' })).then(res => {
        this.$Message.success({
          content: `${this.type === "add" ? "新增" : "编辑"}成功`,
        });
        this.$emit("refresh");
        this.hidden();
      })
    },
  },
  computed: {},
  watch: {
    formInfo: {
      handler: function (newVal) {
        this.form = {};
        this.rules = {};
        this.file = {};
        newVal.forEach((i) => {
          if (i.field) {
            this.form[i.field] = "";
            if (
              i.validation instanceof Array &&
              i.validation.length > 0 &&
              !!i.validation.find((i) => i === "required")
            ) {
              this.rules[i.field] = [
                { required: true, message: `请填写${i.name}` },
              ];
            }
            if (i.edit_input === "files") {
              this.form[i.field] = [];
            }
            if (i.edit_input === "files" || i.edit_input === "file") {
              this.file[i.field] = [];
            }
            if (i.edit_input === "checkbox") {
              this.form[i.field] = [];
            }
            if (i._relations) {
              this.form[i._relations?.link_with_name] = [];
            }
          }
        });

        this.columns = newVal.length > 11 ? '2' : '1'
      },
      //immediate: true,
    },
    dialogVisible(val) {
      if (val) {
        document.documentElement.style.setProperty(
          "--column-num",
          this.columns
        );
        if (this.type === "editor" || this.type === "show") {
          this.$nextTick(() => this.getDetail());
        }
      } else {
        this.id = "";
        this.type = "";
        this.init();
        this.$refs["elForm"].clearValidate();
        delete this.form.id;
        for (let key in this.file) {
          this.file[key] = [];
        }
      }
    },
  },
  created() {
    resolveFormInfo(34).then(res => {
      this.formInfo = res;
    })
  },
  render(h) {
    let formRender = new CreateDialog(this, [],{}, false)

    return (
      <el-drawer visible={this.dialogVisible}
                 title="租金收取"
                 size="64%"
                 on={{
                   ['update:visible']:e => {
                     this.dialogVisible = e;
                   }
                 }}>
        <div style="padding: 0 20px;">
          { formRender.render() }
        </div>
      </el-drawer>
    )
  }
}
</script>

<style scoped lang="scss">
</style>
