<!-- 订单 -->
<template>
  <div class="order">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
      <el-form-item label="状态">
        <el-select
          v-model="dataForm.state"
          placeholder="请选择">
         <el-option
           v-for="item in optionsState"
           :key="item.code"
           :label="item.value"
           :value="item.code">
         </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="申请日期">
        <el-date-picker
          v-model="dataForm.createTime"
          value-format="yyyy-MM-dd"
          type="date">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryData()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center">
      </el-table-column>
      <el-table-column
        prop="businessTypeName"
        header-align="center"
        align="center"
        min-width="180"
        label="流程类别">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        min-width="140"
        :show-overflow-tooltip="true"
        label="申请时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        min-width="140"
        label="更新时间">
      </el-table-column>
      <el-table-column
        prop="state"
        header-align="center"
        align="center"
        min-width="100"
        label="状态">
        <template slot-scope="scope">
          <span>{{ optionsState[Number(scope.row.state) + 1].value  }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="creatorName"
        header-align="center"
        align="center"
        min-width="120"
        label="申请人">
      </el-table-column>
      <el-table-column
        prop="orgName"
        header-align="center"
        align="center"
        label="申请部门">
      </el-table-column>
      <el-table-column
        prop="completeDate"
        header-align="center"
        align="center"
        min-width="150"
        label="处理意见">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="80"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="Number(scope.row.state) !== 3 && checkUser(scope.row)" type="text" size="small" @click="addOrUpdateHandle(scope.row.businessType, scope.row.businessId)">处理</el-button>
          <el-button v-else type="text" size="small" @click="detailHandle(scope.row.businessType, scope.row.businessId)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <detail v-if="detailVisible" ref="detail"/>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getApprovalList"/>
  </div>
</template>

<script>
  import Detail from './approve-detail'
  import AddOrUpdate from './approve-add-or-update'
  import { getApprovalList } from '@/api/msg'
  import { getCusList } from '@/api/cus'
  import { checkStr } from '@/api/util'
  export default {
    name: 'order',
    components: {
      Detail,
      AddOrUpdate
    },
    created () {
      this.queryData()
    },
    data () {
      return {
        addOrUpdateVisible: false,
        detailVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsState: [
          {
            code: null, value: '全部'
          },
          {
            code: '0', value: '待提交'
          },
          {
            code: '1', value: '待审批'
          },
          {
            code: '2', value: '审批中'
          },
          {
            code: '3', value: '审批完成'
          },
          {
            code: '4', value: '审批不通过'
          }
        ],
        optionsCustomer: []
      }
    },
    methods: {
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getApprovalList()
      },
      // 获取数据列表
      getApprovalList () {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'createTime': this.dataForm.createTime ? this.dataForm.createTime : null,
          'state': this.dataForm.state ? this.dataForm.state : null
        }
        getApprovalList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getApprovalList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getApprovalList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 远程方法：获取客户列表
      async remoteCustomer (query) {
        if (!query) {
          query = ''
        }
        await getCusList({'customerName': query}).then(({data}) => {
          if (data && data.code === '200') {
            this.optionsCustomer = []
            data.data.records.forEach((item) => {
              this.optionsCustomer.push({
                code: item.customerId,
                value: item.customerName
              })
            })
          }
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (businessType, businessId) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(businessType, businessId)
        })
      },
      checkUser (row) {
        let currentUser = this.$store.state.user.name
        return checkStr(currentUser, row.approver)
      },
      // 详情
      detailHandle (businessType, businessId) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(businessType, businessId)
        })
      }
    }
  }
</script>

<style scoped>

</style>
