<template>
  <div class="app-container">
    <div class="thirty-percent">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="客服热线" prop="hotline">
          <el-input v-model="form.hotline" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getConnectList, editConnectConfig } from "@/api/connect/index";
export default {
  data() {
    var checkPhone = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入客服热线"));
      } else if (!/^1[3|4|5|7|8][0-9]{9}$/.test(value)) {
        callback(new Error("请输入正确的手机号码"));
      } else {
        callback();
      }
    };
    return {
      form: {
        hotline: "",
        id: "",
      },
      rules: {
        hotline: [{ validator: checkPhone, required: true, trigger: "blur" }],
      },
    };
  },
  mounted() {
    console.log("金");
    this.getConnect();
  },
  methods: {
    //获取联系电话列表
    getConnect() {
      let that = this;
      getConnectList().then((res) => {
        console.log("列表", res);
        const { code, rows } = res;
        if (res.code == 200) {
          if (rows.length) {
            that.form = rows[0];
            // that.$set(that.form,'hotline',rows[0].hotline)
            // that.$set(that.form,'id',rows[0].id)
          }
        }
      });
    },
    handleSave() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // alert('submit!');
          editConnectConfig(this.form).then((res) => {
            const { code } = res;
            if (code != 200) return this.$message.error("修改失败!");
            this.getConnect();
            this.$message.success("修改成功!");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.thirty-percent {
  width: 30%;
}
</style>
