<template>
  <div>
    <!-- 标题 -->
    <el-row>
      <el-col
        :span="24"
        class="first-row"
      >
        <div class="tag">消费占比</div>
      </el-col>
    </el-row>

    <!-- 分割线 -->
    <hr style="background-color: #CCCCCC;height: 1px;border: 0;">
    <div style="margin: 32px 0;"></div>

    <el-row>
      <el-col
        v-for="item in list"
        :key="item.id"
        :span="9"
        style="marginTop: 32px"
      >
        <el-col :span="8">
          <span>{{item.id}}、{{item.categoryName}}</span>
        </el-col>

        <el-input
          class="input-new-tag"
          v-if="item.show"
          v-model="inputValue"
          v-fo
          size="small"
          @blur="handleInputConfirm(item)"
        >
        </el-input>
        <el-button
          v-else
          class="button-new-tag"
          size="small"
          @click="showInput(item)"
        >{{item.value}}</el-button>
        万

      </el-col>
    </el-row>

  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      inputValue: "",
      updateId: "",
      list: [],
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 获取消费占比数据
    async getData() {
      let res = await this.$axios.get(
        "/diseaseRight/consumptionCategoryStatistics/list"
      );
      // console.log(res);
      if (res.data.success) {
        let newArr = [];
        res.data.data.forEach((element) => {
          newArr.push({
            id: element.id,
            categoryName: element.categoryName,
            value: Math.round(element.value),
            show: false,
          });
        });
        this.list = newArr;
      } else {
        console.log("获取 消费占比数据 失败");
      }
    },
    // 修改消费占比数据
    async amendData() {
      let data = {
        id: this.updateId,
        value: this.inputValue,
      };
      let res = await this.$axios({
        url: "/diseaseRight/consumptionCategoryStatistics/update",
        method: "put",
        data,
        headers: {
          "Admin-Token": sessionStorage.getItem("token"),
        },
      });
      // console.log(res);
      if (res.data.success) {
        this.$message({
          showClose: true,
          message: "修改成功",
          type: "success",
        });
        this.getData();
      } else {
        this.$message({
          showClose: true,
          message: "修改失败",
          type: "error",
        });
        this.getData();
      }
    },
    // 点击按钮 切换输入框 并聚焦
    showInput(i) {
      i.show = true;
    },
    // 失去焦点 切换回按钮 并更新数据
    handleInputConfirm(i) {
      this.updateId = i.id;
      i.show = false;
      i.value = this.inputValue;
      this.amendData();
      this.inputValue = "";
    },
  },
};
</script>

<style  scoped>
.first-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
}
.tag {
  width: 108px;
  height: 40px;
  font-size: 24px;
  font-family: Microsoft YaHei-3970(82674968);
  font-weight: bold;
  color: #000000;
  line-height: 54px;
}
.input-new-tag {
  margin-right: 10px;
  width: 90px;
  vertical-align: middle;
}
.button-new-tag {
  margin-right: 10px;
  width: 90px;
  height: 32px;
  vertical-align: middle;
}
</style>