<template>
  <div class="app-container">
    <div class="container">
      <!-- 数据表格 -->
      <el-table :data="tableList"
        :cell-style="{ 'word-wrap': 'break-word', 'white-space': 'normal' }"
        border
        style="width: 100%">
        <el-table-column v-for="item in columns"
          :key="item.label"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
          :fixed="item.fixed"
          show-overflow-tooltip>
          <template #default="scope">
            <template v-if="item.prop === 'storePicture'">
              <!-- 如果是门头照字段，渲染图片 -->
              <el-image :preview-src-list="[scope.row[item.prop]]"
                preview-teleported
                :src="scope.row[item.prop]"
                alt="图片"
                style="width: 100px; height: 100px;" />
            </template>
            <template v-else-if="item.prop === 'temWorkPhotos'">
              <el-image :preview-src-list="[item]"
                preview-teleported
                v-for="item in JSON.parse(scope.row[item.prop])"
                :src="item"
                alt="图片"
                style="width: 100px; height: 100px; margin-right: 10px;" />
            </template>
            <template v-else-if="item.prop === 'storeTcPhoto'">
              <el-image :preview-src-list="[scope.row[item.prop]]"
                preview-teleported
                :src="scope.row[item.prop]"
                alt="图片"
                style="width: 100px; height: 100px;" />
            </template>
            <template v-else-if="item.prop === 'storeZhjPhoto'">
              <el-image :preview-src-list="[scope.row[item.prop]]"
                preview-teleported
                :src="scope.row[item.prop]"
                alt="图片"
                style="width: 100px; height: 100px;" />
            </template>
            <template v-else-if="item.prop === 'posPhotos'">
              <el-image :preview-src-list="[item]"
              preview-teleported
              v-for="item in JSON.parse(scope.row[item.prop])"
                :src="item"
                alt="图片"
                style="width: 100px; height: 100px; margin-right: 10px;" />
            </template>
            <template v-else>
              <!-- 其他字段显示原始值 -->
              {{ formatter(scope.row, scope.column, scope.row[item.prop]) }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作"
          width="200"
          fixed="right">
          <template #default="scope">
            <el-button type="danger"
              @click="deleteView(scope.row)"
              text>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <pagination :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="getTaskList" />
      <!-- 照片查看弹窗 -->
      <el-dialog :title="photoLookInfoObj.storeName"
        v-model="photoDialogVisible"
        width="60%">
        <p>活动记录 ID :{{ photoLookInfoObj.id }}</p>
        <p>活动时间 :{{ photoLookInfoObj.createDate }}</p>
        <el-card v-for="obj in photoDialogList"
          shadow="never">
          <template #header>{{ obj.title }}</template>
          <div class="content">
            <el-result :title="item.time"
              :sub-title="item.title"
              v-for="item in obj.list">
              <template #icon>
                <el-image :src="item.photoUrl"
                  :preview-src-list="[...obj.list.map(o => o.photoUrl)]" />
              </template>
            </el-result>
          </div>
        </el-card>
      </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useDatePickerOptions } from '@/hooks'
import { getWarZoneListAPI, getChargeListAPI, getTaskListAPI, getProCityAPI, deletePromotionSystemAPI, getPromotionSystemAPI, getPromotionActivityAPI, getExamineList } from '@/api'
import { parseTime } from '@/utils'
import store from '@/store'
import { ElMessage, ElMessageBox } from 'element-plus';

const { recentPickerOptions: pickerOptions, last7Date, todayDate } = useDatePickerOptions(0)
const queryParams = reactive({
  taskStatus: '全部',
  region: '全国',
  provinceId: undefined,
  cityId: undefined,
  zoneId: undefined,
  managerId: undefined,
  storeName: undefined,
  page: 1,
  pageSize: 10,
  date: []
})
const photoDialogVisible = ref(false)
const photoDialogList = ref([])
const photoLookInfoObj = ref({})
const total = ref(0)

// 城市经理状态为 true
const cityManagerPrivilege = computed(() => {
  return store.state?.value?.user?.userInfo?.privilegeId == 1
})

// 任务状态
const taskStatusList = ref([
  {
    label: '是',
    value: '是'
  },
  {
    label: '否',
    value: '否'
  },
  {
    label: '全部',
    value: '全部'
  }
])

