<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="clicksearchShow">{{ searchxianshitit }}</el-button>
          <div v-show="isSearchShow" class="search">
            <el-form ref="listQueryform" :inline="true" :model="listQuery" label-width="80px" class="tab-header">
              <el-form-item label="项目名称">
                <el-input v-model="listQuery.params.projectName" maxlength="20" placeholder="" class="addinputw" clearable/>
              </el-form-item>
              <el-form-item label="企业名称">
                <el-input v-model="listQuery.params.enterpriseName" maxlength="20" placeholder="" class="addinputw" clearable/>
              </el-form-item>
              <el-form-item label="申请日期">
                <el-date-picker v-model="listQuery.params.createTimeStart" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
                <span style="padding: 0 8px">至</span>
                <el-date-picker v-model="listQuery.params.createTimeEnd" type="date" clearable value-format="yyyy-MM-dd" placeholder="选择日期"/>
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" icon="el-icon-search" size="small" @click="handleReset">查询</el-button>
              <el-button type="primary" icon="el-icon-refresh" size="small" @click="handleFilter">重置</el-button>
            </div>
          </div>
        </div>
        <div class="listtop">
          <div class="tit">用款列表</div>
        </div>
        <div class="">
          <el-table v-loading="listLoading" ref="Table" :data="list" border style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column fixed width="50" type="selection" align="center"/>
            <el-table-column fixed label="序号" type="index" width="60" :index="indexMethod" align="center"/>
            <el-table-column fixed width="150" label="操作" align="center">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="toEdit(scope.row)" :disabled="scope.row.nodeState =='发起申请' ? false : scope.row.nodeState == '待提交' ? false : true">办理</el-button>
                <el-button type="primary" size="mini" @click="toInfo(scope.row)">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column label="状态" width="90" align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.nodeState=='待提交'" type="primary" size="mini">待提交</span>
                <span v-else @click="handleFlowRecord(scope.row)" class="bluezi">{{ scope.row.nodeState }}</span>
              </template>
            </el-table-column>
            <el-table-column label="项目名称" prop="projectName" align="center"/>
            <el-table-column label="企业名称" prop="enterpriseName" align="center"/>
            <el-table-column label="申请人" prop="createByName" align="center"/>
            <el-table-column label="申请日期" prop="createTime" align="center"/>
          </el-table>
        </div>
        <div class="pages">
          <pagination v-show="list.length > 0" :total="listQuery.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" class="pagination" @pagination="getList"/>
        </div>
      </div>

    </div>
    <ykAdd v-show="viewState ==2 || viewState ==3" ref="divAdd" @doback="resetState" @reloadlist="getList"/>
    <ykInfo v-show="viewState ==4" ref="divInfo" @doback="resetState"/>
    <el-dialog title="" :visible.sync="editDialog" width="78%" height="1%" :before-close="closeIt" center>
      <!-- <iframe frameborder="0" id="iframe" style="width:100%;" scrolling="no" :src="this.centerDialogVisible === true ? url :''"></iframe> -->
      <flow-records :xml-data="xmlData" :task-data="taskList" :flow-record-list="flowRecordList"/>
    </el-dialog>
  </div>
</template>

<script>
import req from '@/api/Yksq/yksq'
import Pagination from '@/components/pagination'
import ButtonBar from '@/components/ButtonBar'
import ykAdd from './ykAdd.vue'
import ykInfo from './ykInfo.vue'
import { getStorage } from '@/utils/auth'
import flowRecords from '@/components/flow/flowRecord'
import { getFlowViewer, readXml } from '@/api/flow/todo'

