<template>
  <div>
    <el-card>
      <el-button type="primary"
                 @click="handAdd">新增</el-button>
      <el-table ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 100%"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection"
                         width="55">
        </el-table-column>
        <el-table-column prop="name"
                         label="商品种类">
        </el-table-column>
        <el-table-column prop=""
                         label="操作">
          <template slot-scope="scope">
            <el-button type="primary"
                       @click="handEdit(scope.row.id,scope.row)">编辑</el-button>
            <el-button type="danger"
                       @click="handdel(scope.row.id)">删除</el-button>
          </template>

        </el-table-column>
      </el-table>
    </el-card>
    <el-pagination @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"
                   :current-page="page"
                   :page-size="per_page"
                   layout="total, sizes, prev, pager, next, jumper"
                   :total="total">
    </el-pagination>
    <!-- 新增种类对话框 -->
    <el-dialog title="新增"
               :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="商品种类"
                      :label-width="formLabelWidth">
          <el-input v-model="form.kindName"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog title="编辑"
               :visible.sync="dialogFormVisible1">
      <el-form :model="form1">
        <el-form-item label="编辑种类"
                      :label-width="formLabelWidth1">
          <el-input v-model="form1.kindName1"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible1 = false">取 消</el-button>
        <el-button type="primary"
                   @click="EditSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  goods_types,
  Delgoods_types,
  Addgoods_types,
  editGoods_types,
} from '../../api/rankingData'
export default {
  data() {
    return {
      id: '', //每一项的id
      page: 1, //开始页
      per_page: 10, //一页多少数据
      total: 1,
      tableData: [],
      multipleSelection: [],
      currentPage4: 4,
      dialogFormVisible: false,
      dialogFormVisible1: false,
      form: {
        kindName: '', //种类名
      },
      form1: {
        kindName1: '', //种类名
      },
      formLabelWidth: '120px',
      formLabelWidth1: '120px',
      newKind: [],
    }
  },
  created() {
    this.getGoods_types()
  },
  methods: {
    // 复选框按钮
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.per_page = val
      this.page = 1
      this.getGoods_types()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page = val
      this.getGoods_types()
    },
    // 新增
    handAdd() {
      this.form.kindName = ''
      this.dialogFormVisible = true
    },
    // 确认新增
    addSubmit() {
      var string
      string = this.form.kindName.replace(/\s/g, ',').split(',')
      console.log(string, 'lkkkk')
      Addgoods_types({
        names: string,
      }).then((res) => {
        this.$message({
          type: 'success',
          message: '添加成功',
        })
        this.getGoods_types()
      })
      this.dialogFormVisible = false
    },
    // 编辑
    handEdit(id, item) {
      this.id = id
      console.log('item', item)
      this.form1.kindName1 = item.name
      this.dialogFormVisible1 = true
    },
    // 编辑确定按钮
    EditSubmit() {
      editGoods_types(this.id, {
        name: this.form1.kindName1,
      }).then((res) => {
        this.$message({
          type: 'success',
          message: '编辑成功',
        })
        this.getGoods_types()
      })
      this.dialogFormVisible1 = false
    },
    // 删除
    handdel(id) {
      this.$confirm('确定删除此条商品种类吗?', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          Delgoods_types(id).then((res) => {
            this.getGoods_types()
          })
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 获取列表
    getGoods_types() {
      goods_types({
        page: this.page,
        per_page: this.per_page,
      }).then((res) => {
        console.log('res=>>>>>>>>', res)
        this.tableData = res.data.data
        this.total = res.data.data.length
        this.page = res.meta.current_page
        this.per_page = res.meta.per_page
      })
    },
  },
}
</script>

<style>
</style>