<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公司名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示状态" prop="liveShow">
        <el-select
          v-model="queryParams.liveShow"
          placeholder="请选择显示状态"
          clearable
          size="small"
          style="width: 150px"
        >
          <el-option label="展示" :value="1"></el-option>
          <el-option label="不展示" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="3">
        <el-select
          v-model="batchLiveShow"
          placeholder="批量操作"
          size="mini"
          style="width: 120px"
          :disabled="selectedCompanyIds.length === 0"
          @change="handleBatchUpdateLiveShow"
        >
          <el-option label="批量展示" :value="1"></el-option>
          <el-option label="批量不展示" :value="0"></el-option>
        </el-select>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column label="公司ID" align="center" prop="companyId" width="100" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
      <el-table-column label="显示状态" align="center" prop="liveShow" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.liveShow == 1" type="success">显示</el-tag>
          <el-tag v-else type="info">不显示</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEditLiveShow(scope.row)"
          >修改显示状态</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 修改显示状态对话框 -->
    <el-dialog title="修改显示状态" :visible.sync="editLiveShowDialog" width="500px" append-to-body>
      <el-form :model="editLiveShowForm" label-width="100px">
        <el-form-item label="公司名称">
          <el-input v-model="editLiveShowForm.companyName" :disabled="true" />
        </el-form-item>
        <el-form-item label="显示状态" prop="liveShow">
          <el-select v-model="editLiveShowForm.liveShow" placeholder="请选择显示状态" style="width: 100%">
            <el-option label="不展示" :value="0"></el-option>
            <el-option label="展示" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEditLiveShow">确 定</el-button>
        <el-button @click="cancelEditLiveShow">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { liveShowList, batchUpdateCompanyLiveShow } from "@/api/company/company";

export default {
  name: "LiveMiniLives",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 公司列表加载（对话框）
      companyLoading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司列表总条数（对话框）
      companyTotal: 0,
      // 公司列表
      companyList: [],
      // 对话框中的公司列表
      dialogCompanyList: [],
      // 选中的公司
      selectedCompanies: [],
      // 是否显示弹出层
      open: false,
      // 是否显示修改显示状态对话框
      editLiveShowDialog: false,
      // 修改显示状态表单
      editLiveShowForm: {
        companyId: null,
        companyName: '',
        liveShow: 0
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        liveShow: null
      },
      // 公司查询参数（对话框）
      queryCompanyParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null
      },
      // 批量操作选中的公司
      selectedCompanyIds: [],
      // 批量操作显示状态
      batchLiveShow: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司列表 */
    getList() {
      this.loading = true;
      liveShowList(this.queryParams).then(response => {
        if (response.code === 200) {
          this.companyList = response.rows || [];
          this.total = response.total || 0;
        } else {
          this.companyList = [];
          this.total = 0;
        }
        this.loading = false;
      }).catch(() => {
        this.companyList = [];
        this.total = 0;
        this.loading = false;
      });
    },
    /** 查询公司列表（对话框） */
    getCompanyList() {
      this.companyLoading = true;
      liveShowList(this.queryCompanyParams).then(response => {
        if (response.code === 200) {
          this.dialogCompanyList = response.rows || [];
          this.companyTotal = response.total || 0;
        } else {
          this.dialogCompanyList = [];
          this.companyTotal = 0;
        }
        this.companyLoading = false;
      }).catch(() => {
        this.dialogCompanyList = [];
        this.companyTotal = 0;
        this.companyLoading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.companyName = null;
      this.queryParams.liveShow = null;
      this.handleQuery();
    },
    /** 公司搜索按钮操作 */
    handleCompanyQuery() {
      this.queryCompanyParams.pageNum = 1;
      this.getCompanyList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      // 记录所有选中的公司ID（用于批量操作）
      this.selectedCompanyIds = selection.map(item => item.companyId);
      // 只记录已绑定（liveShow == 1）的公司ID（用于批量删除）
      this.ids = selection.filter(item => item.liveShow == 1).map(item => item.companyId);
      // 只有选中了已绑定的公司才能批量删除
      this.multiple = this.ids.length === 0;
    },

    /** 绑定公司按钮操作 */
    handleAdd() {
      this.open = true;
      this.selectedCompanies = [];
      this.queryCompanyParams = {
        pageNum: 1,
        pageSize: 10,
        companyName: null
      };
      this.getCompanyList();
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.selectedCompanies = [];
    },
    /** 检查公司是否已绑定 */
    isCompanyBound(company) {
      return company.liveShow == 1;
    },
    /** 检查行是否可选（对话框表格） */
    checkSelectable(row) {
      // 已绑定的公司（liveShow == 1）不可选
      return row.liveShow != 1;
    },
    /** 打开修改显示状态对话框 */
    handleEditLiveShow(row) {
      this.editLiveShowForm = {
        companyId: row.companyId,
        companyName: row.companyName,
        liveShow: row.liveShow || 0
      };
      this.editLiveShowDialog = true;
    },
    /** 取消修改显示状态 */
    cancelEditLiveShow() {
      this.editLiveShowDialog = false;
      this.editLiveShowForm = {
        companyId: null,
        companyName: '',
        liveShow: 0
      };
    },
    /** 提交修改显示状态 */
    submitEditLiveShow() {
      if (this.editLiveShowForm.liveShow === null || this.editLiveShowForm.liveShow === undefined) {
        this.msgWarning("请选择显示状态");
        return;
      }
      const selectIds=[]
      selectIds.push(this.editLiveShowForm.companyId)

      const data = {
        ids: selectIds,
        liveShow: this.editLiveShowForm.liveShow
      };

      batchUpdateCompanyLiveShow(data).then(response => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
          this.editLiveShowDialog = false;
          this.getList();
        } else {
          this.msgError(response.msg || "修改失败");
        }
      }).catch(() => {
        this.msgError("修改失败");
      });
    },
    /** 批量修改显示状态 */
    handleBatchUpdateLiveShow(value) {
      if (value === null || value === undefined) {
        return;
      }

      if (!this.selectedCompanyIds || this.selectedCompanyIds.length === 0) {
        this.msgWarning("请先选择要操作的公司");
        this.batchLiveShow = null;
        return;
      }

      const statusText = value == 1 ? "展示" : "不展示";
      this.$confirm('是否确认将所选' + this.selectedCompanyIds.length + '个公司设置为"' + statusText + '"?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const data = {
          ids: this.selectedCompanyIds,
          liveShow: value
        };
        batchUpdateCompanyLiveShow(data).then(response => {
          if (response.code === 200) {
            this.msgSuccess("批量修改成功");
            this.batchLiveShow = null;
            this.getList();
          } else {
            this.msgError(response.msg || "批量修改失败");
            this.batchLiveShow = null;
          }
        }).catch(() => {
          this.msgError("批量修改失败");
          this.batchLiveShow = null;
        });
      }).catch(() => {
        this.batchLiveShow = null;
      });
    },
  }
};
</script>

<style scoped>
::v-deep .bound-row {
  background-color: #f0f9ff;
  color: #909399;
}
</style>