export default {
  name: 'YongKuan',
  components: {
    Pagination,
    ButtonBar,
    ykAdd,
    ykInfo,
    flowRecords
  },
  data() {
    return {
      listLoading: false,
      btndisabled: false,
      url: '',
      taskList: [],
      xmlData: '',
      btnList: [
        {
          type: 'primary',
          size: 'small',
          icon: 'plus',
          btnKey: 'toAdd',
          btnLabel: '新增'
        },
        {
          type: 'danger',
          size: 'small',
          icon: 'del',
          btnKey: 'doDel',
          btnLabel: '删除'
        },
        {
          type: 'info',
          size: 'small',
          icon: 'cross',
          btnKey: 'doClose',
          btnLabel: '关闭'
        }
      ],
      isSearchShow: false,
      editDialog: false,
      searchxianshitit: '显示查询条件',
      list: [],
      flowRecordList: [],
      state: '',
      listQuery: {
        current: 1,
        size: 10,
        total: 0,
        params: {
          projectName: '',
          enterpriseName: '',
          createTimeStart: '',
          createTimeEnd: '',
          userSid: ''
        }
      },
      viewState: 1,
      sids: []
    }
  },
  mounted() {
    this.$refs['btnbar'].setButtonList(this.btnList)
  },
  created() {
    this.init()
  },
  methods: {
    btnHandle(btnKey) {
      switch (btnKey) {
        case 'toAdd':
          this.toAdd()
          break
        case 'doDel':
          this.doDel()
          break
        case 'toBan':
          this.toEdit()
          break
        case 'doClose':
          this.doClose()
          break
        default:
          break
      }
    },
    // 搜索条件效果
    clicksearchShow() {
      this.isSearchShow = !this.isSearchShow
      if (this.isSearchShow) {
        this.searchxianshitit = '隐藏查询条件'
      } else {
        this.searchxianshitit = '显示查询条件'
      }
    },
    // 初始化
    init() {
      this.getList()
    },
    // 序号
    indexMethod(index) {
      var pagestart = (this.listQuery.current - 1) * this.listQuery.size
      var pageindex = index + 1 + pagestart
      return pageindex
    },
    // 查询列表信息
    getList() {
      this.listLoading = true
      this.state = ''
      this.listQuery.params.userSid = window.sessionStorage.getItem('userSid')
      req.listPage(this.listQuery).then((res) => {
        if (res.success) {
          this.listLoading = false
          this.list = res.data.records
          this.listQuery.total = res.data.total
        }
      })
    },
    // 查询按钮
    handleReset() {
      this.listQuery.current = 1
      this.getList()
    },
    // 重置
    handleFilter() {
      this.listQuery = {
        current: 1,
        size: 10,
        total: 0,
        params: {
          projectName: '',
          enterpriseName: '',
          createTimeStart: '',
          createTimeEnd: '',
          userSid: ''
        }
      }
      this.getList()
    },
    closeIt() {
      this.editDialog = false
    },
    // 打开添加
    toAdd() {
      this.viewState = 2
      this.$refs['divAdd'].showAdd()
    },
    // 打开办理
    toEdit(row) {
      this.viewState = 3
      this.$refs['divAdd'].showEdit(row)
    },
    toInfo(row) {
      this.viewState = 4
      this.$refs['divInfo'].showInfo(row)
    },
    /** 流程流转记录 */
    handleFlowRecord(row) {
      this.getModelDetail(row.procDefId)
      this.getFlowViewer(row.procInstId)
      this.getFlowRecordList(row.procInstId, row.procDefId)
      this.editDialog = true
    },
    /** xml 文件 */
    getModelDetail(procDefId) {
      var token = getStorage()
      // 发送请求，获取xml
      readXml(procDefId, token).then(res => {
        this.xmlData = res.data
      })
    },
    // 已办环节的节点
    getFlowViewer(procInstId) {
      var token = getStorage()
      getFlowViewer(procInstId, token).then(res => {
        this.taskList = res.data
      })
    },
    /** 流程流转记录 */
    getFlowRecordList(procInstId, procDefId) {
      req.flowRecord(procInstId, procDefId).then(res => {
        this.flowRecordList = res.data.flowList
        // 流程过程中不存在初始化表单 直接读取的流程变量中存储的表单值
        //   if (res.data.formData) {
        //     this.formConf = res.data.formData
        //     this.formConfOpen = true
        //   }
      }).catch(res => {
        this.getList()
      })
    },
    handleSelectionChange(row) {
      const aa = []
      row.forEach((element) => {
        aa.push(element.sid)
      })
      this.sids = aa
    },
    // 根据本行ID删除数据
    doDel() {
      if (this.sids.length === 0) {
        this.$message({ showClose: true, type: 'error', message: '请选择至少一条记录进行删除操作' })
        return
      }
      const tip = '请确认是否删除所选 ' + this.sids.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.sids).then((resp) => {
          if (resp.success) {
            this.$message({ type: 'success', message: resp.msg, showClose: true })
          }
          this.getList()
          loading.close()
        }).catch(e => {
          loading.close()
        })
      }).catch(() => {
      })
    },
    resetState() {
      this.viewState = 1
    },
    doClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
/deep/ thead .el-table-column--selection .cell {
  display: none;
}
</style>
