<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="materialGroupName">
        <el-input
          v-model="queryParams.materialGroupName"
          placeholder="请输入分组名称"
          clearable
          size="small"
          v-on:input="handleQuery"
        />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['qw:materialGroup:add']"
        >新增分组</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div>
    <el-table v-loading="loading" :data="materialGroupList"
              @row-click="changeCurrentRow"
              :row-style="rowStyle"
              :cell-style="setSellStyle" >
      <!-- 保留的列 -->
      <el-table-column label="组名" align="center" prop="materialGroupName">
        <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.materialGroupName }}
<!--          <i class="el-icon-folder" style="color: #1aa4ff"></i>-->
<!--          {{ scope.row.materialGroupName }} &lt;!&ndash; 后面跟着组名 &ndash;&gt;-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <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="materialGroupName">
          <el-input v-model="form.materialGroupName" 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 { listMaterialGroup, getMaterialGroup, delMaterialGroup, addMaterialGroup, updateMaterialGroup, exportMaterialGroup } from "@/api/qw/materialGroup";

export default {
  name: "MaterialGroup",
  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: 100,
        materialGroupId: null,
        materialGroupName: null,
        companyId: null,
        corpId: null,
        groupType:"1"
      },
      // 表单参数
      form: {
        groupType:"1",
      },
      // 表单校验
      rules: {
        materialGroupName: [
          { required: true, message: "分组名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业微信素材分组列表 */
    getList() {
      this.loading = true;
      listMaterialGroup(this.queryParams).then(response => {
        this.materialGroupList = 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.materialGroupId) {
        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.materialGroupId);
      if (this.currentRowId === row.materialGroupId){
        return
      }else{
        this.currentRowId = row.materialGroupId
      }
    },
    // 表单重置
    reset() {
      this.form = {
        materialGroupId: null,
        materialGroupName: null,
        companyId: null,
        corpId: null,
        createTime: null,
        updateTime: null,
        groupType:"1"
      };
      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.materialGroupId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业微信素材分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialGroupId = row.materialGroupId || this.ids
      getMaterialGroup(materialGroupId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改企业微信素材分组";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialGroupId != null) {
            updateMaterialGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterialGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialGroupIds = row.materialGroupId || this.ids;
      this.$confirm('是否确认删除企业微信素材分组编号为"' + materialGroupIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMaterialGroup(materialGroupIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
          this.$emit('refresh-index');
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业微信素材分组数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMaterialGroup(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
