<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus"
                 @click="handleCreateAdd()">
        增加类目属性
      </el-button>
    </div>
    <el-table :data="tableData" v-loading="listLoading" row-key="id"
              border>
      <el-table-column prop="id" label="编号">
      </el-table-column>
      <el-table-column prop="infoName" label="属性值名称">
      </el-table-column>
      <el-table-column prop="specName" label="属性名称">
      </el-table-column>
      <el-table-column prop="categoryName" label="类目名称">
      </el-table-column>
      <el-table-column prop="sortNum" label="排序">
      </el-table-column>

      <el-table-column label="状态" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            正常
          </el-tag>
          <el-tag v-else="row.voidFlag==1" type="danger">
            停用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="230">
        <template slot-scope="{row}">
          <el-button @click="handleUpdate(row)" type="primary" size="mini">
            编辑
          </el-button>
          <el-button @click="editStatus(row,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
            禁用
          </el-button>
          <el-button @click="editStatus(row,'0')" v-else="row.voidFlag==1" size="mini">
            启用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page"
                :limit.sync="listData.pageSize"
                @pagination="getCategoryInfo"/>


    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">

      <el-form label-width="130px" label-position="right">
<!--        <el-form-item v-if="parentForm" label="商品类目">-->
<!--          <el-button @click="innerBtn()">{{nameTitle}}</el-button>-->
<!--        </el-form-item>-->
        <el-form-item label="属性值名称">
          <el-input v-model="form.infoName"></el-input>
        </el-form-item>
        <el-form-item label="显示排序">
          <el-input v-model="form.sortNum"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <!--    <servicedialog @tableRefresh="tableRefresh" ref="menuDialog" :dialogStatus="dialogStatus" :menuId="menuId" :titleDialog="titleDialog" :dialogVisible="dialogVisible"/>-->
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
  import {
    getSkuCategorySpecValueList,
    addSkuCategorySpecValue,
    updateSkuCategorySpecValue,
    getSkuCategorySpecValueInfoById
  } from '@/api/productFile/categorySpecValue.js'
  import {getMenu} from '@/utils/getTable.js'
  import Cancledialog from '@/components/Dialog/Cancledialog.vue'

  const form = {
    categoryId: '',
    specId:'',
    infoName:'',
    sortNum: '',
  }

  export default {
    components: {
      Cancledialog
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          1: 'success',
          0: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        idCategory: '',
        idSpec:'',
        dialogStatus: 1, //1增加，2修改
        dialogTitle: '',
        dialogVisible: false,



        search: {
          keyWord: ''
        },
        form: Object.assign({}, form),
        parentForm: {},
        listData: {
          total: 0,
          page: 1,
          pageSize: 10
        },

        options: [],

        confirmdialog: false,
        listLoading: false,
        tableData: [],
        menuId: ''
      }
    },
    mounted() {
      this.idCategory = this.$route.query.idCategory
      this.idSpec = this.$route.query.idSpec
      this.getCategoryInfo();
    },
    methods: {
      //获取列表
      getCategoryInfo() {
        const _that = this
        _that.listLoading = true


        var params = _that.search;
        var page = {
          pageIndex: _that.listData.page,
          pageSize: _that.listData.pageSize,
        }
        var data = {
         "idCategory":_that.idCategory,
          "idSpec":_that.idSpec
        }

        getSkuCategorySpecValueList(data).then(response => {
          console.log(response)
          const data = response.data
          // const menuData = getMenu(data)
          // //  组合表单数据
          _that.tableData = data
          // console.log(_that.tableData)
          _that.listData.total = response.total
          _that.listLoading = false
        }).catch(err => {
          _that.$message.error(err)
          _that.listLoading = false
        })
      },
      //增加类目属性
      handleCreateAdd(id) {
        const _that = this
        _that.form = Object.assign({}, form),
        _that.form.categoryId = _that.idCategory
        _that.form.specId = _that.idSpec
        _that.dialogTitle = '增加类目属性'
        _that.dialogStatus = '1';
        _that.dialogVisible = true;

      },
      //刷新变革
      tableRefresh() {
        //刷新变革
        this.getCategoryInfo()
      },
      //增加类目属性
      addBtn() {
        const _that = this
        var printerTypeInfo = JSON.stringify(_that.form)
        var data = {
          "specValue": printerTypeInfo,
        }
        // console.log(data);
        // return

        addSkuCategorySpecValue(data).then(res => {
          // console.log(res);
          if (res.httpCode == 200) {
            _that.dialogVisible = false
            _that.$message({
              message: '增加成功',
              type: 'success'
            });
            _that.tableRefresh();
          } else {
            _that.$message.error(res.msg)
          }
        }).catch(err => {
          _that.$message.error(err)
          _that.dialogVisible = true
        })
      },

      //唤起修改
      handleUpdate(row) {
        const _that = this;
        _that.form = Object.assign({}, form),
        _that.dialogTitle = '修改类目属性信息'
        _that.dialogVisible = true
        _that.dialogStatus = '2';

        var idPrint = row.idProduct
        var data = {
          idCategory:_that.idCategory,
          idSpec: _that.idSpec,
          idValue:row.id
        }

        // console.log(data);
        getSkuCategorySpecValueInfoById(data).then(res => {
          console.log(res);
          var data = res.data
          _that.form = {
            id: data.id,
            categoryId:data.categoryId,
            specId:data.specId,
            sortNum: data.sortNum,
            infoName:data.infoName
          }
        }).catch(err => {
          _that.$message.error(err)
        })
      },
      //修改类目属性
      editBtn() {
        const _that = this
        var printerTypeInfo = JSON.stringify(_that.form)
        var data = {
          "specValue": printerTypeInfo,
        }

        updateSkuCategorySpecValue(data).then(res => {
          // console.log(res);
          if (res.httpCode == 200) {
            _that.dialogVisible = false
            _that.$message({
              message: '修改成功',
              type: 'success'
            });
            _that.tableRefresh();
          } else {
            _that.$message.error(res.msg)
          }
        }).catch(err => {
          _that.$message.error(err)
          _that.dialogVisible = true
        })

      },

      //唤起选择上级科目
      handleNodeClick(data) {
        console.log(data);
        var _that = this;
        _that.handleTree = true;
        _that.form.parentId = data.id
        _that.form.parentName = data.categoryName
        _that.checkName = data.categoryName
        _that.nameTitle = data.categoryName
      },

      //确定选中上级科目
      handleCheck() {
        const _that = this
        _that.innerVisible = false
      },

      //选择上级科目
      innerBtn() {
        var _that = this;
        _that.innerVisible = true;
        _that.checkName = ''
      },

      //启用禁用
      editStatus(row, status) {
        var _that = this;
        var partInfo = {
          categoryId:_that.idCategory,
          specId:_that.idSpec,
          id: row.id,
          voidFlag: status
        }
        console.log(partInfo)
        var params = {
          "specValue": JSON.stringify(partInfo),
        }

        var url = '/product/voidSkuCategorySpecValue';
        _that.$refs.confirmDialog.updataClick(params, url);
      },

      //搜索
      searchData() {
        this.listData.page = 1;
        this.getCategoryInfo()
      }
    }
  }
</script>

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