<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <button-bar view-title="挂车出库管理" ref="btnbar" :btndisabled="btndisabled" @btnhandle="btnHandle"/>
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="isSearchShow = !isSearchShow">
            {{ isSearchShow ? "隐藏查询条件" : "显示查询条件" }}
          </el-button>
          <div v-show="isSearchShow" class="search">
            <el-form :inline="true" class="tab-header" label-width="100px">
              <el-form-item label="申请编号">
                <el-input v-model="queryParams.params.billNo" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="合同编号">
                <el-input v-model="queryParams.params.contractNo" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="客户名称">
                <el-input v-model="queryParams.params.customerName" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="主车车架号">
                <el-input v-model="queryParams.params.zcVinNo" placeholder="" clearable/>
              </el-form-item>
              <el-form-item label="挂车车架号">
                <el-input v-model="queryParams.params.gcVinNo" placeholder="" clearable/>
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center">
              <el-button type="primary" size="small" icon="el-icon-search" @click="dosearch">查询</el-button>
              <el-button type="primary" size="small" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </div>
          </div>
        </div>
        <!--Start 项目列表头部-->
        <div class="listtop">
          <div class="tit">挂车出库申请列表</div>
          <pageye v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current" :limit.sync="queryParams.size" class="pagination" @pagination="loadList"/>
        </div>
        <!--End 项目列表头部-->
        <!--Start 项目列表-->
        <div class="">
          <el-table v-loading="tableLoading" :data="dataList" border style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column width="50px" fixed type="selection" align="center"/>
            <el-table-column width="60px" fixed label="序号" type="index" :index="indexMethod" align="center"/>
            <el-table-column label="操作" fixed width="160px" align="center">
              <template slot-scope="scope">
                <el-button type="primary" size="small" :disabled="scope.row.nodeState =='发起申请' ? false : scope.row.nodeState == '待提交' ? false : true" @click="toEdit(scope.row)">办理</el-button>
                <el-button type="primary" size="small" @click="toInfo(scope.row)">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column label="状态" width="180" header-align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.nodeState=='待提交'" type="primary" size="mini">待提交</span>
                <span v-else @click="flowRecord(scope.row)" class="bluezi">{{ scope.row.nodeState }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="billNo" label="申请单编号" align="center" width="150"/>
            <el-table-column prop="contractNo" label="合同编号" align="center" width="140"/>
            <el-table-column prop="customerName" label="客户名称" align="center" width="130"/>
            <el-table-column prop="saleTypeValue" label="销售类型" align="center" width="130"/>
            <el-table-column prop="zcVinNo" label="主车车架号" align="center" width="120"/>
            <el-table-column prop="gcVinNo" label="挂车车架号" align="center" width="120"/>
            <el-table-column prop="businessState" label="业务状态" align="center" width="130"/>
            <el-table-column prop="invoiceTitle" label="开票单位" align="center" width="150"/>
            <el-table-column prop="billTypeValue" label="开票类型" align="center" width="100"/>
            <el-table-column prop="oneBillMoney" label="开票金额" align="center" width="100"/>
            <el-table-column prop="openTickRemarks" label="开票信息" align="center" width="200"/>
          </el-table>
        </div>
        <!--End 项目列表-->
        <div class="pages">
          <div class="tit"/>
          <!-- 翻页 -->
          <pagination v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current" :limit.sync="queryParams.size" class="pagination" @pagination="loadList"/>
        </div>
      </div>
    </div>
    <!--End 查询和其列表部分-->
    <!--新增修改部分组件-->
    <traileroutAdd v-show="viewState == 2" ref="divAdd" @doback="resetState" @reloadlist="loadList"/>
    <!--详情部分组件-->
    <traileroutInfo v-show="viewState == 3" ref="divinfo" @doback="resetState"/>

    <el-dialog title="" :visible.sync="centerDialogVisible" width="78%" height="1%" :before-close="closeIt" center>
      <iframe frameborder="0" id="iframe" style="width:100%;" scrolling="no" :src="this.centerDialogVisible === true ? url :''"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/guache/guachechuku'
import ButtonBar from '@/components/ButtonBar'
import Pagination from '@/components/pagination'
import pageye from '@/components/pagination/pageye'
import traileroutAdd from './traileroutAdd'
import traileroutInfo from './traileroutInfo'
import { getStorage } from '@/utils/auth'

export default {
  name: 'GuaCheChuKu',
  components: {
    ButtonBar,
    Pagination,
    pageye,
    traileroutAdd,
    traileroutInfo
  },
  data() {
    return {
      url: '',
      dialogHeight: '80%',
      centerDialogVisible: false,
      btndisabled: false,
      btnList: [
        {
          type: 'danger',
          size: 'small',
          icon: 'del',
          btnKey: 'doDel',
          btnLabel: '删除'
        },
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      viewState: 1, // 1、列表 2、添加 3、修改 4、查看
      isSearchShow: false,
      tableLoading: false,
      dataList: [],
      queryParams: {
        current: 1,
        size: 5,
        total: 0,
        params: {
          billNo: '',
          contractNo: '',
          customerName: '',
          gcVinNo: '',
          orgSidPath: '',
          sid: '',
          userSid: '',
          zcVinNo: ''
        }
      },
      multipleSelection: []
    }
  },
  mounted() {
    // 在外部vue的window上添加postMessage的监听，而且绑定处理函数handleMessage
    window.addEventListener('message', this.handleMessage)
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  created() {
    this.loadList()
  },
  methods: {
    async handleMessage(event) {
      var code = ''
      if (event.data.params !== null && event.data.params !== undefined) {
        code = event.data.params.code
      }
      if (code === 1) {
        this.centerDialogVisible = false
      } else if (code === 2) {
        this.dialogHeight = event.data.params.data
        this.setIframeHeight(document.getElementById('iframe'))
      }
    },
    closeIt() {
      this.url = ''
      this.centerDialogVisible = false
    },
    setIframeHeight(iframe) {
      iframe.height = this.dialogHeight
    },
    flowRecord(row) {
      this.centerDialogVisible = true
      var params = {
        deployId: row.procDefId,
        procInsId: row.procInstSid,
        token: getStorage()
      }
      this.url = '/#/flow/flowRecordForBusiness?data=' + encodeURI((JSON.stringify(params)))
    },
    btnHandle(btnKey) {
      console.log('XXXXXXXXXXXXXXX    ' + btnKey)
      switch (btnKey) {
        case 'doDel':
          this.doDel()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    loadList() {
      this.tableLoading = true
      this.queryParams.params.userSid = window.sessionStorage.getItem('userSid')
      this.queryParams.params.orgSidPath = window.sessionStorage.getItem('defaultOrgPath')
      req.listPage(this.queryParams).then((resp) => {
        this.tableLoading = false
        if (resp.success) {
          const data = resp.data
          this.queryParams.total = data.total
          this.dataList = data.records
        } else {
          this.queryParams.total = 0
          this.dataList = []
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleSelectionChange(row) {
      const aa = []
      row.forEach((element) => {
        aa.push(element.sid)
      })
      this.multipleSelection = aa
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.queryParams.current - 1) * this.queryParams.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    dosearch() {
      this.queryParams.current = 1
      this.loadList()
    },
    resetQuery() {
      this.queryParams = {
        current: 1,
        size: 5,
        total: 0,
        params: {
          billNo: '',
          contractNo: '',
          customerName: '',
          gcVinNo: '',
          orgSidPath: '',
          sid: '',
          userSid: '',
          zcVinNo: ''
        }
      }
      this.loadList()
    },
    toEdit(row) {
      this.viewState = 2
      this.$refs['divAdd'].showEdit(row)
    },
    toInfo(row) {
      this.viewState = 3
      this.$refs['divinfo'].showInfo(row)
    },
    doDel() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          showClose: true,
          type: 'error',
          message: '请选择至少一条记录进行删除操作'
        })
        return
      }
      const tip = '请确认是否删除所选 ' + this.multipleSelection.length + ' 条记录?'
      this.$confirm(tip, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        req.delBySids(this.multipleSelection).then((resp) => {
          loading.close()
          if (resp.success) {
            this.$message({ type: 'success', message: resp.msg, showClose: true })
            this.loadList()
          }
        }).catch((e) => {
          loading.close()
        })
      }).catch(() => {
      })
    },
    resetState() {
      this.viewState = 1
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
</style>
