<template>
  <div class="app-container algorithm">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="开始时间" prop="inTime">
             <el-date-picker clearable size="small"
               v-model="queryParams.inTime"
               type="date"
               value-format="yyyy-MM-dd"
               placeholder="请选择开始时间">
             </el-date-picker>
           </el-form-item>
      <el-form-item label="结束时间" prop="outTime">
             <el-date-picker clearable size="small"
               v-model="queryParams.outTime"
               type="date"
               value-format="yyyy-MM-dd"
               placeholder="请选择结束时间">
             </el-date-picker>
           </el-form-item>
      <el-form-item label="处理状态" prop="inOutType">
              <el-select v-model="queryParams.status" placeholder="选择时间类型">
                <el-option
                  v-for="item in dealStatus"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
      </el-form-item>
      <el-form-item label="是否报警" prop="inOutType">
        <el-select v-model="queryParams.isAlarm" placeholder="请选择是否报警">
          <el-option
            v-for="item in alarmList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item >
      <el-form-item>
          <p class="inquire-button-one" @click="handleQuery">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>

      </el-form>

    <el-table v-loading="loading" :data="algorithmList" @selection-change="handleSelectionChange">
      <el-table-column label="类型" align="center" prop="algorithmType" >
        <template scope="scope">
          <p v-if="scope.row.algorithmType=='image'">图片类型</p>
          <p v-if="scope.row.algorithmType=='video'">视频类型</p>
        </template>
      </el-table-column>
      <el-table-column label="算法名称" align="center" prop="algorithmName" />
      <el-table-column label="原始图" width="150">
        <template scope="scope">
          <img :src="scope.row.originalImg" width="120" style="cursor:pointer;" height="80" class="img" @click="imgBigBox(scope.row,'original')"/>
        </template>
      </el-table-column>
      <el-table-column label="请求时间" align="center" prop="createTime" />
      <el-table-column label="识别结果" width="150">
        <template scope="scope">
          <img :src="scope.row.algorithmResult" width="120" style="cursor:pointer;" height="80" class="img" @click="imgBigBox(scope.row,'result')"/>
        </template>
      </el-table-column>
      <el-table-column label="响应时间" align="center" prop="outTime" />
      <el-table-column label="是否报警" align="center" prop="isAlarm">
        <template scope="scope">
          <p v-if="scope.row.isAlarm=='0'">否</p>
          <p v-if="scope.row.isAlarm=='1'">是</p>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="status" >
      <template scope="scope">
        <p v-if="scope.row.status=='0'">未处理</p>
        <p v-if="scope.row.status=='1'">已处理</p>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
               style="margin-right:10px;"
               @click="handleUpdate(scope.row)"
               v-hasPermi="['laboratory:algorithm:edit']"
            ><i class="el-icon-edit-outline"></i>修改</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['laboratory:algorithm:remove']"
            ><i class="el-icon-edit-outline"></i>删除</p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination :page-sizes="[20, 30, 40, 50]"
      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="algorithmType">
           <el-input v-model="form.algorithmType" placeholder="请输入解析结果" />
        </el-form-item>
        <el-form-item label="算法名称" prop="algorithmName">
          <el-input v-model="form.algorithmName" 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>
    <el-dialog :title="imgTitle" :visible.sync="imgOpen" width="800px" append-to-body>
      <div class="scrollbar-box" style="width:760px;max-height:700px;overflow-y: scroll;">
        <img :src="imgUrl" style="width:760px;">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAlgorithm, getAlgorithm, delAlgorithm, addAlgorithm, updateAlgorithm } from "@/api/laboratory/algorithm";

export default {
  name: "Algorithm",
  data() {
    return {
      imgTitle:"",
      imgOpen:false,
      imgUrl:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 算法回调结果表格数据
      algorithmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dealStatus: [
        {
          value: -1,
          label: '请选择'
        },
        {
          value: '0',
          label: '未处理'
        },
        {
          value: '1',
          label: '已处理'
        }
      ],
      alarmList: [
          {
              value: null,
              label: '请选择'
          },
          {
              value: '0',
              label: '否'
          },
          {
              value: '1',
              label: '是'
          }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        algorithmType: null,
        algorithmResult: null,
        parseResult: null,
        parseResultMsg: null,
        signId: null,
        subId: null,
        isAlarm:null,
        inTime: null,
        outTime: null,
        status: -1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createTime: [
          { required: true, message: "签到id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //点击放大图片
    imgBigBox(row,doType){
      if(doType=='original'){
        this.imgTitle = '算法名称：'+row.algorithmName;
        this.imgUrl = row.originalImg;
      }else if(doType=='result'){
        this.imgTitle = '算法名称：'+row.algorithmName;
        this.imgUrl = row.algorithmResult;
      }

      this.imgOpen = true;
    },
    /** 查询算法回调结果列表 */
    getList() {
      this.loading = true;
      listAlgorithm(this.queryParams).then( response => {
        this.algorithmList =  response.rows;
        this.total =  response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        algorithmType: null,
        algorithmResult: null,
        parseResult: null,
        parseResultMsg: null,
        signId: null,
        subId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.status=-1;
      this.queryParams.isAlarm='';
      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
      getAlgorithm(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) {
            updateAlgorithm(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAlgorithm(this.form).then( response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAlgorithm(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/algorithm/export', {
        ...this.queryParams
      }, `laboratory_algorithm.xlsx`)
    }
  }
};

</script>

<style scoped lang="scss">
  .algorithm {
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:20px 20px 20px!important;
    .button-box{
      width:190px;
      display: flex;
      margin:0 auto;
    }
  }
</style>
