<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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bus:japanzh: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="phone" :show-overflow-tooltip="true"/>
      <el-table-column label="应对方法" align="center" prop="copingMethod" :show-overflow-tooltip="true"/>
      <el-table-column label="召回时间" align="center" prop="recallTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recallTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对象特定情报" align="center" prop="specificInformation" :show-overflow-tooltip="true"/>
      <el-table-column label="参照情报" align="center" prop="referenceInformation" :show-overflow-tooltip="true" />
      <el-table-column label="回收原因" align="center" prop="recycleCause" :show-overflow-tooltip="true"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 原有的对话框保持不变 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model="form.createUser" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建部门" prop="createDept">
          <el-input v-model="form.createDept" placeholder="请输入创建部门" />
        </el-form-item>
        <el-form-item label="修改人" prop="updateUser">
          <el-input v-model="form.updateUser" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="删除状态" prop="isDeleted">
          <el-input v-model="form.isDeleted" placeholder="请输入删除状态" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="危险描述" prop="dangerDescription">
          <el-input v-model="form.dangerDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="补救描述" prop="remedyDescription">
          <el-input v-model="form.remedyDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="召回时间" prop="recallTime">
          <el-date-picker clearable
                          v-model="form.recallTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择召回时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="召回数量" prop="recallAmount">
          <el-input v-model="form.recallAmount" placeholder="请输入召回数量" />
        </el-form-item>
        <el-form-item label="召回原因" prop="recallCause">
          <el-input v-model="form.recallCause" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="售出地点" prop="pointSale">
          <el-input v-model="form.pointSale" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="制造商" prop="producers">
          <el-input v-model="form.producers" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="产地" prop="origin">
          <el-input v-model="form.origin" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="事故/伤亡描述" prop="accidentDescription">
          <el-input v-model="form.accidentDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="详情页地址" prop="detailsUrl">
          <el-input v-model="form.detailsUrl" type="textarea" placeholder="请输入详情页地址" />
        </el-form-item>
      </el-form>
      <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 { listJapanzh, getJapanzh, delJapanzh, addJapanzh, updateJapanzh } from "@/api/bus/japanzh"

export default {
  name: "Japan",
  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,
        updateUser: 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
      listJapanzh(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,
        updateUser: 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
      getJapanzh(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) {
            updateJapanzh(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addJapanzh(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 delJapanzh(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('bus/japanzh/export', {
        ...this.queryParams
      }, `日本召回商品中文_${new Date().getTime()}.xlsx`)
    },

  }
}
</script>
