<template>
  <div>
    <div style="padding: 10px">
    <div style="border-bottom: 1px solid #e6e6e6;background-color: white; display: flex;justify-content: left;">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px" style="padding-top: 10px">
      <el-form-item label="企微公司" prop="corpId">
          <el-select v-model="queryParams.corpId" placeholder="企微公司"  size="small" @change="updateCorpId()">
            <el-option
              v-for="dict in myQwCompanyList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="发送次数" prop="sendCount">-->
<!--        <el-input-->
<!--          v-model="queryParams.sendCount"-->
<!--          placeholder="请输入发送次数"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置/刷新页面/回到全部</el-button>
        <el-button type="primary"  size="mini" @click="handleAdd()" v-hasPermi="['qw:material:add']">添加素材</el-button>
      </el-form-item>
    </el-form>

    </div>

    <div style="margin-top: 10px;height: 700px;width: auto;border: 1px solid #e6e6e6; display: flex;">
      <div style="background-color: white;height: 100%;width: 16%; overflow-y: auto; ">
          <div style="padding: 10px;">
            <MaterialGroup ref="MaterialGroup" @selectGroupScreen="selectGroupScreen" @refresh-index="refreshIndex"></MaterialGroup>
          </div>
      </div>
      <div style="height: 100%;width: 1%">

      </div>
      <div style="background-color: white;height: 100%;width: 83%;">
        <div style="padding: 20px; height: calc(100% - 40px); overflow-y: auto;">
          <el-tabs v-model="activeName" @tab-click="handleClick" style="height: 100%" >
            <el-tab-pane label="所有" name="1">
              <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange" border>
                <el-table-column label="素材内容" align="center" >
                  <template slot-scope="scope">
                    <div v-if="scope.row.materialType=='text'">
                      <el-tooltip class="item" effect="dark" :content="scope.row.textContent" placement="top">
                        <div style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; text-overflow: ellipsis;">
                          <span>{{ scope.row.textContent }}</span>
                        </div>
                      </el-tooltip>
                    </div>
                    <el-image
                      v-else-if="scope.row.materialType=='image'"
                      style="width: 100px; height: 100px"
                      :src="scope.row.materialUrl"
                      fit="contain"
                      @click="openImageViewer(scope.row.materialUrl)"/>
                    <div v-else-if="scope.row.materialType=='imagetext'">
                      <el-tooltip class="item" effect="dark" :content="scope.row.textContent" placement="top">
                        <div style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; text-overflow: ellipsis;">
                          <span>{{ scope.row.textContent }}</span>
                          <el-link :href="scope.row.materialUrl" target="_blank">{{ scope.row.materialUrl }}</el-link>
                        </div>
                      </el-tooltip>

                      <div v-if="scope.row.titleUrl">
                        <el-image style="width: 100px; height: 100px"  :src="scope.row.titleUrl"
                                  fit="contain" @click="openImageViewer(scope.row.titleUrl)" />
                      </div>

                    </div>
                    <div v-else-if="scope.row.materialType == 'voice'"  style="width: 200px">
                      <audio  controls :src="scope.row.materialUrl"/>
                    </div>
                    <video
                      v-else-if="scope.row.materialType == 'video'"
                      :src="scope.row.materialUrl"
                      controls style="width: 200px;height: 100px">
                    </video>
                    <div v-else-if="scope.row.materialType == 'file'" class="file-link-container" style="width: 100%">
                      <el-link type="primary" :href="downloadUrl(scope.row.materialUrl)" download>
                         {{scope.row.materialUrl}}
                      </el-link>
                    </div>
                  </template>
                </el-table-column>
                <el-table-column label="标题" align="center"  prop="title"/>
                <el-table-column label="创建人" align="center"  prop="createName"/>
