<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      class="app-container--search"
    >
      <el-form-item label="发生时间">
        <el-date-picker
          v-model="daterangeWarnTime"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsSkuName">
        <el-input
          v-model="queryParams.goodsSkuName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品编号" prop="goodsSkuSn">
        <el-input
          v-model="queryParams.goodsSkuSn"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择处理状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.zs_operation_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['system:operationWarnresult: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="['system:operationWarnresult:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:operationWarnresult:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:operationWarnresult:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="operationWarnresultList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="false" label="主键ID" align="center" prop="id" />
      <el-table-column
        label="发生时间"
        align="center"
        prop="warnTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.warnTime, '{y}-{m}-{d} {h}:{i}:{s}')
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警商品" align="center" prop="goodsSkuName">
        <template slot-scope="scope">
          <div class="goods--info">
            <el-link
              type="primary"
              :underline="false"
              :href="scope.row.goodsSkuUrl"
              target="_blank"
              :title="scope.row.goodsSkuName"
            >
              {{ scope.row.goodsSkuName }}
            </el-link>
            <em
              style="
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
              "
              :title="scope.row.remark"
            >{{ scope.row.remark }}</em>
            <em>{{ scope.row.goodsSkuSn }}</em>
            <em>￥{{ scope.row.initPrice }}</em>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        width="150"
        label="预警价格"
        align="center"
        prop="initPrice"
      >
        <template slot-scope="scope">
          <div
            v-if="scope.row.curPrice"
            class="price--info"
            style="text-align: left; display: flex"
          >
            <span
              style="
                display: inline-flex;
                align-items: center;
                justify-content: center;
                text-align: center;
                width: 30px;
                padding: 2px;
              "
            >
              <i
                v-if="
                  parseFloat(scope.row.curPrice) >
                    parseFloat(scope.row.initPrice)
                "
                class="el-icon-top"
                style="color: green; font-size: 16px"
              />
              <i
                v-else-if="
                  parseFloat(scope.row.curPrice) ===
                    parseFloat(scope.row.initPrice)
                "
                class="el-icon-minus"
                style="color: #999; font-size: 16px"
              />
              <i
                v-else
                class="el-icon-bottom"
                style="color: red; font-size: 16px"
              />
            </span>

            <span
              style="color: #303133; font-size: 14px"
            >￥{{ scope.row.curPrice }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        width="120"
        label="通知方式"
        align="center"
        prop="warnTypes"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.zs_operation_warn_types"
            :value="scope.row.warnTypes"
          />
        </template>
      </el-table-column>
      <el-table-column width="120" label="平台" align="center" prop="platform">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.zs_operation_platform"
            :value="scope.row.platform"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="220"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 0"
            v-hasPermi="['system:operationWarnresult:edit']"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleDispose(scope.row)"
          >处理</el-button>

          <span
            v-else
            class="text-info"
            style="margin-right: 10px"
          >已处理</span>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-camera"
            @click="handleView(scope.row)"
          >快照</el-button>

          <el-button
            v-hasPermi="['system:operationWarnresult:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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"
    />

    <!-- 添加或修改预警结果对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <div class="warn-result" v-html="warnResult" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listOperationWarnresult,
  getOperationWarnresult,
  delOperationWarnresult,
  addOperationWarnresult,
  updateOperationWarnresult
} from '@/api/system/operationWarnresult'

export default {
  name: 'OperationWarnresult',
  dicts: [
    'zs_operation_warn_types',
    'zs_operation_status',
    'zs_operation_compare_status',
    'zs_operation_platform'
  ],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警结果表格数据
      operationWarnresultList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 图片快照时间范围
      daterangeWarnTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warnTime: undefined,
        goodsSkuName: undefined,
        goodsSkuSn: undefined,
        status: undefined,
        compareStatus: undefined
      },
      // 表单参数
      form: {},
      // 预警结果
      warnResult: '',
      // 表单校验
      rules: {
        snapshotUrl: [
          { required: true, message: '图片快照不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询预警结果列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (this.daterangeWarnTime != null && this.daterangeWarnTime != '') {
        this.queryParams.params['beginWarnTime'] = this.daterangeWarnTime[0]
        this.queryParams.params['endWarnTime'] = this.daterangeWarnTime[1]
      }
      listOperationWarnresult(this.queryParams).then((response) => {
        this.operationWarnresultList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        warnTime: undefined,
        goodsSkuName: undefined,
        goodsSkuSn: undefined,
        initPrice: undefined,
        curPrice: undefined,
        priceChangeRatio: undefined,
        warnTypes: undefined,
        warnContent: undefined,
        status: undefined,
        snapshotUrl: undefined
      }
      this.resetForm('form')
    },
    /** 处理按钮操作 */
    handleDispose(row) {
      const _html = `
        <p class="warn-result-text">预警商品：<a href="${row.goodsSkuUrl}" target="_blank">${row.goodsSkuName}</a></p>
        <p class="warn-result-text">预警价格：${row.curPrice}</p>
        <p class="warn-result-text">预警时间：${row.warnTime}</p>
        <p class="warn-result-text">预警平台：${row.platform}</p>
        <p class="warn-result-text">预警备注：${row.remark}</p>
        <p class="warn-result-text">预警快照：<a href="${row.snapshotUrl}" target="_blank">点击查看</a></p>
        <p class="warn-result-tips">注：如果点击“稍后处理”，则预警结果将不会被处理，预警状态将不会被修改。如果点击“立即处理”，则当前商品基准价格将会被修改为预警价格，价格监控按照新的基准价格进行监控。</p>
      `
      this.$confirm(_html, '提示', {
        confirmButtonText: '立即处理',
        cancelButtonText: '稍后处理',
        dangerouslyUseHTMLString: true,
        closeOnClickModal: false,
        closeOnPressEscape: false,
        showClose: false,
        customClass: 'warn-result-message'
      }).then(() => {
        this.loading = true
        row.status = 1
        updateOperationWarnresult(row)
          .then((response) => {
            this.$modal.msgSuccess('处理成功')
            this.open = false
            this.getList()
          })
          .finally(() => {
            this.loading = false
          })
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeWarnTime = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleView(row) {
      // 将状态设置为已处理
      this.open = true
      this.title = '预警结果'
      this.warnResult = row.warnContent
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.id != null) {
            updateOperationWarnresult(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addOperationWarnresult(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除预警结果编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true
          return delOperationWarnresult(ids)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/operationWarnresult/export',
        {
          ...this.queryParams
        },
        `operationWarnresult_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
<style lang="scss">
.warn-result table {
  width: 100% !important;
}
.warn-result table td a {
  display: none;
}
.warn-result table img {
  width: 100% !important;
}
.warn-result-message {
  .warn-result-text {
    font-size: 14px;
    color: #303133;
    a {
      color: #409eff;
      text-decoration: underline;
    }
  }
  .warn-result-tips {
    font-size: 12px;
    color: #999;
    margin-top: 10px;
  }
}
</style>
<style lang="scss" scoped>
.goods--info {
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: left;
  .el-link {
    font-size: 14px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  em {
    font-size: 12px;
    color: #999;
    font-style: normal;
  }
}
</style>
