<template>
  <div class="mod-hotSearch-add-or-update">
    <el-dialog :title="!dataForm.id ? '新增' : '修改'"
               :close-on-click-modal="false"
               :visible.sync="visible">
      <el-form :model="dataForm"
               :rules="dataRule"
               ref="dataForm"
               @keyup.enter.native="dataFormSubmit()"
               label-width="120px">
        <el-form-item label="配送费立减" prop="money">
            <el-input-number
              size="small"
              v-model="dataForm.money"
              controls-position="right"
              :precision="2"
              :max="1000000000"
              :min="0.00"
            ></el-input-number>
            元
        </el-form-item>


        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="dataForm.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="dataForm.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
   
        <el-form-item label="状态"
                      size="mini"
                      prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">关闭</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" :loading="loading"
                   @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      dataForm: {
        id: 0,
        money: '',
        startTime: '',
        endTime: '',
        status: 0
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      addProdVisible: false,
      visible: false,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl,
      dataRule: {
        money: [
          { required: true, message: '不能为空', trigger: 'blur' },
          // { min: 1, max: 50, message: '长度在1到50个字符内', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' },
        ],
         startTime: [
          { required: true, message: '请选择有效时间', trigger: 'change' },
        ],
         endTime: [
          { required: true, message: '请选择截止时间', trigger: 'change' },
        ],
       
      }
    }
  },
  components: {},
  methods: {
    init (data) {
      this.visible = true
      if (data) {
        // this.dataForm = Object.assign({}, data)
         this.dataForm = {
            id: data.id,
            money: data.money,
            startTime: data.startTime,
            endTime: data.endTime,
            status: data.status
          }
      } else {
        this.$nextTick(() => {
          this.dataForm = {
            money: '',
            startTime: '',
            endTime: '',
            status: 1
          }
          this.$refs['dataForm'].resetFields()
        })
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.loading = true
          let param = this.dataForm
          this.$http({
            url: this.$http.adornUrl(`/freightFee/addOrUpdate`),
            method:'post',
            data: this.$http.adornData(param)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList', this.page)
                this.$nextTick(() => {
                  this.loading = false
                })
              }
            })
          })
        }
      })
    }
  }
}
</script>
