<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 物料流转数据 -->
      <el-col :span="24" :xs="24">
        <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-table v-loading="loading" :data="dealRecordList">
          <el-table-column label="受理编号" align="center" prop="serialNumber"/>
          <el-table-column label="业务类型" align="center" prop="withType">
            <template slot-scope="scope">
              <span v-if="scope.row.withType === 1">个人</span>
              <span v-if="scope.row.withType === 2">企业</span>
            </template>
          </el-table-column>
          <el-table-column label="联系人" align="center" prop="contacts" width="100"/>
          <el-table-column label="联系电话" align="center" prop="phone" width="120"/>
          <el-table-column label="受理人" align="center" prop="userName">
            <template slot-scope="scope">
              <span v-if="scope.row.userName">{{ scope.row.userName }}（{{ scope.row.deptName }}）</span>
            </template>
          </el-table-column>
          <el-table-column label="受理时间" align="center" prop="createTime" width="160">
          </el-table-column>
          <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(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-col>
    </el-row>

    <el-dialog title="受理单详情" :visible.sync="dealDetailDialog" :close-on-click-modal="false" width="60%"
               destroy-on-close append-to-body>
      <el-descriptions v-if="currDealRecord" class="margin-top" style="height: 60vh; overflow: auto;" :column="3"
                       border :key="detailKey">
        <el-descriptions-item :span="3" label-style="width:110px;">
          <template slot="label">
            <i class="el-icon-price-tag"></i>
            受理编号
          </template>
          {{ currDealRecord.serialNumber }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-full-screen"></i>
            条形码
          </template>
          <el-image :src="'data:image/png;base64,' + currDealRecord.barcode" style="width: 300px;"></el-image>
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-tickets"></i>
            事项名称
          </template>
          {{ currDealRecord.itemName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-collection-tag"></i>
            业务类型
          </template>
          {{ currDealRecord.withType == 1 ? '个人' : '企业' }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            联系人
          </template>
          {{ currDealRecord.contacts }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            联系电话
          </template>
          {{ currDealRecord.phone }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-postcard"></i>
            {{ currDealRecord.withType == 1 ? '身份证号码' : '企业社会信用代码' }}
          </template>
          {{ currDealRecord.idCard }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            受理人
          </template>
          {{ currDealRecord.userName }}
        </el-descriptions-item>
        <el-descriptions-item :span="2">
          <template slot="label">
            <i class="el-icon-time"></i>
            受理时间
          </template>
          {{ currDealRecord.createTime }}
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-document-checked"></i>
            已有材料
          </template>
          <template v-if="this.currDealRecord.materialList && this.currDealRecord.materialList.length > 0">
            <ul>
              <li v-for="(item) in this.currDealRecord.materialList" v-if="item.reasonFlag">
                {{ item.verification === 1 ? (item.listName + '【' + item.dealStatusName + '】') : item.listName }}
              </li>
            </ul>
          </template>
        </el-descriptions-item>
        <el-descriptions-item :span="3">
          <template slot="label">
            <i class="el-icon-document-delete"></i>
            缺失材料
          </template>
          <template v-if="this.currDealRecord.materialList && this.currDealRecord.materialList.length > 0">
            <ul>
              <li v-for="(item) in this.currDealRecord.materialList" v-if="!item.reasonFlag">
                {{ item.listName + '，【原因】：' + item.reason }}
              </li>
            </ul>
          </template>
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary">打印</el-button>-->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getManualVerifyByIds, listDealRecord} from "@/api/matter/record";

export default {
  name: "Deal",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      dealDetailDialog: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: undefined,
        withStatus: 1
      },
      // 总条数
      total: 0,
      // 受理记录数据
      dealRecordList: [],
      currDealRecord: null,
      detailKey: 0
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料流转数据 */
    getList() {
      this.loading = true;
      listDealRecord(this.queryParams).then(response => {
        this.dealRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 受理单详情 */
    handleDetail(row) {
      this.currDealRecord = row
      if (row.materials) {
        let obj = JSON.parse(row.materials)
        let manualVerifyList = []
        obj.forEach((item, index) => {
          if (item.verification === 1 && item.manualVerifyId) {
            manualVerifyList.push(item.manualVerifyId)
          }
        })
        if (manualVerifyList && manualVerifyList.length > 0) {
          getManualVerifyByIds(manualVerifyList).then(response => {
            let data = response.data
            obj.forEach((item, index) => {
              data.forEach((d) => {
                if (d.materialId == item.id) {
                  item.dealStatus = d.dealStatus
                  item.dealStatusName = d.dealStatus == 1 ? '人工核验通过' : (d.dealStatus == 2 ? '人工核验未通过' : '待核验')
                }
              })
            })
            this.$nextTick(() => {
              this.currDealRecord.materialList = obj
              this.detailKey ++
            })
          });
        } else {
          this.$nextTick(() => {
            this.currDealRecord.materialList = obj
            this.detailKey ++
          })
        }
      }
      this.dealDetailDialog = true
    }
  }
};
</script>
<style scoped>
</style>
