<template>
    <div>
      <div class="gva-search-box">
        <el-form ref="searchForm" :inline="true" :model="searchInfo">
          <el-form-item label="游戏id">
            <el-input v-model="searchInfo.game_id" placeholder="游戏id" />
          </el-form-item>
          <el-form-item label="日期" prop="create_date">
           <el-date-picker
            v-model="searchInfo.create_date"
            popper-class="picker-popovers"
            class="timefilter"
            type="datetime"
            placeholder="选择日期时间"
            value-format="YYYY-MM-DD"
          >
          </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" icon="search" @click="onSubmit">查询</el-button>
            <el-button size="small" icon="refresh" @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="gva-table-box">
        <el-table border
        :data="tableData"
        :span-method="objectSpanMethod"
        @sort-change="sortChange"
        @selection-change="handleSelectionChange"
        header-align="center"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column fixed="left" align="left" label="游戏id" min-width="70" prop="game_id" sortable="custom" />
          <el-table-column fixed="left" align="left" label="日期" min-width="90" prop="create_date" />
          <el-table-column fixed="left" align="left" label="脚本负责人" min-width="70" prop="operator" />
          <el-table-column fixed="left" align="left" label="类型" min-width="70" prop="type">
          <template #default="scope">
            <div>
              {{ typeFiletr(scope.row.type) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="left" align="center" label="任务总数" min-width="70" prop="target_num" />
        <el-table-column fixed="left" align="center" label="下发账号" min-width="70" prop="issued_account" />
        <el-table-column label="留存账号" align="center"  min-width="100" >
        <el-table-column label="能付费" align="center">
          <template #default="scope">
              {{ scope.row.retained_account_num }}
							<el-divider style="margin:0" />
              {{ scope.row.fee_account_num}}
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="下拉成功率" align="center"  min-width="100" prop="pull_success_rate" sortable="custom">
        <el-table-column label="拉取账号" align="center" >
            <el-table-column prop="pull_account_ok,pull_account_fail" label="下发账号" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.pull_account_ok }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.issued_account}}
							<el-divider style="margin:0" />
							{{ scope.row.pull_success_rate + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column label="启动成功率" align="center" min-width="100" prop="start_success_rate" sortable="custom">
        <el-table-column label="模拟器启动成功" align="center">
            <el-table-column prop="game_start_ok,pull_account_ok" label="拉取成功" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.simulator_start_ok }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.pull_account_ok}}
							<el-divider style="margin:0" />
							{{ scope.row.start_success_rate + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column label="主线成功率" align="center"  min-width="100" prop="main_success_rate" sortable="custom">
        <el-table-column label="进入主线" align="center">
            <el-table-column prop="enter_main,game_start_ok" label="游戏启动成功" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.enter_main }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.game_start_ok}}
							<el-divider style="margin:0" />
							{{ scope.row.main_success_rate + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column label="任务成功率" align="center"  min-width="100" prop="task_success_rate" sortable="custom">
        <el-table-column label="进入主线" align="center">
          <el-table-column prop="enter_main,pull_account_fail" label="总任务数" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.enter_main }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.target_num}}
							<el-divider style="margin:0" />
							{{ scope.row.task_success_rate + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column label="扫码完成率" align="center"  min-width="100" prop="local_order_success_rate" sortable="custom">
        <el-table-column label="扫码成功" align="center">
          <el-table-column prop="target_num,scanning_code_success" label="任务总数" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.scanning_code_success }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.target_num}}
							<el-divider style="margin:0" />
							{{ scope.row.scanning_code_success == 0  ? 0 + '%' : Math.round(scope.row.scanning_code_success/scope.row.target_num * 10000) / 100 + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column label="完成扫码率" align="center"  min-width="100" prop="local_order_success_rate" sortable="custom">
        <el-table-column label="扫码成功" align="center">
          <el-table-column prop="new_complete,scanning_code_success" label="完成总数" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.scanning_code_success }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.new_complete}}
							<el-divider style="margin:0" />
							{{ scope.row.scanning_code_success == 0  ? 0 + '%' : Math.round(scope.row.scanning_code_success/scope.row.new_complete * 10000) / 100 + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column label="微信扫码率" align="center"  min-width="100" prop="scanning_success_rate" sortable="custom">
        <el-table-column label="扫码成功" align="center">
          <el-table-column prop="scanning_code_success,enter_scanning_code" label="扫码总数" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.scanning_code_success }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.enter_scanning_code}}
							<el-divider style="margin:0" />
							{{ scope.row.scanning_success_rate + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column label="订单完成率" align="center"  min-width="100" prop="local_order_success_rate" sortable="custom">
        <el-table-column label="付费成功" align="center">
          <el-table-column prop="fee_ok,pull_account_fail" label="下发付费" align="center" min-width="100">
            <template #default="scope">
							{{ scope.row.fee_ok }}
							<el-divider style="margin:0 5px" direction="vertical" />
              {{ scope.row.order_create}}
							<el-divider style="margin:0" />
							{{ scope.row.fee_ok == 0 || scope.row.order_create == 0 ? 0 + '%' : Math.round(scope.row.fee_ok/scope.row.order_create * 10000) / 100 + '%'}}
						</template>
          </el-table-column>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="付费任务" min-width="70" prop="pay_target"/>
      <el-table-column align="center" label="付费完成" min-width="70" prop="pay_complete"/>
      <el-table-column align="center" label="下发付费" min-width="70" prop="order_create"/>
      <el-table-column align="center" label="订单数" min-width="70" prop="order_success"/>
      <el-table-column align="center" label="付费成功" min-width="70" prop="fee_ok"/>


      <el-table-column label="封号" min-width="60" prop="ban_off" />
      <el-table-column label="冻结" min-width="60" prop="freeze" />
      <el-table-column prop="has_role" label="新增有角色" width="60" />
      <el-table-column prop="not_role" label="活跃无角色" width="60" />
        </el-table>
        <div class="gva-pagination">
          <el-pagination
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
          />
        </div>

      </div>

      <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" :title="dialogTitle">
        <el-table :data="tableData" @sort-change="sortChange" @selection-change="handleSelectionChange" height="500" style="width: 100%">
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column fixed="left" align="left" label="游戏id" min-width="90" prop="game_id" sortable="custom" />
          <el-table-column fixed="left" align="left" label="日期" min-width="90" prop="create_date" />
          <el-table-column fixed="left" align="left" label="脚本负责人" min-width="90" prop="operator" />
          </el-table>
      </el-dialog>
    </div>
  </template>

  <script>
  export default {
    name: 'LogStatisticsList',
  }
  </script>

  <script setup>
  import {
    logStatistics,
  } from '@/api/log'
  import { toSQLLine } from '@/utils/stringFun'
  import { createElementBlock, ref } from 'vue'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import warningBar from '@/components/warningBar/warningBar.vue'

  const apis = ref([])
  const form = ref({
      game_name: '',
      game_package_name: ''
  })


  const type = ref('')
  const rules = ref({
    game_name: [{ required: true, message: '请输入游戏名称', trigger: 'blur' }],
    game_package_name: [
      { required: true, message: '请输入游戏包名', trigger: 'blur' }
    ]
  })

  const page = ref(1)
  const total = ref(0)
  const pageSize = ref(10)
  const tableData = ref([])
  const tableFailData = ref([])
  const searchInfo = ref({})
  let spanArr = []
  const onReset = () => {
    searchInfo.value = {}
  }
  // 搜索

  const onSubmit = () => {
    page.value = 1
    pageSize.value = 10
    searchInfo.value.game_id = Number(searchInfo.value.game_id)
    searchInfo.value.coding = Number(searchInfo.value.coding)
    getTableData()
  }

  const searchFailData = (gameid) => {
    //form.value.parent_id = String(id)
    getTableFailData
    dialogFormVisible.value = true
  }

  // 查询
  const getTableFailData = async() => {
    const table = await logStatistics({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
    if (table.code === 0) {
      tableData.value = table.data
    }
  }

  const typeFiletr = (value) => {
  const target = typeOptions.value.filter(item => item.value === value)[0]
  return target && `${target.label}`
}

const renderheader = ({ column, $index  }) => {
  console.log(column)
  console.log($index)
}

  const typeOptions = ref([
  {
    value: 0,
    label: '新增',
    type: ''
  },
  {
    value: 1,
    label: '活跃',
    type: ''
  }
])

  // 分页
  const handleSizeChange = (val) => {
    pageSize.value = val
    getTableData()
  }

  const handleCurrentChange = (val) => {
    page.value = val
    getTableData()
  }

  const headerStyle = () => {
    return {
      fontSize:"12px"
    }
  }
  // 排序
  const sortChange = ({ prop, order }) => {
    if (prop) {
      if (prop === 'id') {
        prop = 'id'
      }
      searchInfo.value.orderKey = toSQLLine(prop)
      searchInfo.value.desc = order === 'descending'
    }
    getTableData()
  }


  const getSpanArr = (data) => {
    spanArr = []
    var pos = 0
    for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            spanArr.push(1);
            pos = 0
          } else {
            if (data[i].game_id === data[i - 1].game_id) {
              spanArr[pos] += 1;
              spanArr.push(0);
            } else {
              spanArr.push(1);
              pos = i;
            }
          }
        }
        console.log(spanArr)
  }

  const objectSpanMethod = (row) => {
    if (row.columnIndex === 1 || row.columnIndex === 3 || row.columnIndex === 7 || row.columnIndex === 0 || row.columnIndex === 2 || row.columnIndex === 14|| row.columnIndex === 15|| row.columnIndex === 16|| row.columnIndex === 17) {
          const rowspan = spanArr[row.rowIndex];
          const colspan = rowspan > 0 ? 1 : 0;
          return {
            rowspan: rowspan,
            colspan: colspan
          }
        }
  }

  // 查询
  const getTableData = async() => {
    const table = await logStatistics({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
    if (table.code === 0) {
      tableData.value = table.data.list
      total.value = table.data.total
      page.value = table.data.page
      pageSize.value = table.data.pageSize
      getSpanArr(table.data.list)
    }
  }

  getTableData()

  // 批量操作
  const handleSelectionChange = (val) => {
    apis.value = val
  }

  const deleteVisible = ref(false)


  // 弹窗相关
  const apiForm = ref(null)
  const initForm = () => {
    apiForm.value.resetFields()
    form.value = {
      game_name: '',
      game_package_name: ''
    }
  }

  const dialogTitle = ref('新增game')
  const dialogFormVisible = ref(false)
  const openDialog = (key) => {
    switch (key) {
      case 'addGame':
        dialogTitle.value = '新增game'
        break
      case 'edit':
        dialogTitle.value = '编辑game'
        break
      default:
        break
    }
    type.value = key
    dialogFormVisible.value = true
  }
  const closeDialog = () => {
    dialogFormVisible.value = false
  }

  const editCardFunc = async(row) => {
    const res = await getGameById({ id: row.id })
    form.value = res.data
    openDialog('edit')
  }

  const enterDialog = async() => {
    apiForm.value.validate(async valid => {
      if (valid) {
        switch (type.value) {
          case 'addGame':
            {
              const res = await cuGame(form.value)
              if (res.code === 0) {
                ElMessage({
                  type: 'success',
                  message: '添加成功',
                  showClose: true
                })
              }
              getTableData()
              closeDialog()
            }

            break
          case 'edit':
            {
              const res = await cuGame(form.value)
              if (res.code === 0) {
                ElMessage({
                  type: 'success',
                  message: '编辑成功',
                  showClose: true
                })
              }
              getTableData()
              closeDialog()
            }
            break
          default:
            // eslint-disable-next-line no-lone-blocks
            {
              ElMessage({
                type: 'error',
                message: '未知操作',
                showClose: true
              })
            }
            break
        }
      }
    })
  }


  </script>

  <style scoped lang="scss">
  .el-table .cell {
  white-space: pre-wrap;   /*这是重点。文本换行*/
}
  .button-box {
    padding: 10px 20px;
    .el-button {
      float: right;
    }
  }
  .warning {
    color: #dc143c;
  }
  </style>
