<template>
  <div class="hgxsc">
    <el-form
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      :rules="rules"
    >
      <el-form-item label="分析范围:" prop="xzmj">
        <range type="hgxfx" :keys="['hx', 'sc']" class="range" ref="range" />
      </el-form-item>

      <el-form-item label="预检名称:" prop="xmmc">
        <el-input
          v-model="ruleForm.xmmc"
          size="mini"
          placeholder="请输入预检名称"
        ></el-input>
      </el-form-item>
    </el-form>

    <div class="bottomBtns">
      <span class="clearBtn" @click="reset">取消</span>
      <span class="sureBtn" @click="submitData">确定</span>
    </div>
  </div>
</template>

<script>
import { Add } from "@/api/ghss/hgxfx.js";
import { Message, MessageBox } from "element-ui";
import range from "@/components/mapView/range.vue"; ///mapview/range
import moment from "moment";
export default {
  components: {
    range,
  },
  props: {},

  data() {
    return {
      ruleForm: {
        xmmc: "",
        xzfw: "",
        xzmj: 0,
      },
      rules: {
        xzmj: [{ required: true, message: "请填写范围的数据" }],
        xmmc: [
          { required: true, message: "请输入预检名称", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.initform();
  },
  methods: {
    initform() {
      this.ruleForm.xmmc = `整治预检_${moment(new Date()).format(
        "YYYYMMDDHHmmss"
      )}`;
    },

    reset() {
      this.ruleForm = {
        xmmc: "",
        xzfw: "",
        xzmj: 0,
      };
      this.$refs.range.reset();
    },
    submitData() {
      //更新范围
      var _temp = this.$refs.range.getRange();
      this.ruleForm.xzfw = _temp.xzfw;
      this.ruleForm.xzmj = _temp.xzmj;
      console.log(this.ruleForm);
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (!this.ruleForm.xzfw) {
            Message.warning("请绘制或导入选址范围!");
            return;
          }

          MessageBox.confirm("是否开始进行整治预检?", "整治预检", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            this.$emit("updateParent", "loading", true);
            Add({ ...this.ruleForm }).then((res) => {
              if (res.success) {
                this.$emit("updateParent", "nowObj", this.ruleForm);
                this.$emit("updateParent", "rzBsm", res.message);
                this.reset();
              } else {
                Message.warning(res.message);
              }
              this.$emit("updateParent", "loading", false);
            });
          });
        }
      });
    },
  },
  watch: {},
};
</script>

<style lang="scss" scoped>
.hgxsc {
  height: 100%;
  line-height: 40px;
  .rangDiv {
    width: 100%;
    // height: 100px;
    display: flex;
    justify-content: space-between;
  }
  .range {
    flex: 1;
    width: 100%;
    // position: absolute;
    // left: 100px;
  }
}
/deep/ .el-input .el-input--mini .el-input--suffix {
  width: 100%;
}
/deep/ .el-select {
  width: 100%;
}
/deep/ .el-form-item__error {
  top: 32px !important;
}
</style>
