
<template>
  <div class="labelList app-container">
   <div class="search">
      <div>
        <el-input
          v-model="query.labelName"
          size="small"
          placeholder="请输入名称"
          class="item-width-200 "
        ></el-input>
          <el-select
          v-model="platformType"
          multiple
          size="small"
          clearable
          class="item-width-200 ml10"
          placeholder="请选择主商户"
        >
          <el-option
            v-for="item in masterShopOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
         <el-button
        class="ml10"
        type="primary"
        size="small"
         icon="el-icon-search"
        @click="handleSearch"
      >
        搜索
      </el-button>
        <!-- <el-select
          v-model="query.columnId"
          size="small"
          clearable
          class="item-width-200 ml10"
          placeholder="请选择栏目"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select> -->
      </div>
      <el-button class="add-btn" type="primary" size="small" @click="handleAdd"
        >添加</el-button
      >
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
 
      <el-table-column
        prop="labelName"
        align="center"
        label="标签名称"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column
        prop="accountChildType"
        align="center"
        label="所属平台"
        fit
      >
      <template slot-scope="scope">
        {{filterMasterName(scope.row.platformType )}}
      </template>
      </el-table-column>
      
      <el-table-column
        prop="createTime"
        align="center"
        label="创建时间"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column prop="address" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="530px">
       <el-form
        :model="setQuery"
        ref="setQuery"
        :rules="rules"
        label-width="130px"
        >
          
        <el-form-item label="标签名称：" prop="labelName">
            <el-select
            v-model="setQuery.platformType"
            clearable
            class="item-width-300"
            placeholder="请选择主商户"
          >
            <el-option
              v-for="item in masterShopOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称：" prop="labelName">
            <el-input
            v-model="setQuery.labelName"
            :maxlength="6"
            class="item-width-300"
            ></el-input>
        </el-form-item>
        </el-form>
         <div class="btn-group">
            <el-button @click="cancle">取 消</el-button>
            <el-button type="primary" :loading="btnLoading"  @click="save">确 定</el-button>
        </div>
    </el-dialog>


  </div>
</template>
  
  <script>
import { debounce } from "@/utils/index";
import {getLabelList,delLabel,addOrUpdateLabel} from '@/api/goods';
export default {
  name: "labelList",
  data() {
    return {
        dialogFormVisible:false,
      tableData: [],
      options:[],
      loading: false,
      btnLoading: false,
      platformType:"",
      query: {
        platformType:null,
        // labelType: '',
        labelName: "",
        currentPage: 1,
        pageSize: 10,
      },
      title: "",
      total: 0,
      setQuery:{
        platformType:"",
        "id": 0,
        "labelName": "",
        "labelType": ''
      },
      rules: {
        platformType: [{ required: true, message: "请选择主商户", trigger: "change" }],
        labelName: [{ required: true, message: "请输入标签名称", trigger: "blur" }],
      },
    };
  },
   watch: {
    platformType(val) {
        if(val.length){
          this.query.platformType = val.join(',')
        }else{
          this.query.platformType = null
        }
      }
  },
  methods: {
    handleSearch(){
      this.query.currentPage = 1
      this.getList();

    },
    handleEdit(row){
     this.dialogFormVisible = true
     this.title = '编辑标签'
      this.setQuery ={
        "id": row.id,
        "labelName": row.labelName,
        // "labelType": row.labelType
      }
    },
    handleAdd(){
     this.dialogFormVisible = true
     this.title = '添加标签'
     this.setQuery ={
        "id": 0,
        "labelName": "",
        // "labelType": 0
      }
    
    },
    // 点击删除
    handleDel(id) {
       this.$confirm(`此操作将删除该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delLabel({id}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        })
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
    this.getList()
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
    this.getList()
    },
    getList(){
      this.loading = true
      getLabelList(this.query).then(res=>{
        this.loading = false
        if(res.state == 'Success'){
          this.tableData = res.content.records
          this.total = res.content.total
        }
      })
    },
    cancle(){
         this.dialogFormVisible = false

    },
    save() {
      this.$refs.setQuery.validate((v) => {
        if (v) {
          this.btnLoading = true
         
          addOrUpdateLabel(this.setQuery).then(res=>{
            this.btnLoading = false
            if(res.state == 'Success'){
                this.dialogFormVisible = false
                this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success'
                });
               this.handleSearch()
            }
          })
              
        }
      });
    },
  },
  created() {
    this.getList()
  },
};
</script>
  
<style lang="scss" scoped>
.labelList {
  .search{
    justify-content: space-between;
  }
  .icon{
    width: 80px;
    height: 80px;
  }
   .btn-group{
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
  