<template>
  <layout
    :title="layoutTitle"
    subtitle="每日 09:00/15:00 定时抓取目标商品库存数据"
  >
    <template #icon>
      <el-tooltip content="抓取商品管理" placement="top">
        <i class="el-icon-setting" @click="handleUrlManage" />
      </el-tooltip>
    </template>

    <template v-if="showSearch" #search>
      <el-form
        ref="queryForm"
        :model="queryParams"
        size="small"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="关键字" prop="queryWord">
          <el-input
            v-model="queryParams.queryWord"
            placeholder="请输入编号/名称"
            clearable
            style="width: 300px"
            @keyup.enter="handleQuery"
          />
        </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>
    </template>

    <template #toolbar>
      <toolbar
        :show-search.sync="showSearch"
        :excel="true"
        @excel="handleExport"
        @queryTable="getList"
      />
    </template>

    <template #table>
      <el-table v-loading="loading" :data="inventoryList">
        <el-table-column label="序号" type="index" width="60" />
        <el-table-column label="商品" prop="title" width="340">
          <template #default="scope">
            <div style="display: flex; align-items: center; gap: 10px">
              <el-image
                :src="scope.row.image"
                fit="contain"
                style="width: 40px; height: 40px"
              />
              <a
                :href="scope.row.url"
                target="_blank"
                style="
                  flex-grow: 1;
                  width: 0;
                  margin: 0;
                  font-size: 13px;
                  color: var(--primary-color);
                "
              >{{ scope.row.title }}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="店铺名称" prop="shopName" />
        <el-table-column label="商品价格" prop="price" />
        <el-table-column label="库存信息" prop="enableQuantity" sortable />
        <el-table-column label="相关信息" prop="remark" width="300">
          <template #default="scope">
            <div
              v-for="(text, index) in scope.row.remark.split('\n')"
              :key="index"
            >
              <div v-show="text.trim() !== ''" style="font-size: 12px">
                {{ index + 1 }}.{{ text }}
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="最后更新时间"
          prop="updateDate"
          width="200"
          sortable
        />
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleDeleteRow(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 分类管理弹出层 -->
    <el-dialog
      title="抓取商品管理"
      :visible.sync="urlVisible"
      width="660px"
      :before-close="handleUrlCancel"
      custom-class="app-container--dialog"
    >
      <div class="app-container--toolbar">
        <div class="app-container--toolbar-left">
          <div class="toolbar-title">
            <span>数据列表</span>
          </div>
        </div>
        <div class="app-container--toolbar-right">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </div>
      </div>

      <el-table :data="urlList" max-height="520px" size="small">
        <el-table-column label="序号" type="index" align="center" width="60" />
        <el-table-column
          label="商品URL"
          align="center"
          :show-overflow-tooltip="true"
          prop="url"
        />
        <el-table-column
          label="备注"
          align="center"
          :show-overflow-tooltip="true"
          prop="remark"
        />
        <el-table-column
          label="操作"
          align="center"
          width="150"
          class-name="small-padding fixed-width"
        >
          <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>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 添加或修改商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="top">
        <el-form-item label="所属平台" prop="type">
          <el-select v-model="form.type" :disabled="true">
            <el-option
              v-for="dict in dict.type.zs_mall_platform"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="商品URL" prop="url">
          <el-input v-model="form.url" placeholder="请输入商品URL" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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>
  </layout>
</template>

<script>
import {
  listInventory,
  listInventoryUrl,
  getInventoryUrl,
  deleteInventory,
  deleteInventoryUrl,
  addInventoryUrl,
  updateInventoryUrl
} from '@/api/operational/tools'

export default {
  name: 'Post',
  dicts: ['zs_mall_platform'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      inventoryList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        type: this.$route.query.platform,
        queryDate: '',
        queryWord: ''
      },
      // 分类弹出层
      urlVisible: false,
      // 分类列表
      urlList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        url: [{ required: true, message: '商品URL不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    layoutTitle() {
      const { platform } = this.$route.query
      let title = ''
      if (platform === '1') {
        title = 'Rakuten 商城竞品库存查询工具'
      } else if (platform === '2') {
        title = 'Yahoo! 商城竞品库存查询工具'
      }
      return title
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询库存列表 */
    getList() {
      this.loading = true
      listInventory(this.queryParams).then((response) => {
        this.inventoryList = response
        this.loading = false
      })
    },
    getUrlList() {
      listInventoryUrl({
        type: this.$route.query.platform
      }).then((response) => {
        this.urlList = response.rows
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 分类弹出层操作 */
    handleUrlManage() {
      this.urlVisible = true
      this.getUrlList()
    },
    /** 分类弹出层取消 */
    handleUrlCancel() {
      this.urlVisible = false
    },
    /** 分类新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增商品地址'
      this.form.type = this.$route.query.platform
    },
    /** 分类修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id
      getInventoryUrl(id).then((response) => {
        this.form = response
        this.open = true
        this.title = '修改商品地址'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateInventoryUrl([this.form]).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getUrlList()
            })
          } else {
            addInventoryUrl(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getUrlList()
            })
          }
        }
      })
    },
    handleDeleteRow(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认删除编号为"' + id + '"的数据项？')
        .then(function() {
          return deleteInventory(id)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id
      this.$modal
        .confirm('是否确认删除库存编号为"' + id + '"的数据项？')
        .then(function() {
          return deleteInventoryUrl(id)
        })
        .then(() => {
          this.getUrlList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'inventory/exportExcel',
        {
          ...this.queryParams
        },
        `inventory_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
