<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleTabClick">
      <el-tab-pane label="物料流转" style="height: calc(-180px + 100vh); overflow: auto;" name="first">
        <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="enterpriseName"/>
              <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>-->
                  <el-button size="mini" type="text" icon="el-icon-position" @click="handleProcessDeal(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-tab-pane>
      <el-tab-pane label="受理通知单" style="height: calc(-180px + 100vh); overflow: auto;" name="second">
        <el-table :data="dealRecord1List">
          <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="matterName"/>
          <el-table-column label="企业名称" align="center" prop="enterpriseName"/>
          <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="printTime" 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="handleDealDetail(scope.row)">详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total1 > 0" :total="total1" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getDeal1List"/>
      </el-tab-pane>
      <el-tab-pane label="一次性告知单" style="height: calc(-180px + 100vh); overflow: auto;" name="five">
        <el-table :data="dealRecord5List">
          <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="matterName"/>
          <el-table-column label="企业名称" align="center" prop="enterpriseName"/>
          <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="printTime" 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="handleDealDetail(scope.row)">详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total5 > 0" :total="total5" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getDeal5List"/>
      </el-tab-pane>
      <el-tab-pane label="补正材料告知书" style="height: calc(-180px + 100vh); overflow: auto;" name="third">
        <el-table :data="dealRecord2List">
          <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="matterName"/>
          <el-table-column label="企业名称" align="center" prop="enterpriseName"/>
          <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="printTime" 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="handleDealDetail(scope.row)">详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total2 > 0" :total="total2" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getDeal2List"/>
      </el-tab-pane>
      <el-tab-pane label="不予受理通知书" style="height: calc(-180px + 100vh); overflow: auto;" name="fourth">
        <el-table :data="dealRecord3List">
          <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="matterName"/>
          <el-table-column label="企业名称" align="center" prop="enterpriseName"/>
          <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="printTime" 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="handleDealDetail(scope.row)">详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total3 > 0" :total="total3" :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize" @pagination="getDeal3List"/>
      </el-tab-pane>
    </el-tabs>

    <el-dialog title="受理单详情" :visible.sync="dealDetailDialog" :close-on-click-modal="false" width="60%"
               destroy-on-close append-to-body>
      <el-button type="primary" size="small" icon="el-icon-printer" v-print="printObj"
                 style="margin-left: 85%;margin-bottom: 10px;">打印
      </el-button>
      <el-row style="height: 70vh; overflow: auto;">
        <el-descriptions v-if="currDealRecord" id="print_deal_detail" class="margin-top"
                         style="width: 100%; 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>
      </el-row>
    </el-dialog>
    <el-dialog title="详情" :visible.sync="printDetailDialog" :close-on-click-modal="false" width="60%"
               destroy-on-close append-to-body> <el-row id="print_ycxgzd" style="height: auto; padding: 10px;">
      <el-row style="overflow: auto; height: 70vh">
        <div class="matter-material" style="background-color: #fff;" v-html="currPrintDetailInfo"></div>
      </el-row>
    </el-row>
    </el-dialog>
  </div>
</template>

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

export default {
  name: "DealRecord",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      dealDetailDialog: false,
      printDetailDialog: false,
      activeName: 'first',
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serialNumber: undefined,
        withStatus: 0
      },
      // 总条数
      total: 0,
      total1: 0,
      total2: 0,
      total3: 0,
      total5: 0,
      // 受理记录数据
      dealRecordList: [],
      dealRecord1List: [],
      dealRecord2List: [],
      dealRecord3List: [],
      dealRecord5List: [],
      currDealRecord: null,
      currPrintDetailInfo: null,
      detailKey: 0,
      printObj: {
        id: "print_deal_detail",
        popTitle: "打印物料流转单",
        extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
        beforeOpenCallback() {
        },
        openCallback(vue) {
          vue.savePrintLog('print_deal_detail', 4)
        },
        closeCallback(vue) {
        }
      }
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getDeal1List();
    this.getDeal2List();
    this.getDeal3List();
    this.getDeal5List();
  },
  methods: {
    handleTabClick(tab, event) {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        serialNumber: undefined,
        withStatus: 0
      }
    },
    /** 查询物料流转数据 */
    getList() {
      this.loading = true;
      listDealRecord(this.queryParams).then(response => {
        this.dealRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDeal1List() {
      let params = JSON.parse(JSON.stringify(this.queryParams))
      params.printType = 1
      listPrintLogList(params).then(response => {
        this.dealRecord1List = response.rows;
        this.total1 = response.total;
      });
    },
    getDeal5List() {
      let params = JSON.parse(JSON.stringify(this.queryParams))
      params.printType = 5
      listPrintLogList(params).then(response => {
        this.dealRecord5List = response.rows;
        this.total5 = response.total;
      });
    },
    getDeal2List() {
      let params = JSON.parse(JSON.stringify(this.queryParams))
      params.printType = 2
      listPrintLogList(params).then(response => {
        this.dealRecord2List = response.rows;
        this.total2 = response.total;
      });
    },
    getDeal3List() {
      let params = JSON.parse(JSON.stringify(this.queryParams))
      params.printType = 3
      listPrintLogList(params).then(response => {
        this.dealRecord3List = response.rows;
        this.total3 = response.total;
      });
    },
    handleDealDetail(row) {
      console.log(row)
      this.currPrintDetailInfo = row.content
      this.printDetailDialog = true
    },
    /** 搜索按钮操作 */
    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) {
            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
    },
    /** 新增打印日志 **/
    savePrintLog(elm, type) {
      let data = {}
      data.content = document.getElementById(elm).outerHTML
      data.printType = type
      data.matter = this.currDealRecord.matterId
      data.matterName = this.currDealRecord.itemName
      data.withType = this.currDealRecord.withType
      data.contacts = this.currDealRecord.contacts
      data.phone = this.currDealRecord.phone
      data.idCard = this.currDealRecord.idCard
      data.serialNumber = this.currDealRecord.serialNumber
      addPrintLog(data).then((response) => {
      });
    },
    /** 物料流转 */
    handleProcessDeal(row) {
      this.$confirm('您正在流转该受理记录, 是否继续？', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let data = {
          "id": row.id
        }
        updateDealRecordStatus(data).then((response) => {
          if (response.data > 0) {
            this.handleDetail(row)
            this.getList()
          } else {
            this.$modal.msgError("流转失败");
          }
        });
      }).catch(() => {
      })
    }
  }
};
</script>
<style scoped>
.ql-align-center {
  text-align: center;
}
</style>
