<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24" style="height: calc(100vh - 130px); overflow: hidden;">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px" @submit.native.prevent>
          <el-form-item label="打印编号" prop="serialNumber">
            <el-input v-model="queryParams.serialNumber" placeholder="请输入打印编号" clearable style="width: 240px"
                      @keyup.enter.native="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>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" size="small" icon="el-icon-position" @click="handleSendCert">发放</el-button>
          </el-col>
        </el-row>
        <el-row style="height: calc(100% - 100px); padding: 10px;overflow: auto;">
          <el-table v-loading="loading" :data="certificateSendList"
                    :row-key="getRowKey"
                    @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" :reserve-selection="true" />
            <el-table-column label="打印编号" align="center" prop="serialNumber"/>
            <el-table-column label="证照类型" align="center" prop="certificateType">
              <template slot-scope="scope">
                <span v-if="scope.row.certificateType === 1">个人</span>
                <span v-if="scope.row.certificateType === 2">企业</span>
              </template>
            </el-table-column>
            <el-table-column label="证照名称" align="center" prop="certificateName"/>
            <el-table-column label="证照号码" align="center" prop="certificateNo"/>
            <el-table-column label="发放状态" align="center" prop="certStatus">
              <template slot-scope="scope">
                <span v-if="scope.row.certStatus === 0">未发放</span>
                <span v-if="scope.row.certStatus === 1">已发放</span>
              </template>
            </el-table-column>
            <el-table-column label="打印人员" align="center" prop="printUser">
              <template slot-scope="scope">
                <span v-if="scope.row.printUser">{{ scope.row.printUser }}（{{ scope.row.printDept }}）</span>
              </template>
            </el-table-column>
            <el-table-column label="打印时间" align="center" prop="printTime" width="160" />
            <el-table-column label="发放人员" align="center" prop="sendUser">
              <template slot-scope="scope">
                <span v-if="scope.row.sendUser">{{ scope.row.sendUser }}（{{ scope.row.sendDept }}）</span>
              </template>
            </el-table-column>
            <el-table-column label="发放时间" align="center" prop="sendTime" width="160" />
          </el-table>
          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                      :limit.sync="queryParams.pageSize" @pagination="getList"/>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {listCertificateSend, sendCertificate} from "@/api/matter/record";

export default {
  name: "CertificateSend",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      dealDetailDialog: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: undefined
      },
      // 总条数
      total: 0,
      // 证照发放数据
      certificateSendList: [],
      selectionList: []
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料流转数据 */
    getList() {
      this.loading = true;
      listCertificateSend(this.queryParams).then(response => {
        this.certificateSendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getRowKey(row) {
      return row.id;
    },
    /** 多选框选中数据 **/
    handleSelectionChange(selection) {
      this.selectionList = selection
    },
    handleSendCert() {
      if (this.selectionList.length == 0) {
        this.$modal.msgError("至少选择一条数据");
        return
      }
      let data = []
      this.selectionList.forEach((item, index) => {
        if (item.certStatus != 1) {
          data.push({
            "id": item.id
          })
        }
      })
      if (data.length == 0) {
        this.$modal.msgError("请选择未发放数据");
        return
      }
      sendCertificate(data).then(response => {
        if (response.code == 200) {
          this.$modal.msgSuccess("发放成功");
          this.getList()
          return
        }
        this.$modal.msgError("发放失败");
      });
    }
  }
};
</script>
<style scoped>
</style>
