<template>
  <Container>
    <div slot="tip" class="page-tip-div" style="margin-top: 0">
      <i class="el-icon-info"></i>
      <span>
        温馨提示：属性选择使用后，若编辑或者删除属性组或者属性名称，会造成商品属性有误，请谨慎操作！！
      </span>
    </div>
    <div slot="left">
      <el-button
        v-if="$accessCheck($Access.specManageAdd)"
        size="small"
        type="primary"
        @click="openModel(false, '属性名')"
      >
        新增属性名
      </el-button>
      <el-button
        v-if="$accessCheck($Access.specManageAdd)"
        size="small"
        type="primary"
        @click="openModel(false, '属性值')"
      >
        新增属性值
      </el-button>
    </div>
    <el-table
      class="tree-table"
      :data="spec_data"
      row-key="id"
      :row-class-name="tableRowClassName"
      :tree-props="{ children: 'child' }"
    >
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column prop="specName" label="名称"></el-table-column>
      <el-table-column prop="address" label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="$accessCheck($Access.specManageEdit)"
            type="text"
            @click="
              openModel(true, !scope.row.pid ? '属性名' : '属性值', scope.row)
            "
          >
            编辑
          </el-button>
          <el-button
            v-if="$accessCheck($Access.specManageDel)"
            type="text"
            @click="delData(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
    <el-dialog
      :title="(is_edit ? '编辑' : '新增') + spec_type"
      :visible.sync="show_model"
      width="40%"
      @close="show_model = false"
    >
      <el-form ref="form" :model="add_form" :rules="rules" label-width="100px">
        <el-form-item label="名称:" prop="specName">
          <el-input v-model="add_form.specName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item v-if="spec_type === '属性值'" label="属性名:" prop="pid">
          <el-select v-model="add_form.pid" placeholder="请选择属性名">
            <el-option
              v-for="(item, index) in edit_spec_data"
              :key="index"
              :label="item.specName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button :loading="sub_loading" @click="show_model = false">
          取 消
        </el-button>
        <el-button :loading="sub_loading" type="primary" @click="addData">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </Container>
</template>

<script>
  import {
    SpecManagegetAll,
    SpecManageAdd,
    SpecManageedit,
    SpecManagedel,
  } from "@/api/goods";
  export default {
    name: "SpecManage",
    data() {
      const pidRequied = () => {
        return this.spec_type === "属性值";
      };
      return {
        sub_loading: false,
        pageSize: 10,
        page: 1,
        total: 0,
        show_model: false,
        is_edit: false,
        unit_id: 0,
        add_form: {
          pid: "",
          specName: "",
        },
        rules: {
          specName: [{ required: true, message: "请输入名称" }],
          pid: [{ required: pidRequied, message: "请选择属性名" }],
        },
        edit_spec_data: [],
        spec_data: [],
        spec_type: "",
      };
    },
    created() {
      this.getAllUnit();
    },
    activated() {
      if (this.$_isInit()) return;
      this.getAllUnit();
    },
    methods: {
      // 获取列表
      async getAllUnit() {
        const { data, pageTotal } = await SpecManagegetAll({
          page: this.page,
          pageSize: this.pageSize,
        });

        this.spec_data = data;
        this.total = pageTotal;
      },
      // 获取列表
      async getAllUnitE() {
        const { data, pageTotal } = await SpecManagegetAll({
          page: 1,
          pageSize: 100,
        });

        this.edit_spec_data = data;
      },

      async addData() {
        if (!this.add_form.specName.trim()) {
          this.$message.warning("名称不能为空");
          return;
        }
        this.$refs["form"].validate(async (valid) => {
          if (valid) {
            let target = {};
            this.sub_loading = true;
            try {
              if (!this.is_edit) {
                target = await SpecManageAdd({
                  ...this.add_form,
                  pid: this.add_form.pid || 0,
                });
              } else {
                target = await SpecManageedit(this.unit_id, {
                  ...this.add_form,
                });
              }
              this.sub_loading = false;

              this.show_model = false;
              this.pageChange(1);
            } finally {
              this.sub_loading = false;
            }
          }
        });
      },
      sizeChange(size) {
        this.pageSize = size;
        this.pageChange(1);
      },
      pageChange(page) {
        this.page = page;
        this.getAllUnit();
      },
      openModel(isEdit, type, row) {
        this.getAllUnitE();
        this.spec_type = type;
        this.add_form = {
          pid: "",
          specName: "",
        };
        this.show_model = true;
        this.is_edit = isEdit;
        if (row) {
          this.unit_id = row.id;
          this.add_form = {
            pid: row.pid,
            specName: row.specName,
          };
        }
      },
      delData(id) {
        this.$confirm("确定要删除该属性吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await SpecManagedel(id);
          this.getAllUnit();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      },
      tableRowClassName({ row, rowIndex }) {
        if (row.pid) {
          return "success-row";
        }
        return "";
      },
    },
  };
</script>

<style scoped></style>
