<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="上架状态" prop="goodsName">
        <el-select v-model="queryParams.publishStatus" style="width: 200px;" @change="handleQuery" clearable
                   placeholder="请选择上架状态">
          <el-option style="text-align: center;" v-for="item in options" :key="item.value" :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下架状态" prop="goodsName">
        <el-select v-model="queryParams.deleteStatus" style="width: 200px;" @change="handleQuery" clearable
                   placeholder="请选择下架状态">
          <el-option style="text-align: center;" v-for="item in options2" :key="item.value"
                     :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="更新状态" prop="goodsName">
        <el-select v-model="queryParams.updateStatus" style="width: 200px;" @change="handleQuery" clearable
                   placeholder="请选择更新状态">
          <el-option style="text-align: center;" v-for="item in options3" :key="item.value"
                     :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="合作方" prop="gamePlatformMapping">
        <el-select v-model="queryParams.partnerName" placeholder="请选择合作方" filterable clearable>
          <el-option v-for="dict in dict.type.notice_partner" :key="dict.value" :label="dict.label"
                     :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="店铺名称" prop="shopId">
                <el-select filterable clearable v-model="queryParams.shopId" style="width: 200px;" @change="handleQuery" clearable
                    placeholder="请选择店铺名称">
                    <el-option v-for="item in deptOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item> -->

      <el-form-item label="开始日期" prop="beginTime">
        <div class="block">
          <el-date-picker @change="handleQuery" v-model="queryParams.beginTime" value-format="yyyy-MM-dd"
                          type="date" placeholder="选择日期" style="width: 200px;">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="结束日期" prop="endTime">
        <div class="block">
          <el-date-picker @change="handleQuery" v-model="queryParams.endTime" type="date"
                          value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 200px;">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="游戏名称" prop="gameName">
        <el-select v-model="queryParams.gameName" @change="handleQuery" style="width: 200px" clearable
                   placeholder="请选择游戏名称">
          <el-option v-for="(item,index) in gameList" :key="item.id" :label="item.gameName"
                     :value="item.gameName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" style="width: 200px;" placeholder="请输入商品名称" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsId">
        <el-input v-model="queryParams.goodsId" style="width: 200px;" placeholder="请输入商品编号" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="游戏账号" prop="gameAccount">
        <el-input v-model="queryParams.gameAccount" style="width: 200px;" placeholder="请输入游戏账号" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery2">搜索</el-button>

        <el-button style="margin-left: 20px;" icon="el-icon-refresh" size="mini"
                   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-select v-model="dsqType" style="width: 200px;" @change="handleQuery" clearable
                 placeholder="请选择状态">
        <el-option style="text-align: center;" v-for="item in optionsStaus" :key="item.value"
                   :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <span v-if="ceshi">
				<text>通知上架</text>
				<el-radio v-model="radio" label="500">500</el-radio>
				<el-radio v-model="radio" label="1000">1000</el-radio>
				<el-radio v-model="radio" label="2000">2000</el-radio>
				<el-radio v-model="radio" label="3000">3000</el-radio>
				<el-radio v-model="radio" label="5000">5000</el-radio>
				<el-button @click="startUp()">开始</el-button>
			</span>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="tableData" :row-class-name="tableRowClassName" border style="width: 100%">

      <el-table-column prop="gameName" label="游戏名称">
      </el-table-column>
      <el-table-column prop="goodsId" label="商品编号">
      </el-table-column>
      <el-table-column prop="gameAccount" label="游戏账号">
      </el-table-column>
      <el-table-column prop="goodsName" show-overflow-tooltip height="20px" label="商品名称">
      </el-table-column>
      <el-table-column prop="price" label="商品价格">
      </el-table-column>
      <el-table-column prop="markupsPrice" label="通货价">
      </el-table-column>
      <el-table-column prop="expectedPrice" label="到手价(预计)">
      </el-table-column>
      <el-table-column label="合作方" align="center" prop="partnerName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.notice_partner" :value="scope.row.partnerName" />
        </template>
      </el-table-column>
      <el-table-column prop="publishStatus" label="上架状态">
        <template slot-scope="scope">
          <span v-if="scope.row.publishStatus=='Y'">成功</span>
          <el-tooltip v-if="scope.row.publishStatus=='X'" class="item" effect="dark"
                      :content="scope.row.publishData" placement="top-start">
            <span>失败</span>
          </el-tooltip>
          <span v-if="scope.row.publishStatus=='N'">未上架</span>
        </template>
      </el-table-column>
      <el-table-column prop="updateStatus" label="更新状态">
        <template slot-scope="scope">
          <span v-if="scope.row.updateStatus=='Y'">成功</span>
          <el-tooltip v-if="scope.row.updateStatus=='X'" class="item" effect="dark"
                      :content="scope.row.updateData" placement="top-start">
            <span>失败</span>
          </el-tooltip>
          <span v-if="scope.row.updateStatus=='N'">请稍后</span>
        </template>
      </el-table-column>
      <el-table-column prop="deleteStatus" label="下架状态">
        <template slot-scope="scope">
          <span v-if="scope.row.deleteStatus=='Y'">成功</span>
          <el-tooltip v-if="scope.row.deleteStatus=='X'" class="item" effect="dark"
                      :content="scope.row.deleteData" placement="top-start">
            <span>失败</span>
          </el-tooltip>
          <span v-if="scope.row.deleteStatus=='N'">未下架</span>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="上架时间">
        <!-- <template slot-scope="scope">
                    <span v-if="scope.row.deleteStatus!='N'">{{scope.row.createTime}}</span>
                </template> -->
      </el-table-column>
      <el-table-column prop="updateTime" label="下架时间">
        <template slot-scope="scope">
					<span
            v-if="scope.row.deleteStatus=='Y'||scope.row.deleteStatus=='X'">{{scope.row.updateTime}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button type="success" size="small" @click="handleSave(scope.row)">上架
          </el-button>
          <el-button type="primary" size="small" @click="handleUpdata(scope.row)">更新
          </el-button>
          <el-button type="danger" size="small" @click="handleDelete(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 v-if="open" :title="title" :visible.sync="open" append-to-body width="1000px">
      <div style="padding: 10px;overflow: auto">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>

            <el-col :span="12">
              <el-form-item label="游戏名称" prop="gameId">
                <el-select v-model="form.gameId" style="width: 100%" filterable placeholder="请选择游戏名称">
                  <el-option v-for="(item,index) in gameList" :key="item.id" :label="item.gameName"
                             :value="item.id"></el-option>
                </el-select>

              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="游戏账号" prop="accountId">
                <el-input v-model="form.accountId" placeholder="请输入游戏账号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证号" prop="idNo">
                <el-input v-model="form.idNo" placeholder="请输入身份证号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" placeholder="请输入姓名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="手机号" prop="mobile">
                <el-input v-model="form.mobile" placeholder="请输入手机号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="微信号" prop="wxNo">
                <el-input v-model="form.wxNo" placeholder="请输入微信号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
      </div>
      <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 {
    getpartnerlist,
    listpartner,
    updatepartnerlist,
    getInitGoodsManager,
    notice,
  } from "@/api/data/partnerlist";
  import {
    queryTBusShop,
    getTBusShopInfo
  } from "@/api/data/goodsPayment";
  export default {
    dicts: ['notice_partner'],

    data() {
      return {
        radio: '1000',
        disabled: true,
        block: false,
        gameList: [],
        dsqType:'',
        optionsStaus:[{
          value: 'S',
          label: '上架'
        }, {
          value: 'G',
          label: '更新'
        }, {
          value: 'X',
          label: '下架'
        }],
        options: [{
          value: 'Y',
          label: '成功'
        }, {
          value: 'X',
          label: '失败'
        }, {
          value: 'N',
          label: '未上架'
        }],
        options2: [{
          value: 'Y',
          label: '成功'
        }, {
          value: 'X',
          label: '失败'
        }, {
          value: 'N',
          label: '未下架'
        }],
        options3: [{
          value: 'Y',
          label: '成功'
        }, {
          value: 'X',
          label: '失败'
        }, {
          value: 'N',
          label: '请稍后'
        }],
        options1: [],
        deptOptions: [],
        tableData: [],
        // 遮罩层
        loading: true,
        // open:false

        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
        // 显示搜索条件
        showSearch: true,
        ceshi:false,
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          publishStatus: null,
          deleteStatus: null,
          updateStatus: null,
          goodsName: null,
          gameName: null,
          partnerName: null,
          beginTime: null,
          endTime: null
        },
        // 表单参数
        form: {
          publishStatus: null,
          deleteStatus: null,
          updateStatus: null,
          goodsName: null,
          gameName: null,
          partnerName: null,
        },
        queryForm: {
          publishStatus: null,
          deleteStatus: null,
          updateStatus: null,
          goodsName: null,
          gameName: null,
          partnerName: null,
        },
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: '请输入姓名',
            trigger: 'change'
          }],
          mobile: [{
            required: true,
            message: '请输入手机号',
            trigger: 'change'
          }],
          idNo: [{
            required: true,
            message: '请输入身份证号',
            trigger: 'change'
          }],
          gameId: [{
            required: true,
            message: '请输入游戏名称',
            trigger: 'change'
          }],
          wxNo: [{
            required: true,
            message: '请输入微信号',
            trigger: 'change'
          }],
          accountId: [{
            required: true,
            message: '请输入游戏账号',
            trigger: 'change'
          }],

        },
        currentIndex: 0,
      };
    },
    created() {
      this.getList();
      this.initGoodsManager();
    },
    methods: {
      startUp() {
        if (this.currentIndex < this.tableData.length) {
          const currentRes = this.tableData[this.currentIndex];
          if(this.dsqType=="S"){
            this.handleSave(currentRes);
          }else if(this.dsqType=="G"){
            this.handleUpdata(currentRes);
          }else if(this.dsqType=="X"){
            this.handleDelete(currentRes);
          }

          // 每次处理完一个元素后，更新索引并设置下一次处理的定时器
          this.currentIndex++;
          setTimeout(() => {
            this.startUp();
          }, Number(this.radio));
        } else {
          // 当数组所有元素都处理完后，重置索引，以便后续重新开始处理（如果需要）
          this.currentIndex = 0;
          this.startUp();
        }
      },
      tableRowClassName({
                          row,
                          rowIndex
                        }) {
        if (rowIndex === 1 || Number.isInteger((rowIndex - 1) / 4)) {
          return 'warning-row';
        } else if (rowIndex === 3 || Number.isInteger((rowIndex - 3) / 4)) {
          return 'success-row';
        }
        return '';
      },
      // 游戏类型
      initGoodsManager() {
        getTBusShopInfo().then(res => {
          if (res.msg) {

          } else { //admin/分销/买号
            this.ceshi = true
          }
        })
        getInitGoodsManager().then(res => {
          this.pictureServerPath = res.pictureServerPath
          this.gameList = res.gameList
        });
        queryTBusShop().then(response => {
          response.data.forEach((item) => {
            this.deptOptions.push({
              label: item.name,
              value: item.id,
            });
          });
        });
        return this.deptOptions;
      },

      /** 查询黑号信息列表 */
      getList() {
        this.loading = true;
        listpartner(this.queryParams).then(response => {
          // console.log(response)
          this.tableData = response.rows;
          this.total = response.total
          this.loading = false;
        });
      },
      //搜索按钮使用
      getList1() {
        this.loading = true;
        listpartner(this.queryParams).then(response => {
          // console.log(response)
          this.disabled = false;
          this.block = true;
          this.tableData = response.rows;
          this.total = response.total
          this.loading = false;
        });
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.disabled = true;
        this.block = false;
        this.queryParams.pageNum = 1;
        this.getList();
      },
      handleQuery1() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      handleQuery2() {
        this.disabled = true;
        this.block = false;
        this.queryParams.pageNum = 1;
        this.getList1();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.block = false;
        this.disabled = true;
        this.resetForm("queryForm");
        this.handleQuery1();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.names = selection.map(item => item.name)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      handleSave(e) {
        let query = {
          id: e.id,
          noticeType: "S"
        };
        notice(query).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess("已通知上架");
            this.getList();
          } else {
            alert(res.msg)
          }
        })
      },
      handleUpdata(e) {
        let query = {
          id: e.id,
          noticeType: "U"
        };
        notice(query).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess("已通知更新");
            this.getList();
          } else {
            alert(res.msg)
          }
        })
      },
      handleDelete(e) {
        let query = {
          id: e.id,
          noticeType: "D"
        };
        notice(query).then(res => {
          if (res.code == 200) {
            this.$modal.msgSuccess("已通知下架");
            this.getList();
          } else {
            alert(res.msg)
          }
        })
      },


      /** 修改按钮操作 */
      handleUpdate(row) {
        this.open = true;
        this.reset();
        const id = row.id || this.ids
        getpartnerlist(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) {
              updatepartnerlist(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addpartnerlist(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },


    }
  };
</script>
<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }
  .start{
    margin: 20px 0;
  }
</style>
