<template>
  <div class="table">
    <table>
      <thead>
        <tr>
          <td>时间</td>
          <td>上传平台</td>
          <td>链接上传数量</td>
          <td>操作</td>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="(obj, index) in tableData"
          :key="index"
          :class="{ table_gray: index % 2 === 0 }"
        >
          <td>{{ obj.publishMonth || "" }}</td>
          <td>{{ obj.publishPlatformName || "" }}</td>
          <td style="width: 20%">
            <p v-if="!editFlag[index]">{{ obj.publishCount || "" }}</p>
            <input type="text" v-model="newName" v-else />
          </td>
          <td style="width: 25%" class="operation">
            <span @click="edit(index)" v-if="!editFlag[index]">编辑</span>
            <span @click="submitEdit(index)" v-else>确定</span>
            <span @click="showModal(index)">删除</span>
          </td>
        </tr>
      </tbody>
    </table>
    <DeleteModal
      @detlet_data="detletData"
      @hide_modal="closeModal"
      :modalFlag="deleteModalFlag"
    ></DeleteModal>
  </div>
</template>

<script>
import DeleteModal from "../DeleteModal";
export default {
  props: {
    // 表数据
    tableData: {
      type: Array,
      default: () => {
        return [];
      },
    },
    //  表格一页共有多少数据
    pageSize: {
      type: Number,
      default: 2,
    },
  },
  components: {
    DeleteModal,
  },
  data() {
    return {
      deleteModalFlag: false,
      index: 0,
      editFlag: [],
      newName: "", // 编辑之后的值
    };
  },
  methods: {
    edit: function (i) {
      this.getEditArr(); // 确保只有一个在编辑
      this.newName = this.tableData[i] && this.tableData[i].publishCount;
      console.log(this.newName);
      this.editFlag.splice(i, 1, true);
    },
    // 确认编辑
    submitEdit: function (i) {
      let newCount = +this.newName;
      if (!newCount) {
        alert("请填写链接上传数量");
      } else if (newCount === this.tableData[i].publishCount) {
        this.newName = ""; // 没有改
      } else if (Math.ceil(newCount) !== newCount || Math.ceil(newCount) <=0) {
        alert("链接上传数量必须是大于零的整数");
      } else {
        this.$emit("edit", i, newCount);
      }
      this.newName = "";
      this.editFlag.splice(i, 1, false);
    },
    // 点击删除，展示模态框
    showModal: function (i) {
      this.deleteModalFlag = true;
      this.index = i;
    },
    // 模态框确认删除
    detletData: function () {
      this.deleteModalFlag = false;
      this.$emit("delet_data", this.index);
    },
    // 模态框取消
    closeModal: function () {
      this.deleteModalFlag = false;
    },
    // 生成点击编辑按钮所用额flag，
    getEditArr: function () {
      let arr = new Array(this.pageSize).fill(false);
      this.editFlag = arr;
    },
  },
  created() {
    this.getEditArr();
  },
};
</script>

<style scoped lang="less">
.table {
  background-color: #fff;
  width: 100%;
  margin-top: 10px;
  text-align: center;
  table {
    width: 100%;
    border-collapse: collapse;
    border: none;
    thead {
      td {
        background: #8d9092;
        height: 36px;
        color: #fff;
        border: 1px solid #ccc;
      }
    }
    tbody {
      td {
        height: 36px;
        border: 1px solid #ccc;
      }
      .operation {
        span {
          color: #00f;
          margin: 0 20px;
          &:hover {
            cursor: pointer;
          }
        }
      }
    }
  }
}
.table_gray {
  background: #f5f5f5;
}
</style>
