<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>
    <div class="table-con">
      <el-table
        :data="tableData"
        v-loading="listLoading"
        style="width: 100%;margin-bottom: 20px;"
        border
      >
        <el-table-column
            label="#"
            align="center"
            type="index"
            :index="indexMethod">
        </el-table-column>
        <el-table-column label="操作" align="left" width="230" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button @click="handleUpdate(row.id)" type="primary" size="mini">
              编辑
            </el-button>
            <el-button @click="editStatus(row.id,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
              禁用
            </el-button>
            <el-button @click="editStatus(row.id,'0')" v-else="row.voidFlag==1" size="mini">
              启用
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          prop="groupName"
          label="反馈组名称">
        </el-table-column>
        <el-table-column
          prop="sortIndex"
          label="排序">
        </el-table-column>
        <el-table-column
          label="创建时间"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.createTime">{{scope.row.createTime | getTime}}</span>
          </template>
        </el-table-column>

      </el-table>
      <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page" :limit.sync="listData.pageSize" @pagination="getTableInfo" />
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="反馈组名称">
          <el-input  v-model="form.groupName"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input  v-model="form.sortIndex"></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>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import {
  getInfo,
  getInfoById,
  addInfo,
  updateInfo,
  queryFeedbackGroupWithTasks
} from '@/api/outsideFactory/feedbackGroupList/index.js'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from "@/components/Pagination/index";
export default {
  name: "list",
  components: {
    Cancledialog,Pagination
  },
  data() {
    return {
      dialogStatus: 1,//1增加，2修改
      form:{},
      confirmdialog:false,
      dialogVisible:false,
      dialogTitle:"",
      listLoading: false,
      tableData: [],
      search: {
        keyWord:''
      },
      listData:{
        total:0,
        page: 1,
        pageSize:10
      }
    }
  },
  mounted() {
    this.getTableInfo();
  },
  methods: {
    //启用禁用
    editStatus(id,status){
      var _that = this;
      var data = {
        id:id,
        voidFlag:status
      }
      var params = {
        "feedbackGroup": JSON.stringify(data),
      }

      var url = '/outassistDoc/voidFeedbackGroup';
      _that.$refs.confirmDialog.updataClick(params, url);
    },
    handleUpdate(id) {
      console.log(id);
      const _that = this
      _that.dialogTitle = '修改反馈组信息'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
      _that.byId = id
      var data = {
        idGroup: id
      }
      getInfoById(data).then(res => {
        //获取详情
        var datas = res.data;
        console.log(datas);
        _that.form = {
          id: id,
          groupName: datas.groupName,
          sortIndex: datas.sortIndex
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    editBtn(){
      //修改
      const _that = this;
      console.log(_that.form);
      var info =JSON.stringify(_that.form)
      var data = {
        "feedbackGroup": info
      }
      // console.log(data);

      updateInfo(data).then(res => {
        console.log("updateInfo",res);
        if(res.httpCode == 200){
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          });
          _that.getTableInfo();
        }else{
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },
    addBtn(){
      //增加
      const _that = this;
      var info =JSON.stringify(_that.form)
      console.log(info);
      var data = {
        "feedbackGroup": info ,
      }

      // console.log("clientLevel",data);
      addInfo(data).then(res => {
        console.log(res);
        if(res.httpCode == 200){
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          });
          _that.getTableInfo();
        }else{
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },
    handleCreateAdd(){
      //增加反馈组来源
      const _that = this
      _that.form={
        levelName:''
      }
      _that.dialogTitle = '增加反馈组'
      _that.dialogStatus = '1';
      _that.dialogVisible = true;

    },
    //获取表格数据
    getTableInfo(){
      const _that = this
      _that.listLoading = true
      var params = _that.search;
      var page = {
        pageIndex:_that.listData.page,
        pageSize: _that.listData.pageSize,
      }
      var data = {
        "params": {}
      }
      // console.log(data);
      getInfo(data).then(response => {
        console.log("tableData",response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },
    //搜索
    searchData(){
      this.listData.page = 1;
      this.getTableInfo()
    },
    // 刷新表格
    tableRefresh() {
      this.getTableInfo()
    },
    //序号
    indexMethod(index) {
      return index+1;
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #f0f2f5;
  padding: 30px;
  min-height: calc(100vh - 84px);
}
.filter-container{
  margin-bottom: 15px;
}
</style>
