<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-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="召回时间" prop="recallTime">
        <el-date-picker clearable
                        v-model="queryParams.recallTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择召回时间">
        </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="info"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleManualCrawl"
          :loading="crawlLoading"
          v-hasPermi="['bus:eu:manual']"
        >立即触发爬虫</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bus:eu:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usList" @selection-change="handleSelectionChange">
      <el-table-column label="商品名称" align="center" prop="goodsName" :show-overflow-tooltip="true"/>
      <el-table-column label="通知国家" align="center" prop="noticeCountry" :show-overflow-tooltip="true"/>
      <el-table-column label="产品类别" align="center" prop="goodsCategory" :show-overflow-tooltip="true"/>
      <el-table-column label="召回时间" align="center" prop="recallTime" width="120" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recallTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品种类" align="center" prop="goodsKinds" width="100" :show-overflow-tooltip="true"/>
      <el-table-column label="品牌" align="center" prop="brand" :show-overflow-tooltip="true" />
      <el-table-column label="原产国" align="center" prop="originCountry" :show-overflow-tooltip="true"/>
      <el-table-column label="风险类型" align="center" prop="riskType" :show-overflow-tooltip="true"/>
      <el-table-column label="风险描述" align="center" prop="riskDescription" :show-overflow-tooltip="true"/>
      <el-table-column label="不合格原因" align="center" prop="unqualifiedCause" :show-overflow-tooltip="true"/>
      <el-table-column label="补救措施" align="center" prop="remedyMeasures" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--          >删除</el-button>-->
<!--          &lt;!&ndash; 根据status状态显示手动触发按钮 &ndash;&gt;-->
<!--          <el-button-->
<!--            v-if="scope.row.status === 1 && scope.row.detailsUrl"-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-video-play"-->
<!--            @click="handleRowCrawl(scope.row)"-->
<!--            :loading="scope.row.crawlLoading"-->
<!--          >更新本条</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 { listEu, getEu, delEu, addEu, updateEu, manualCrawl, crawlSingle } from "@/api/bus/eu"

export default {
  name: "Eu",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 爬虫加载状态
      crawlLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 美国不合格商品展示表格数据
      usList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createUser: null,
        createDept: null,
        updateEuer: null,
        isDeleted: null,
        status: null,
        goodsName: null,
        dangerDescription: null,
        remedyDescription: null,
        recallTime: null,
        recallAmount: null,
        recallCause: null,
        pointSale: null,
        producers: null,
        origin: null,
        accidentDescription: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询美国不合格商品展示列表 */
    getList() {
      this.loading = true
      listEu(this.queryParams).then(response => {
        // 为每条数据添加爬虫加载状态
        this.usList = response.rows.map(item => ({
          ...item,
          crawlLoading: false
        }))
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createUser: null,
        createDept: null,
        createTime: null,
        updateEuer: null,
        updateTime: null,
        isDeleted: null,
        status: null,
        goodsName: null,
        dangerDescription: null,
        remedyDescription: null,
        recallTime: null,
        recallAmount: null,
        recallCause: null,
        pointSale: null,
        producers: null,
        origin: null,
        remark: null,
        accidentDescription: null,
        detailsUrl: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    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
      getEu(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) {
            updateEu(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addEu(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 delEu(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bus/eu/export', {
        ...this.queryParams
      }, `欧盟召回商品_${new Date().getTime()}.xlsx`)
    },

    /** 立即触发爬虫按钮操作 */
    handleManualCrawl() {
      this.$modal.confirm('是否确认立即触发爬虫程序？').then(() => {
        this.crawlLoading = true
        manualCrawl().then(response => {
          this.$modal.msgSuccess("爬虫任务已启动")
          this.crawlLoading = false
          // 可以添加一个提示，告诉用户稍后刷新页面查看新数据
          this.$modal.msgSuccess("爬虫任务已启动，请稍后刷新页面查看数据")
        }).catch(() => {
          this.crawlLoading = false
        })
      }).catch(() => {})
    },
  }
}
</script>