// 区域查询
const provinceList = ref([])
const cityList = ref([])
const getProCity = async () => {
  const { data } = await getProCityAPI(queryParams)
  if (queryParams.provinceId) {
    queryParams.cityId = undefined
    cityList.value = data.map(item => ({
      label: item.name,
      value: item.num
    }))
  } else {
    provinceList.value = data.map(item => ({
      label: item.name,
      value: item.num
    }))
  }
  getTaskList()
}
getProCity()

// 战区列表
const zoneList = ref([])
const getZoneList = async () => {
  const { data } = await getWarZoneListAPI()
  zoneList.value = data.map(item => {
    return {
      label: item.orgName,
      value: item.qcId
    }
  })
}
getZoneList()

// 负责人列表
const managerList = ref([])
const getManagerList = async () => {
  const { data } = await getChargeListAPI()
  managerList.value = data.map(item => {
    return {
      label: item.name,
      value: item.id,
      employeeNo: item.employeeNo,
      deptQcName: item.deptQcName,
      deptQcId: item.deptQcId
    }
  })
}
getManagerList()

// 任务列表
const tableList = ref([])
const columns = ref([
  {
    label: '稽核标识',
    prop: 'id',
    width: 80,
  },
  {
    label: '稽核人',
    prop: 'createBy',
    width: 100,
  },
  {
    label: '稽核人工号',
    prop: 'employeeNo',
    width: 100,
  },
  {
    label: 'cp活动 ID',
    prop: 'planId',
    width: 100
  },
  {
    label: '是否执行',
    prop: 'planStatus',
    width: 100
  },
  {
    label: '战区',
    prop: 'deptQcOrgName',
    width: 100
  },
  {
    label: '城市经理',
    prop: 'manageName',
    width: 120
  },
  {
    label: '城市',
    prop: 'city',
    width: 100
  },
  {
    label: '活动日期',
    prop: 'planDate',
    width: 150
  },
  {
    label: '系统名称',
    prop: 'lineName',
    width: 150
  },
  {
    label: '门店',
    prop: 'storeName',
    width: 210
  },
  {
    label: '经销商',
    prop: 'dealerName',
    width: 200
  },
  {
    label: '活动模式',
    prop: 'pattern',
    width: 100
  },
  {
    label: '稽核日期',
    prop: 'createDate',
    width: 120
  },
  {
    label: '门头照',
    prop: 'storePicture',
    width: 150
  },
  {
    label: '促销员人数',
    prop: 'temNum',
    width: 100
  },

  {
    label: '地堆',
    prop: 'storeDd',
    width: 80
  },
  {
    label: '促销员是否在岗',
    prop: 'temOnWork',
    width: 120
  },
  {
    label: '话术',
    prop: 'temHs',
    width: 80
  },
  {
    label: '物料',
    prop: 'temWl',
    width: 80
  },
  {
    label: '着装',
    prop: 'temZz',
    width: 80
  },
  {
    label: '工作取证照片',
    prop: 'temWorkPhotos',
    width: 250
  },
  {
    label: '特陈照',
    prop: 'storeTcPhoto',
    width: 150
  },
  {
    label: '主货架照',
    prop: 'storeZhjPhoto',
    width: 150
  },
  {
    label: 'POS 金额',
    prop: 'posRmb',
    width: 120
  },
  {
    label: 'POS 照片',
    prop: 'posPhotos',
    width: 250
  },
  {
    label: '创建人',
    prop: 'createBy',
    width: 120
  },
  {
    label: '创建时间',
    prop: 'createTime',
    width: 120
  },
  {
    label: '修改人',
    prop: 'modifyBy',
    width: 120
  },
  {
    label: '修改时间',
    prop: 'mobifyTime',
    width: 120
  }
])

