<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="160px">
      <el-form-item label="三方程序类型" prop="thirdSoft">
        <el-select v-model="queryParams.thirdSoft" placeholder="请选择三方程序类型" clearable>
          <el-option
            v-for="dict in dict.type.third_soft"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="浏览记录" prop="viewType">
        <el-select v-model="queryParams.viewType" placeholder="请选择浏览记录" clearable>
          <el-option
            v-for="dict in dict.type.view_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备类型" prop="equipment">
        <el-select v-model="queryParams.equipment" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.equipment_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="请输入产品名称" prop="prodName">-->
<!--        <el-input-->
<!--          v-model="queryParams.prodName"-->
<!--          placeholder="请输入产品名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="来源国家" prop="national">
        <el-input
          v-model="queryParams.national"
          placeholder="请输入来源国家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="email/电话/teams账户" prop="thirdAccount">
        <el-input
          v-model="queryParams.thirdAccount"
          placeholder="请输入email/电话/teams账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="IP" prop="ip">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.ip"-->
      <!--          placeholder="请输入IP"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="时间范围" prop="dataRange">
        <el-date-picker
          v-model="queryParams.dataRange"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        >
        </el-date-picker>
      </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-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['busi:thirdItem:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['busi:thirdItem:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['busi:thirdItem:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['busi:thirdItem:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="thirdItemList" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="60" label="序号" align="center"/>
      <!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="产品名称" align="center" prop="prodName" :min-width="120">-->
<!--        <template slot="header" slot-scope="scope">-->
<!--          <span>产品名称</span>-->
<!--          <el-tooltip class="item" effect="dark" content="鼠标单机数据可查看产品详情"-->
<!--                      placement="bottom"-->
<!--          >-->
<!--            <i class="el-icon-question"></i>-->
<!--          </el-tooltip>-->
<!--        </template>-->
<!--        <template slot-scope="scope">-->
<!--          <span @click="goProdDetail(scope.row.prodId)" style="color: #1890ff;cursor: pointer"-->
<!--          >{{ scope.row.prodName }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="三方程序类型" align="center" prop="thirdSoft" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.third_soft" :value="scope.row.thirdSoft"/>
        </template>
      </el-table-column>
      <el-table-column label="来源国家" align="center" prop="national"/>
      <el-table-column label="email/电话/teams账户" align="center" prop="thirdAccount" width="180"/>
      <el-table-column label="浏览记录" align="center" prop="viewType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.view_type" :value="scope.row.viewType"/>
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="equipment">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.equipment_type" :value="scope.row.equipment"/>
        </template>
      </el-table-column>
      <el-table-column label="来源IP" align="center" prop="ip"/>
      <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="['busi:thirdItem:edit']"-->
      <!--          >修改</el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['busi:thirdItem:remove']"-->
      <!--          >删除</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"
    />

  </div>
</template>

<script>
import { listThirdItem, getThirdItem, delThirdItem, addThirdItem, updateThirdItem } from '@/api/busi/thirdItem'

export default {
  name: 'ThirdItem',
  dicts: ['equipment_type', 'third_soft', 'view_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 三方程序跳转记录表格数据
      thirdItemList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dataRange: '',
        startDate: '',
        endDate: '',
        prodName: null,
        thirdSoft: null,
        national: null,
        thirdAccount: null,
        viewType: null,
        pageUrl: null,
        equipment: null,
        ip: null,
        tenantId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询三方程序跳转记录列表 */
    getList() {
      this.loading = true
      listThirdItem(this.queryParams).then(response => {
        this.thirdItemList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        prodId: null,
        thirdSoft: null,
        national: null,
        thirdAccount: null,
        viewType: null,
        pageUrl: null,
        equipment: null,
        ip: null,
        tenantId: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      console.log(this.queryParams.dataRange, '12')
      if (this.queryParams.dataRange && this.queryParams.dataRange.length > 1) {
        this.queryParams.startDate = this.formatDate(this.queryParams.dataRange[0])
        this.queryParams.endDate = this.formatDate(this.queryParams.dataRange[1])
      } else {
        this.queryParams.startDate = null
        this.queryParams.endDate = null
      }
      this.getList()
    },
    /**
     * 格式化时间戳
     */
    formatDate(timestamp) {
      const date = new Date(timestamp)
      const year = date.getFullYear()
      // 月份是从0开始的，所以要加1
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      return `${year}-${month}-${day}`
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加三方程序跳转记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getThirdItem(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) {
            updateThirdItem(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addThirdItem(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除三方程序跳转记录编号为"' + ids + '"的数据项？').then(function() {
        return delThirdItem(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('busi/thirdItem/export', {
        ...this.queryParams
      }, `thirdItem_${new Date().getTime()}.xlsx`)
    },
    /**
     * 查看产品详情
     */
    goProdDetail(id) {
      this.$router.push({ path: '/product/prodForm', query: { id: id } })
    }
  }
}
</script>
