<template>
  <div>
    <el-row :gutter="10" class="mb8" style="margin: 15px;">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qw:contactWayGroup:add']"
        >新增分组</el-button>
      </el-col>
    </el-row>
    <div>
    <el-table v-loading="loading" :data="materialGroupList"
              @row-click="changeCurrentRow"
              :row-style="rowStyle"
              :cell-style="setSellStyle" height="600px">
      <!-- 保留的列 -->
      <el-table-column label="组名" align="center" prop="groupName" >
        <template slot-scope="scope">
          <i v-if="scope.row.special" class="el-icon-star-off" style="color: #f56c6c"></i> <!-- 全部分组图标 -->
          <i v-else class="el-icon-folder" style="color: #1aa4ff"></i> <!-- 常规分组图标 -->
          {{ scope.row.groupName }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              <i class="el-icon-more"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-edit" @click.native.prevent="handleUpdate(scope.row)">
                修改
              </el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete" @click.native.prevent="handleDelete(scope.row)">
                删除
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    </div>
    <!-- 添加或修改企业微信素材分组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分组名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入分组名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContactWayGroup, getContactWayGroup, delContactWayGroup, addContactWayGroup, updateContactWayGroup, exportContactWayGroup } from "@/api/qw/contactWayGroup";

export default {
  name: "contactWayGroup",
  data() {
    return {
      // 遮罩层
      loading: true,
      //列表选择行数
      currentRowId:null,
      //绑定行
      rowIndex:null,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业微信素材分组表格数据
      materialGroupList: [
        { materialGroupName: '全部分组', special: true },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: null,
        groupName: null,
        companyId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        groupName: [
          { required: true, message: "分组名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业微信素材分组列表 */
    getList() {
      this.loading = true;
      listContactWayGroup(this.queryParams).then(response => {
        this.materialGroupList = response.rows;

        this.$emit('getGroupList', response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 行数重置
    resetRow(){
      this.currentRowId=null,
        //绑定行
      this.rowIndex=null;
    },
    //绑定行
    rowStyle ({row, rowIndex}) {
      if (this.currentRowId === row.groupId) {
        this.rowIndex=rowIndex;
      }
    },

    //选定行的风格样式
    setSellStyle({ row, column, rowIndex, columnIndex }) {
      if(this.rowIndex==rowIndex){
        return "background:  #3facff;#3facff;border: none;color: rgb(255, 255, 255)"
      }
      return "border: none;"
    },

    /** 单点某一行 */
    changeCurrentRow(row){

      this.$emit('selectGroupScreen', row.id);
      if (this.currentRowId === row.id){
        return
      }else{
        this.currentRowId = row.id
      }
    },
    // 表单重置
    reset() {
      this.form = {
        groupId: null,
        groupName: null,
        companyId: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log("多选框选中数据",selection)
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业微信素材分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContactWayGroup(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业微信素材分组";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContactWayGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContactWayGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm('是否确认删除企业微信素材分组编号为"' + id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delContactWayGroup(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业微信素材分组数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportContactWayGroup(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