// 删除任务列表
const deleteView = async (row) => {
  await ElMessageBox.confirm('确认删除吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  await deletePromotionSystemAPI(row.id)
  // 提示删除成功
  getTaskList()
  ElMessage.success('删除成功')
}

// 获取稽核任务列表
const getTaskList = async () => {
  const { data: { records, totalRecord } } = await getExamineList(queryParams)
  tableList.value = records
  total.value = totalRecord
}
getTaskList()

const formatter = (row, columns, value) => {

  // 判断有值再转换
  if (columns.property === 'planDate' || columns.property === 'createDate' || columns.property === 'createTime' || columns.property === 'mobifyTime') {
    return parseTime(value, '{y}-{m}-{d}')
  } else {
    return value
  }

}

// 区域查询切换
const regionChange = (val) => {
  if (val === '全国') {
    queryParams.provinceId = undefined
    queryParams.cityId = undefined
  } else if (val === '省') {
    queryParams.cityId = undefined
  } else if (val === '省-市') {
    cityList.value = []
    getProCity()
  }
  getTaskList()
}

// 查看照片
const lookPhoto = async (row) => {
  photoDialogVisible.value = true
  photoLookInfoObj.value = row
  const result = await getPromotionActivityAPI(row.id)
  const { tgcjPhotoInfos: tgcjPhotoInfosList, tghdPhotoInfos: tghdPhotoInfosList, tgscPhotoInfos: tgscPhotoInfosList } = result.data
  if (tgscPhotoInfosList) {
    var tgscPhotoInfos = [...tgscPhotoInfosList?.map(item => ({
      time: parseTime(item.createTime, '{h}:{i}'),
      photoUrl: item.photoUrl
    }))]
  }
  if (tghdPhotoInfosList) {
    var tghdPhotoInfos = [...tghdPhotoInfosList?.map(item => ({
      time: parseTime(item.createTime, '{h}:{i}'),
      photoUrl: item.photoUrl
    }))]
  }
  if (tgcjPhotoInfosList) {
    var tgcjPhotoInfos = [...tgcjPhotoInfosList?.map(item => ({
      time: parseTime(item.createTime, '{h}:{i}'),
      photoUrl: item.photoUrl
    }))]
  }

  const res = await getPromotionSystemAPI(row.clockDto.id)
  // 判断上班卡，午休下班卡，午休上班卡，下班卡是否有值，有则添加到数组中
  // 数组是二维数组，第一到三是推广数据，第四个是打卡数据
  const { clockInTime, noonClockOutTime, noonClockInTime, clockOutTime } = res.data
  const clockList = []
  if (clockInTime) {
    clockList.push({
      time: parseTime(clockInTime, '{h}:{i}'),
      title: '上班卡',
      photoUrl: res.data.clockInPhoto
    })
  }
  if (noonClockOutTime) {
    clockList.push({
      time: parseTime(noonClockOutTime, '{h}:{i}'),
      title: '午休下班卡',
      photoUrl: res.data.noonClockOutPhoto
    })
  }
  if (noonClockInTime) {
    clockList.push({
      time: parseTime(noonClockInTime, '{h}:{i}'),
      title: '午休上班卡',
      photoUrl: res.data.noonClockInPhoto
    })
  }
  if (clockOutTime) {
    clockList.push({
      time: parseTime(clockOutTime, '{h}:{i}'),
      title: '下班卡',
      photoUrl: res.data.clockOutPhoto
    })
  }

  // 组装数据
  photoDialogList.value = [{
    title: '推广试吃照片',
    list: tgscPhotoInfos
  }, {
    title: '推广互动照片',
    list: tghdPhotoInfos
  }, {
    title: '推广成交照片',
    list: tgcjPhotoInfos
  }, {
    title: '考勤打卡照片',
    list: clockList
  }]
}

const currentChange = (page) => {
  queryParams.page = page
  getTaskList()
}
</script>


<style scoped
  lang="scss">
  ::v-deep(.el-form-item) {
    width: 350px !important;
  }

  .el-card {
    margin-top: 20px;
  }

  /* 单元格支持 \n 换行 */
  .container .el-table ::v-deep(.cell) {
    /* white-space: pre-line; */
    /* 强制显示 2 行，超出的省略号 */
    /* overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical; */

  }

  ::v-deep(.el-overlay) {
    .el-card__body {


      .content {
        /* display: flex; */
        overflow-y: hidden;
        overflow-x: scroll;
        white-space: nowrap;

        /* 阻止内容换行 */
        &::-webkit-scrollbar-track {
          background: transparent;
          /* 滚动条轨道背景透明 */
        }

        .el-result {
          width: 250px;
          height: 350px;
          display: inline-block;

          .el-result__icon {
            width: 100%;
            height: 100%;
          }

          .el-image {
            width: 100%;
            height: 100%;
          }
        }
      }
    }
  }

</style>