<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="钢卷号" prop="currentCoilNo">
        <el-input
          v-model="queryParams.currentCoilNo"
          placeholder="请输入钢卷号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="挂接状态" prop="bindStatus">
        <el-select v-model="queryParams.bindStatus" placeholder="请选择挂接状态" clearable>
          <el-option label="已挂接" :value="1" />
          <el-option label="已撤回" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作人" prop="operateUser">
        <el-input
          v-model="queryParams.operateUser"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        />
      </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="['mill:relation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="relationList">
      <el-table-column label="关系ID" align="center" prop="relationId" />
      <el-table-column label="钢卷号" align="center" prop="currentCoilNo" />
      <el-table-column label="二级异常ID" align="center" prop="secondAbnormalId" />
      <el-table-column label="三级钢卷ID" align="center" prop="thirdCoilId" />
      <el-table-column label="三级异常ID" align="center" prop="thirdAbnormalId" />
      <el-table-column label="挂接状态" align="center" prop="bindStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bindStatus === 1" type="success">已挂接</el-tag>
          <el-tag v-else-if="scope.row.bindStatus === 2" type="info">已撤回</el-tag>
          <el-tag v-else type="warning">未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="挂接时间" align="center" prop="bindTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bindTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="撤回时间" align="center" prop="withdrawTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.withdrawTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operateUser" />
      <el-table-column label="操作备注" align="center" prop="operateRemark" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </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 { listRelation } from "@/api/mill/coilAbnormalRelation";

export default {
  name: "RelationLog",
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      relationList: [],
      dateRange: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        currentCoilNo: null,
        bindStatus: null,
        operateUser: null,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      const params = { ...this.queryParams };
      if (this.dateRange && this.dateRange.length === 2) {
        params.beginTime = this.dateRange[0];
        params.endTime = this.dateRange[1];
      }
      listRelation(params).then(response => {
        this.relationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    handleExport() {
      const params = { ...this.queryParams };
      if (this.dateRange && this.dateRange.length === 2) {
        params.beginTime = this.dateRange[0];
        params.endTime = this.dateRange[1];
      }
      this.download('mill/relation/export', params, `relation_log_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
