<template>
  <!-- 添加或修改部门对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="form.nickname" placeholder="请输入昵称" maxlength="10" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="form.gender">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身高" prop="height">
            <el-input v-model="form.height" placeholder="请输入身高">
              <template slot="suffix">cm</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="体重" prop="weight">
            <el-input v-model="form.weight" placeholder="请输入体重">
              <template slot="suffix">kg</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出生日期" prop="birthday">
            <el-date-picker size="small" v-model="form.birthday" type="date" placeholder="选择出生日期"
              value-format="yyyy-MM-dd" :picker-options="pickerOptions" style="width: 100%">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="目标步数" prop="targetStep">
            <el-input-number v-model="form.targetStep" controls-position="right" :min="0" style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="目标卡路里" prop="targetCalorie" label-width="100px">
            <el-input-number v-model="form.targetCalorie" controls-position="right" :min="0"
              style="width: calc(100% - 30px)" />
            <span style="padding-left: 5px">kal</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>


<script>
import { isNum, isNumber } from "@/utils/validate.js";
import { setUserInfo } from "@/api/watch/deviceInfoSet.js";
export default {
  data() {
    const checkIsNum = (rule, value, callback) => {
      if (!isNum(value) || value < 50) {
        callback(new Error("请输入正确的数字"));
      } else {
        callback();
      }
    };
    const checkIsNumber = (rule, value, callback) => {
      if (!isNumber(value) || value < 5) {
        callback(new Error("请输入正确的数字"));
      } else {
        callback();
      }
    };
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickname: [
          {
            required: true,
            message: "昵称不能为空",
            trigger: ["blur", "change"],
          },
        ],
        gender: [{ required: true, message: "请选择性别", trigger: "change" }],
        height: [
          {
            required: true,
            message: "身高不能为空",
            trigger: ["blur", "change"],
          },
          { validator: checkIsNum, trigger: ["blur", "change"] },
        ],
        weight: [
          {
            required: true,
            message: "体重不能为空",
            trigger: ["blur", "change"],
          },
          { validator: checkIsNumber, trigger: ["blur", "change"] },
        ],
        birthday: [
          { required: true, message: "请选择出生日期", trigger: "change" },
        ],
        targetStep: [
          {
            required: true,
            message: "目标步数不能为空",
            trigger: ["blur", "change"],
          },
        ],
        targetCalorie: [
          {
            required: true,
            message: "目标卡路里不能为空",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        nickname: undefined,
        gender: undefined,
        height: undefined,
        weight: undefined,
        birthday: undefined,
        targetStep: undefined,
        targetCalorie: undefined
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (row == null) {
        this.reset();
      } else {
        this.form = { ...row };
        // 将性别字符串转换为相应的 el-radio 的 label 值
        if (row.gender === "男") {
          this.form.gender = 1;
        } else if (row.gender === "女") {
          this.form.gender = 2;
        }
      }
      //   getDept(row.deptId).then((response) => {
      //     this.form = response.data;
      this.open = true;
      this.title = "修改基本信息";
      //   });
      //   listDeptExcludeChild(row.deptId).then((response) => {
      //     this.deptOptions = this.handleTree(response.data, "deptId");
      //   });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.deviceId != undefined) {
            //下发用户设置到设备
            setUserInfo(this.form).then(Response => {
              this.open = false;
            })
          }
        }
      });
    },
  },
};
</script>

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