<template>
  <div class="pageBox">
    <el-card>
      <div class="opaBox">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增品类</el-button>
      </div>
      <el-table :data="tableData" border v-loading="loading" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" style="width: 100%">
        <el-table-column prop="name" label="商品种类"></el-table-column>
        <!-- <el-table-column prop="" label="是否显示">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.show" active-text="是" inactive-text="否" :active-value="1" :inactive-value="0" disabled></el-switch>
          </template>
        </el-table-column> -->
        <el-table-column prop="" label="操作">
          <template slot-scope="scope">
            <el-button icon="el-icon-plus" v-if="scope.row.level == 1" @click="addSubType(scope.row)">新增品种</el-button>
            <el-button type="primary" @click="handleEdit(scope.row.id, scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 编辑 -->
    <el-dialog :title="curInfo.id ? '编辑' : openType == 'pinLei' ? '新增品类' : '新增品种'" :visible.sync="dialogVisible" width="500px">
      <el-form label-width="100px">
        <el-form-item label="品类：" v-if="openType == 'pinZhong'">
          <el-select v-model="curInfo.parent_id" disabled>
            <el-option v-for="item in tableData" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="品类：">
          <treeselect
            :options="treeList"
            placeholder="请选择品类"
            v-model="curInfo.parent_id" />
        </el-form-item> -->
        <el-form-item :label="openType == 'pinZhong' ? '品种名称：' : '品类名称：'">
          <el-input v-model="curInfo.name" clearable style="width: 300px;"></el-input>
        </el-form-item>
        <!-- <el-form-item label="是否显示：">
          <el-switch v-model="curInfo.show" active-text="是" inactive-text="否" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="commitType" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { goods_types, Delgoods_types, Addgoods_types, editGoods_types, goods_types_detail } from "@/api/rankingData"
// import selectTree from '@/components/selectTree'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  data() {
    return {
      loading: true,
      btnLoading: false,
      tableData: [],
      dialogVisible: false,
      curInfo: {
        parent_id: null,
        name: '',
        show: 1,
        id: 0
      },
      defaultProps: {
        value: 'id',
        label: 'name',
        children: 'children'
      },
      treeList: [],
      openType: ''
    }
  },
  mounted() {
    this.getGoods_types()
  },
  methods: {
    // 新增
    handleAdd() {
      this.curInfo = {
        id: 0,
        parent_id: null,
        name: '',
        show: 1
      }
      this.openType = 'pinLei'
      this.dialogVisible = true
    },
    addSubType(row) {
      this.curInfo = {
        id: 0,
        parent_id: row.id,
        name: '',
        show: 1
      }
      this.openType = 'pinZhong'
      this.dialogVisible = true
    },
    // 编辑
    handleEdit(id, item) {
      this.id = id
      this.curInfo = JSON.parse(JSON.stringify(item))
      this.openType = 'edit'
      if(item.parent_id) {
        this.openType = 'pinZhong'
        goods_types_detail(item.parent_id).then((res) => {
          this.dialogVisible = true
        })
      } else {
        this.curInfo.parent_id = null
        this.dialogVisible = true
      }
    },
    // 编辑确定按钮
    commitType() {
      this.btnLoading = true
      let params = {
        ...this.curInfo
      }
      params.parent_id = params.parent_id || 0
      if(this.curInfo.id) {
        editGoods_types(this.curInfo.id, params).then((res) => {
          this.btnLoading = false
          this.$message({ type: 'success', message: '编辑成功' })
          this.getGoods_types()
          this.dialogVisible = false
        }).catch(() => {
          this.btnLoading = false
        })
      } else {
        Addgoods_types(params).then((res) => {
          this.btnLoading = false
          this.$message({ type: 'success', message: '添加成功' })
          this.getGoods_types()
          this.dialogVisible = false
        }).catch(() => {
          this.btnLoading = false
        })
      }
    },
    handleDel(id) {
      this.$confirm("确定删除此条商品种类吗?", "确认删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        Delgoods_types(id).then((res) => {
          this.$message({ type: 'success', message: '删除成功!' })
          this.getGoods_types()
        })
      })
    },
    // 获取列表
    getGoods_types() {
      let page = {
        parent_id: 0
      }
      goods_types(page).then((res) => {
        this.tableData = res.data.data
        this.treeList = JSON.parse(JSON.stringify(res.data.data).replace(/name/g, "label"))
        console.log(this.treeList)
      })
      this.loading = false
    },
    handleNodeClick(row) {
      this.curInfo.parent_name = row.name
      this.curInfo.parent_id = row.id
    },
    changeSelect(row) {
      if(!row) {
        this.curInfo.parent_name = ''
        this.curInfo.parent_id = ''
      }
    },
    getValue(row) {
      console.log(row)
      this.curInfo.parent_id = row ? row : ''
    }
  }
}
</script>

<style lang="scss" scoped>
  .opaBox{
    margin-bottom: 15px;
  }
</style>