<!--                <el-table-column label="发送次数" align="center"  prop="sendCount"/>-->
                <el-table-column label="隶属分组" align="center"  prop="materialGroupName"></el-table-column>
                <el-table-column label="创建时间" align="center"  prop="createTime"/>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['qw:material:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['qw:material:remove']"
                    >删除</el-button>
                  </template>
                </el-table-column>
              </el-table>

            </el-tab-pane>
            <el-tab-pane label="文本" name="2">
                <el-table v-loading="loading" :data="this.textMaterialsList" @selection-change="handleSelectionChange">
                  <el-table-column label="素材内容" align="center" prop="textContent">
                    <template  slot-scope="scope">
                      <el-tooltip class="item" effect="dark" :content="scope.row.textContent" placement="top">
                        <div style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; text-overflow: ellipsis;">
                          <span>文本消息：</span>{{ scope.row.textContent }}
                        </div>
                      </el-tooltip>
                    </template>
                  </el-table-column>
                  <el-table-column label="标题" align="center"  prop="title"/>
                  <el-table-column label="创建人" align="center"  prop="createName"/>
<!--                  <el-table-column label="发送次数" align="center"  prop="sendCount"/>-->
                  <el-table-column label="隶属分组" align="center"  prop="materialGroupName"></el-table-column>
                  <el-table-column label="创建时间" align="center"  prop="createTime"/>
                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        v-hasPermi="['qw:material:edit']"
                      >修改</el-button>
                      <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['qw:material:remove']"
                      >删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
            </el-tab-pane>
            <el-tab-pane label="图片" name="3">
                <div style="display: flex;  flex-wrap: wrap; gap: 10px; ">
                  <el-card
                    v-for="(item, index) in imageMaterialsList"
                    :key="index">
<!--                    style="width: 250px;height: 300px">-->
<!--                    <div class="card-header">-->
<!--                      <el-checkbox-->
<!--                        :label="item.id"-->
<!--                        @change="handleSelectionChange($event, item)"-->
<!--                      ></el-checkbox>-->
<!--                    </div>-->
                    <el-image
                      style="width: 200px; height: 180px"
                      :src="item.materialUrl"
                      fit="contain"
                      @click="openImageViewer(item.materialUrl)"/>
                    <div>
                      <div style="  display: flex;flex-direction: column;">
                        <span>标题:{{ item.title }}</span>
                        <span>创建人:{{item.createName}}</span>
                        <time class="time" style="white-space: nowrap">时间:{{ item.createTime }}</time>
                      </div>
                      <div class="bottom clearfix">
                        <el-button
                          class="button"
                          size="mini"
                          type="text"
                          icon="el-icon-edit"
                          @click="handleUpdate(item)"
                          v-hasPermi="['qw:material:edit']"
                        >修改</el-button>
                        <el-button
                          class="button"
                          size="mini"
                          type="text"
                          icon="el-icon-delete"
                          @click="handleDelete(item)"
                          v-hasPermi="['qw:material:remove']"
                        >删除</el-button>
                      </div>
                    </div>
                  </el-card>
                </div>
            </el-tab-pane>

<!--            <el-tab-pane label="图文" name="4">-->
<!--              <div style="display: flex;  flex-wrap: wrap; gap: 10px; ">-->
<!--                <el-card-->
<!--                  v-for="(item, index) in imagetextMaterialsList"-->
<!--                  :key="index">-->
<!--                  <el-image-->
<!--                    v-if="item.titleUrl"-->
<!--                    style="width: 200px; height: 180px"-->
<!--                    :src="item.titleUrl"-->
<!--                    fit="contain"-->
<!--                    @click="openImageViewer(item.titleUrl)"-->
<!--                  />-->
<!--                  <div v-else style="width: 200px; height: 180px; display: flex; justify-content: center; align-items: center; border: 1px solid #dcdcdc;">-->
<!--                    暂无标题图片-->
<!--                  </div>-->
<!--                  <div style="margin-top: 3%">-->
<!--                    <div style="  display: flex;flex-direction: column;width: 200px">-->
<!--                      <span>标题:{{ item.title }}</span>-->
<!--                      <div style="display: flex; flex-wrap: wrap; word-break: break-all;margin:3% 0">-->
<!--                        <span>链接:</span>-->
<!--                        <el-link :href="item.materialUrl" target="_blank">{{ item.materialUrl }}</el-link>-->
<!--                      </div>-->
<!--                      <span>创建人:{{item.createName}}</span>-->
<!--                      <time class="time" style="white-space: nowrap">时间:{{ item.createTime }}</time>-->
<!--                    </div>-->
<!--                    <div class="bottom clearfix">-->
<!--                      <el-button-->
<!--                        class="button"-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-edit"-->
<!--                        @click="handleUpdate(item)"-->
<!--                        v-hasPermi="['qw:material:edit']"-->
<!--                      >修改</el-button>-->
<!--                      <el-button-->
<!--                        class="button"-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-delete"-->
<!--                        @click="handleDelete(item)"-->
<!--                        v-hasPermi="['qw:material:remove']"-->
<!--                      >删除</el-button>-->
<!--                    </div>-->
<!--                  </div>-->
<!--                </el-card>-->
<!--              </div>-->
<!--            </el-tab-pane>-->
<!--            <el-tab-pane label="语音" name="5">-->
<!--              <div style="display: flex;  flex-wrap: wrap; gap: 10px; ">-->
<!--                <el-card-->
<!--                  v-for="(item, index) in voiceMaterialsList"-->
<!--                  :key="index">-->
<!--                  &lt;!&ndash;                    style="width: 250px;height: 300px">&ndash;&gt;-->
<!--                  &lt;!&ndash;                    <div class="card-header">&ndash;&gt;-->
<!--                  &lt;!&ndash;                      <el-checkbox&ndash;&gt;-->
<!--                  &lt;!&ndash;                        :label="item.id"&ndash;&gt;-->
<!--                  &lt;!&ndash;                        @change="handleSelectionChange($event, item)"&ndash;&gt;-->
<!--                  &lt;!&ndash;                      ></el-checkbox>&ndash;&gt;-->
<!--                  &lt;!&ndash;                    </div>&ndash;&gt;-->
<!--                  <audio  controls :src="item.materialUrl" style="width: 300px;height: 50px"/>-->
<!--                  <div>-->
<!--                    <div style="  display: flex;flex-direction: column;width: 200px">-->
<!--                      <span>标题:{{ item.title }}</span>-->
<!--                      <span>创建人:{{item.createName}}</span>-->
<!--                      <time class="time" style="white-space: nowrap">时间:{{ item.createTime }}</time>-->
<!--                    </div>-->
<!--                    <div class="bottom clearfix">-->
<!--                      <el-button-->
<!--                        class="button"-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-edit"-->
<!--                        @click="handleUpdate(item)"-->
<!--                        v-hasPermi="['qw:material:edit']"-->
<!--                      >修改</el-button>-->
<!--                      <el-button-->
<!--                        class="button"-->
<!--                        size="mini"-->
<!--                        type="text"-->
<!--                        icon="el-icon-delete"-->
<!--                        @click="handleDelete(item)"-->
<!--                        v-hasPermi="['qw:material:remove']"-->
<!--                      >删除</el-button>-->
<!--                    </div>-->
<!--                  </div>-->
<!--                </el-card>-->
<!--              </div>-->
<!--            </el-tab-pane>-->
            <el-tab-pane label="视频" name="6">
              <div style="display: flex;  flex-wrap: wrap; gap: 10px; ">
                <el-card
                  v-for="(item, index) in videoMaterialsList"
                  :key="index">
                  <!--                    style="width: 250px;height: 300px">-->
                  <!--                    <div class="card-header">-->
                  <!--                      <el-checkbox-->
                  <!--                        :label="item.id"-->
                  <!--                        @change="handleSelectionChange($event, item)"-->
                  <!--                      ></el-checkbox>-->
                  <!--                    </div>-->
                  <video
                    :src="item.materialUrl"
                    controls style="width: 200px;height: 100px">
                  </video>
                  <div>
                    <div style="  display: flex;flex-direction: column;width: 200px">
                      <span>标题:{{ item.title }}</span>
                      <span>创建人:{{item.createName}}</span>
                      <time class="time" style="white-space: nowrap">时间:{{ item.createTime }}</time>
                    </div>
                    <div class="bottom clearfix">
                      <el-button
                        class="button"
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(item)"
                        v-hasPermi="['qw:material:edit']"
                      >修改</el-button>
                      <el-button
                        class="button"
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(item)"
                        v-hasPermi="['qw:material:remove']"
                      >删除</el-button>
                    </div>
                  </div>
                </el-card>
              </div>
            </el-tab-pane>
            <el-tab-pane label="文件" name="7">
              <div style="display: flex;  flex-wrap: wrap; gap: 10px; ">
                <el-card
                  v-for="(item, index) in fileMaterialsList"
                  :key="index">
                  <!--                    style="width: 250px;height: 300px">-->
                  <!--                    <div class="card-header">-->
                  <!--                      <el-checkbox-->
                  <!--                        :label="item.id"-->
                  <!--                        @change="handleSelectionChange($event, item)"-->
                  <!--                      ></el-checkbox>-->
                  <!--                    </div>-->
                  <el-link type="primary" :href="downloadUrl(item.materialUrl)" download>
                    {{item.materialUrl}}
                  </el-link>
                  <div>
                    <div style="  display: flex;flex-direction: column;width: 200px">
                      <span>标题:{{ item.title }}</span>
                      <span>创建人:{{item.createName}}</span>
                      <time class="time" style="white-space: nowrap">时间:{{ item.createTime }}</time>
                    </div>
                    <div class="bottom clearfix">
                      <el-button
                        class="button"
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(item)"
                        v-hasPermi="['qw:material:edit']"
                      >修改</el-button>
                      <el-button
                        class="button"
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(item)"
                        v-hasPermi="['qw:material:remove']"
                      >删除</el-button>
                    </div>
                  </div>
                </el-card>
              </div>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </div>

    <!-- 大图预览对话框 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :modal="false"
      width="800"
      append-to-body>
      <img
        :src="this.dialogImageUrl"
        style="display: block; max-width: 100%; margin: 0 auto"
      />
    </el-dialog>

    <!-- 添加或修改素材对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="分组名称" prop="materialGroupId">
          <el-select v-model="form.materialGroupId" filterable  placeholder="请选择分组">
            <el-option
              v-for="item in meaterialGroupOptions"
              :key="item.materialGroupId"
              :label="item.materialGroupName"
              :value="item.materialGroupId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <div v-if="!form.materialId">
        <el-form-item label="素材类型" prop="materialType">
          <el-radio-group v-model="form.materialType">
            <el-radio :label="item.dictValue" v-for="item in materialTypeOptions" >{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        </div>
        <div v-if="form.materialType=='text'">
          <el-form-item label="文本内容" prop="textContent" required>
            <el-input v-model="form.textContent"  type="textarea" :rows="3" maxlength="1300" show-word-limit placeholder="请输入文本消息" required></el-input>
          </el-form-item>
        </div>
        <div v-else-if="form.materialType=='image'">
          <el-form-item label="上传图片" prop="materialUrl">
            <ImageUpload v-model="form.materialUrl" type="image" :num="1" :width="150" :height="150" />
          </el-form-item>
        </div>
        <div v-else-if="form.materialType=='imagetext'">
          <el-form-item label="上传标题图片" prop="textUrl">
            <ImageUpload v-model="form.titleUrl" type="image" :num="10" :width="150" :height="150" />
          </el-form-item>
          <el-form-item label="文本内容" prop="textContent">
            <el-input v-model="form.textContent"  type="textarea" :rows="3" maxlength="1300" show-word-limit placeholder="请输入文本消息" ></el-input>
          </el-form-item>
          <el-form-item label="图文的URL地址" prop="materialUrl" required>
<!--            <ImageUpload v-model="form.materialUrl" type="image" :num="10" :width="150" :height="150" />-->
            <el-input v-model="form.materialUrl"  placeholder="请输入图文链接地址" />
          </el-form-item>
        </div>
        <div v-else-if="form.materialType=='voice'">
          <el-form-item label="上传音频" prop="materialUrl">
            <el-upload v-if="form.materialUrl==null || form.materialId"
              v-model="form.materialUrl"
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUploadVoice">
               <i class="el-icon-plus avatar-uploader-icon"></i>
               </el-upload>
            <audio  v-if="form.materialUrl" controls :src="form.materialUrl" style="width: 300px;height: 50px"/>
          </el-form-item>
        </div>
        <div v-else-if="form.materialType=='video'">
          <el-form-item label="上传视频" prop="materialUrl">
            <el-upload v-if="form.materialUrl==null || form.materialId"
              v-model="form.materialUrl"
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUploadVideo">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <video v-if="form.materialUrl"
              :src="form.materialUrl"
              controls style="width: 300px;height: 200px">
            </video>
          </el-form-item>
        </div>
        <div v-else-if="form.materialType=='file'">
          <el-form-item label="上传文件" prop="materialUrl">
            <el-upload v-if="form.materialUrl==null || form.materialId"
              v-model="form.materialUrl"
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUploadFile">
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <el-link v-if="form.materialUrl" type="primary" :href="downloadUrl(form.materialUrl)" download>
              {{ form.materialUrl }}
            </el-link>
          </el-form-item>
        </div>
      </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 { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial, exportMaterial } from "@/api/qw/material";
import MaterialGroup from '@/views/qw/materialGroup/materialGroup'
import ImageUpload from "@/views/qw/material/ImageUpload";
import { getMyQwUserList,getMyQwCompanyList } from "@/api/qw/user";
export default {
  name: "Material",
  components: { MaterialGroup,ImageUpload},
  data() {
    return {
      //图片放大
      dialogVisible: false,
      dialogImageUrl:null,
      //上传地址
      uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS2",
      myQwCompanyList:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 素材库表格数据
      materialList: [],
      textMaterialsList:[],
      imageMaterialsList:[],
      imagetextMaterialsList:[],
      voiceMaterialsList:[],
      videoMaterialsList:[],
      fileMaterialsList:[],
      defaultMaterialList:[],

      //素材类型
      materialTypeOptions:[],

      //素材组
      meaterialGroupOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        materialType: null,
        materialMediaId: null,
        createdAt: null,
        materialUrl: null,
        materialGroupId: null,
        companyId: null,
        corpId: null,
        textContent: null,
        title: null,
        createName: null,
        sendCount: null,
        groupType:"1",
      },

      //副框副列选择
      activeName:"1",

      //分组弹出
      group:{
        open:false,
        title:"",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        materialGroupId: [
          { required: true, message: "分组名称不能为空", trigger: "blur" }
        ],
        createName: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        materialUrl:[
          {required: true, message: "值不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    getMyQwCompanyList().then(response => {
            this.myQwCompanyList = response.data;
            if(this.myQwCompanyList!=null){
              this.queryParams.corpId=this.myQwCompanyList[0].dictValue
              this.getList();
            }
    });
    //素材类型字典
    this.getDicts("sys_qw_material_type").then(response => {
      this.materialTypeOptions = response.data;
    });

  },

  methods: {
    updateCorpId(){
           this.getList();
     },
    /** 查询素材库列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {

        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.textMaterialsList= this.materialList.filter(item => item.materialType === 'text');
        this.imageMaterialsList= this.materialList.filter(item => item.materialType === 'image');
        this.imagetextMaterialsList= this.materialList.filter(item => item.materialType === 'imagetext');
        this.voiceMaterialsList= this.materialList.filter(item => item.materialType === 'voice');
        this.videoMaterialsList= this.materialList.filter(item => item.materialType === 'video');
        this.fileMaterialsList= this.materialList.filter(item => item.materialType === 'file');
      });
    },

    textMaterials() {
      this.queryParams.materialType = 'text';
      this.getList();
    },

    imageMaterials() {
      this.queryParams.materialType='image';
      this.getList();
    },
    imagetextMaterials() {
      this.queryParams.materialType='imagetext';
      this.getList();
    },
    voiceMaterials() {
      this.queryParams.materialType='voice';
      this.getList();
    },
    videoMaterials() {
      this.queryParams.materialType='video';
      this.getList();
    },
    fileMaterials() {
      this.queryParams.materialType='file';
      this.getList();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //下载文件
    downloadUrl(materialUrl) {
      // 直接返回文件 URL
      return materialUrl;
    },

    handleAvatarSuccess(res, file) {
      if(res.code==200){
        this.form.materialUrl=res.url;
        // this.self.$forceUpdate()
      }
      else{
        this.msgError(res.msg);
      }
    },
    beforeAvatarUploadVoice(file){
      const isLt1M = file.size / 1024 / 1024 < 2;
      if (!isLt1M) {
        this.$message.error('上传大小不能超过 2MB!');
      }
      return isLt1M;
    },
    beforeAvatarUploadVideo(file){
      const isLt1M = file.size / 1024 / 1024 < 10;
      if (!isLt1M) {
        this.$message.error('上传大小不能超过 10MB!');
      }
      return isLt1M;
    },

    beforeAvatarUploadFile(file){
      const isLt1M = file.size / 1024 / 1024 < 20;
      if (!isLt1M) {
        this.$message.error('上传大小不能超过 20MB!');
      }
      return isLt1M;
    },

    //选择小板块方法
    handleClick(row){

      switch (row.name) {
        case '1':
          this.resetParam();
          this.getList();
          break;
        case '2':
          this.textMaterials();
          break;
        case '3':
          this.imageMaterials();
          break;
        case '4':
          this.imagetextMaterials()
          break;
        case '5':
          this.voiceMaterials();
          break;
        case '6':
          this.videoMaterials()
          break;
        case '7':
          this.fileMaterials()
          break;
        default:
          this.resetParam();
          this.getList();
          break;
      }
    },

    //选择的分组
    selectGroupScreen(row){
      this.queryParams.materialGroupId=row
      this.getList();
    },

    //删除分组后刷新页面
    refreshIndex(){
      this.resetParam();
      this.reset();
      this.getList();
    },
    openImageViewer(url) {
      // 打开大图预览对话框
      this.dialogImageUrl=url
      this.dialogVisible = true;
    },

    // 表单重置      console.log(this.deptOptions)
    reset() {
      this.form = {
        materialId: null,
        materialType: null,
        materialMediaId: null,
        createdAt: null,
        materialUrl: null,
        materialGroupId: null,
        companyId: null,
        corpId: null,
        textContent: null,
        createTime: null,
        title: null,
        titleUrl: null,
        updateTime: null,
        createName: null,
        sendCount: null,
        groupType:"1",
      };
      this.resetForm("form");
    },

    /** 参数重置 */
    resetParam(){
      this.queryParams={
        pageNum: 1,
          pageSize: 8,
          materialType: null,
          materialMediaId: null,
          createdAt: null,
          materialUrl: null,
          materialGroupId: null,
          companyId: null,
          corpId: null,
          textContent: null,
          title: null,
          createName: null,
          groupType:"1",
      };
    },

    /** 素材组选择 */
    materialGroupRow(){},
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset();
      this.resetParam();
      this.resetForm("queryForm");
      this.queryParams.corpId= this.myQwCompanyList[0].dictValue
      this.handleQuery();
      this.$refs.MaterialGroup.resetRow();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log("选中的数据",selection)
      this.ids = selection.map(item => item.materialId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加素材库";
      this.form.materialType='text'
      this.meaterialGroupOptions=this.$refs.MaterialGroup.materialGroupList;
    },

    /** 新增分组操作 */
    handleAddGroup() {
      this.reset();
      this.group.open = true;
      this.group.title = "添加企业微信素材分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materialId = row.materialId || this.ids
      getMaterial(materialId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改素材库";
        this.meaterialGroupOptions=this.$refs.MaterialGroup.materialGroupList;

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.corpId=this.queryParams.corpId;
        if (valid) {
          if (this.form.materialId != null) {

            this.open = false;
            let loadingRock = this.$loading({
              lock: true,
              text: '正在执行中请稍后~！',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });

            updateMaterial(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.getList();
            }).finally(res=>{
              loadingRock.close();
            });

          } else {

            this.open = false;
            let loadingRock = this.$loading({
              lock: true,
              text: '正在执行中请稍后~~',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            });

            addMaterial(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.getList();
              loadingRock.close();
            }).finally(res=>{
              loadingRock.close();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids;
      this.$confirm('是否确认删除素材库编号为"' + materialIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMaterial(materialIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有素材库数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMaterial(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.file-link-container {
  display: flex;          /* 使用 Flexbox */
  justify-content: center; /* 水平居中 */
  align-items: center;     /* 垂直居中 */
  height: 100px;
  width: 50px;
}
.text-container {
  max-height: 7.5em; /* 设置最大高度为6行，根据字体大小调整 */
  overflow-y: auto; /* 内容超出时显示滚动条 */
  line-height: 1.5em; /* 行高设置，确保每行高度一致 */
}
.custom-span-title {
  display: block; /* 确保元素是块级元素 */
  width: 500px;
  height: 45px; /* 设置固定高度 */
  overflow-y: auto; /* 超出高度时显示滚动条 */
  word-wrap: break-word; /* 自动换行 */
  word-break: break-all; /* 在必要时进行换行 */
}
</style>
