/*
 * @Author: zwq
 * @Date: 2022-08-24 11:19:43
 * @LastEditors: zwq
 * @LastEditTime: 2023-03-03 10:38:42
 * @Description: 
 */
export default {
  data() {
    /* eslint-disable */
    return {
      urlOptions: {
        submitURL: '',
        infoURL: '',
        getOption: false,
        optionArrUrl: [],
        optionArr: {}
      },
      visible: false,
      setData: false,
      isGetCode: false
    }
  },
  created() {
  },
  activated() {
  },
  methods: {
    init(id) {
      this.dataForm.id = id || "";
      this.visible = true;
      if (this.urlOptions.getOption) {
        this.getArr()
      }
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http
            .get(`${this.urlOptions.infoURL}/${this.dataForm.id}`)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.dataForm = res.data;
              if (this.setData) {
                this.setDataForm()
              }
            })
            .catch(() => { });
        } else {
          if (this.isGetCode) {
            this.getCode()
          }
        }
      });
    },
    getArr() {
      this.urlOptions.optionArrUrl.forEach((item, index) => {
        this.$http
          .get(item, {
            params: {
              page: 1,
              limit: 500,
            },
          })
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$set(this.urlOptions.optionArr, `arr${index}`, res.data.list)
          })
          .catch(() => {
          });
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.$http[!this.dataForm.id ? "post" : "put"](this.urlOptions.submitURL, this.dataForm)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: this.$t("prompt.success"),
              type: "success",
              duration: 500,
              onClose: () => {
                this.visible = false;
                this.$emit("successSubmit");
              },
            });
          })
          .catch(() => { });
      });
    },
    formClear() {
      this.$refs.dataForm.resetFields()
    }
  }
}