<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="peopleName">
        <el-input
          v-model="queryParams.peopleName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称"  prop="equipmentId">
        <el-select filterable  v-model="queryParams.equipmentId" placeholder="请选择" clearable @keyup.enter.native="handleQuery" >
          <el-option
            v-for="item in equipmentOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="下发状态"  prop="downResult">
        <el-select filterable  v-model="queryParams.downResult" placeholder="请选择" clearable @keyup.enter.native="handleQuery" >
          <el-option key=0 label="成功" value=0></el-option>
          <el-option key=1 label="失败" value=1></el-option>
        </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-download"
          size="mini"
          :disabled="multiple"
          @click="handleDown"
          v-hasPermi="['system:eqDown:down']"
        >重新下发</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eqDownList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" width="50">
        <template v-slot="scope">
              {{scope.row.id}}
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name">
        <template v-slot="scope">
              {{scope.row.peopleName}}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="equipmentName" >
        <template v-slot="scope">
          {{scope.row.equipmentName}}
        </template>
      </el-table-column>
      <el-table-column label="信息下发" align="center" prop="infoDown" >
        <template v-slot="scope">
          <template v-if="scope.row.downResult===1">
            失败
          </template>
          <template v-if="scope.row.downResult===0">
            成功
          </template>
        </template>
      </el-table-column>
      <el-table-column label="照片下发" align="center" prop="peopleId" min-width="100">
        <template v-slot="scope">
          <el-image style="width: 80px; height: 80px" :src="scope.row.avatar" fit="cover"
                    :preview-src-list="[scope.row.avatar]"
          />
          <div class="published-div" v-if="scope.row.downResult===0">
            <el-button type="success" icon="el-icon-check" size="mini" circle></el-button>
          </div>
          <div class="published-div" v-if="scope.row.downResult===1">
            <el-button type="danger" icon="el-icon-close" size="mini" circle></el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作类型" align="center" prop="operType" >
        <template v-slot="scope">
          <template v-if="scope.row.operType===0">
            新增
          </template>
          <template v-if="scope.row.operType===1">
            修改
          </template>
          <template v-if="scope.row.operType===2">
            重新下发
          </template>
        </template>
      </el-table-column>
      <el-table-column label="下发回执" align="center" prop="msg" width="180">
        <template v-slot="scope">
          <div class="receipt-content">
            <span v-if="!expandStatus[scope.row.id]">
              {{ scope.row.msg ? (scope.row.msg.length > 25 ? scope.row.msg.substring(0, 25) + '...' : scope.row.msg) : '' }}
              <span v-if="scope.row.msg && scope.row.msg.length > 25" class="expand-btn" @click="toggleExpand(scope.row.id)">展开</span>
            </span>
            <span v-else>
              {{ scope.row.msg }}
              <span class="expand-btn" @click="toggleExpand(scope.row.id)">收起</span>
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="100">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
                     size="mini"
                     type="text"
                     icon="el-icon-download"
                     @click="handleDown(scope.row)"
                     v-hasPermi="['system:eqDown:down']"
          >重新下发</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"
    />

  </div>
</template>

<script>
import {alllistEquipment} from "@/api/system/equipment";
import {down, pageList} from "@/api/system/eqDown";

export default {
  name: "EmpowerRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      expandStatus: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 授权记录表格数据
      eqDownList: [],
      equipmentOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        peopleName: null,
        equipmentId: null,
        downResult: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getOptions();
  },
  methods: {
    /** 查询选项列表 */
    getOptions() {
      alllistEquipment({productId: 3}).then(response => {
        this.equipmentOptions = response.data
      })
    },
    /** 查询授权记录列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
        this.eqDownList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    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
    },

    /** 重新下发按钮操作 */
    handleDown(row) {
      const ids = row.id ? [row.id] : this.ids;
      if (!ids.length) {
        this.$modal.msgError("请选择要重新下发的记录！");
        return;
      }
      const title = row.peopleName ? row.peopleName : ids.length + "条数据";
      this.$modal.confirm('是否确定要重新下发' + title + '？').then(function() {
        return down(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("重新下发成功！");
      }).catch(() => {
        this.getList();
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/empowerRecord/export', {
        ...this.queryParams
      }, `empowerRecord_${new Date().getTime()}.xlsx`)
    },
    /** 切换展开/收起状态 */
    toggleExpand(id) {
      this.$set(this.expandStatus, id, !this.expandStatus[id]);
    },
  }
};
</script>
<style>
.published-div {
  position: absolute;
  top: 70px;
  right: 150px;
}
.el-button--mini.is-circle {
  padding: 2px;
}
.expand-btn {
  color: #409EFF;
  cursor: pointer;
  margin-left: 5px;
}
</style>
