<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="clearfix">
        <span class="left-text">商品分组</span>
        <el-button
          type="primary"
          size="mini"
          icon="el-icon-plus"
          style="float: right"
          @click="handleAdd()"
          >新增</el-button
        >
      </div>
      <!-- 分组表格 -->
      <el-table :data="tableData" :height="tableHeight" style="width: 100%">
        <el-table-column prop="id" label="分组ID" width="180">
        </el-table-column>
        <el-table-column prop="name" label="分组名称" width="180">
        </el-table-column>
        <el-table-column prop="goods_num" label="商品数量"> </el-table-column>
        <el-table-column prop="created_time" label="创建时间">
          <template slot-scope="scope">
            <div>
              {{ timeFormatter(scope.row.created_time) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div v-if="!scope.row.seller_id == 0">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
                >编辑
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"
                style="color: red"
                >删除
              </el-button>
            </div>
            <div v-if="scope.row.seller_id == 0">--</div>
            <!-- <el-divider direction="vertical"></el-divider> -->
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <!-- <div class="footer_pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[20, 40, 60, 80, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div> -->
    </el-card>
    <!-- 新增或修改弹窗 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      center
      @close="clearPayform"
      width="40%"
    >
      <div style="width: 100%; margin: 50px auto 200px">
        <el-form
          :model="formInline"
          ref="formInline"
          :rules="formInlines"
          label-width="150px"
          size="mini"
        >
          <el-form-item label="分组名称：" prop="name">
            <el-input
              v-model="formInline.name"
              style="width: 80%"
              placeholder="请输入商品分组名称"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          @click="closeDialogVisible('formInline')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 删除弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogDelete"
      center
      :close-on-click-modal="false"
      width="30%"
    >
      <div style="text-align: center; margin: 0 auto">
        <p>
          确定要删除该分组吗？删除后，该分组的商品将分配到系统默认分组里面。商品的相关设置及状态保持不变。
        </p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogDelete = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini" @click="closeDialogDelete()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  groupList,
  addGroup,
  updateGroup,
  GroupInfo,
  deleteGroup,
} from "@/api/module/settings";
import { dateFormat } from "@/utils";
export default {
  name: "commodityGroup",
  data() {
    return {
      fullHeight: 0,
      tableHeight: 0,
      code: 0,
      tableData: [],
      //新增和修改提示框分组名称
      formInline: {
        name: "",
      },
      //表单验证
      formInlines: {
        name: [
          { required: true, message: "分组名称不能为空", trigger: "blur" },
        ],
      },
      title: "",
      //新增修改提示框
      dialogVisible: false,
      //删除提示框
      dialogDelete: false,
    };
  },
  created() {
    this.$nextTick(() => {
      this.fullHeight =
        document.getElementsByClassName("box-card")[0].clientHeight;
    });
    this.getGroupList();
  },
  watch: {
    fullHeight(val) {
      let formHeight =
        document.getElementsByClassName("clearfix")[0].clientHeight;
      this.tableHeight = val - formHeight - 100;
    },
  },
  methods: {
    //新增分组
    handleAdd() {
      this.dialogVisible = true;
      this.title = "新增分组";
    },
    //删除分组
    handleDelete(val) {
      this.code = val.id;
      this.dialogDelete = true;
    },
    //编辑分组
    handleUpdate(val) {
      this.title = "编辑分组";
      this.code = val.id;
      let obj = {
        id: val.id,
      };
      GroupInfo(obj).then((res) => {
        if (res.code == 1) {
          this.formInline.name = res.data.name;
          this.dialogVisible = true;
        }
      });
    },
    //新增或修改弹框确定回调
    closeDialogVisible(formInline) {
      this.$refs[formInline].validate((valid) => {
        if (valid) {
          let obj = {
            name: this.formInline.name,
          };
          if (this.title == "新增分组") {
            addGroup(obj).then((res) => {
              if (res.code == 1) {
                this.$message({
                  type: "success",
                  message: "添加成功",
                });
                this.getGroupList();
              } else if (res.code == 0) {
                this.$message({
                  type: "error",
                  message: res.message,
                });
              }
            });
          }
          if (this.title == "编辑分组") {
            obj["id"] = this.code;
            updateGroup(obj).then((res) => {
              if (res.code == 1) {
                this.$message({
                  type: "success",
                  message: "编辑成功",
                });
                this.getGroupList();
              } else {
                this.$message({
                  message: res.message,
                  type: "error",
                });
              }
            });
          }
          this.dialogVisible = false;
        }
      });
    },
    //删除提示框
    closeDialogDelete() {
      let obj = {
        id: this.code,
      };
      deleteGroup(obj).then((res) => {
        if (res.code == 1) {
          this.$message({
            type: "success",
            message: "删除成功",
          });
          this.getGroupList();
        } else {
          this.$message({
            message: res.message,
            type: "error",
          });
        }
      });
      this.dialogDelete = false;
    },
    /** 一般时间 格式化 */
    timeFormatter(time) {
      if (time) {
        return dateFormat(time * 1000, "Y-m-d H:i:s");
      } else {
        return "--";
      }
    },
    getGroupList() {
      groupList().then((res) => {
        if (res.code == 1) {
          this.tableData = res.data.list;
        } else {
          this.$message({
            message: res.message,
            type: "error",
          });
        }
      });
    },
    clearPayform() {
      this.formInline.name = "";
    },
  },
};
</script>
<style lang="scss" scoped type="text/stylus">
.app-container {
  overflow: auto;
}
.clearfix {
  margin: 10px 0 15px 0;
}
.left-text {
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #333333;
  line-height: 28px;
}
/* 分页 */
.footer_pagination {
  text-align: center;
  margin-top: 15px;
}
</style>