<!-- 订单 -->
<template>
  <div class="order">
    <template v-if="!addOrUpdateVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
        <el-form-item label="业务类型">
          <el-select
            v-model="dataForm.businessType"
            placeholder="请选择">
            <el-option
              v-for="item in optionsType"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <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="160"
          :show-overflow-tooltip="true"
          label="申请时间">
        </el-table-column>
        <el-table-column
          prop="updateTime"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="更新时间">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          min-width="100"
          label="状态">
          <template slot-scope="scope">
            <span>{{(optionsState.find(t=>t.code === scope.row.state.toString())).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"
          min-width="140"
          :show-tooltip-when-overflow="true"
          label="申请部门">
        </el-table-column>
        <el-table-column
          fixed="right"
          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)">处理</el-button>
            <el-button v-else type="text" size="small" @click="detailHandle(scope.row)">查看</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>
    </template>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getApprovalList" @onChose="onChose"/>
  </div>
</template>

<script>
  import AddOrUpdate from './approve-add-or-update'
  import { getApprovalList } from '@/api/msg'
  import { readNotices } from '@/utils/msg'
  import { getCusList } from '@/api/cus'
  import { checkStr } from '@/api/util'
  export default {
    name: 'order',
    components: {
      // Detail,
      AddOrUpdate
    },
    created () {
      this.optionsState = this.$store.state.common.approveStates
      this.queryData()
    },
    computed: {
      // 监听消息ID
      listenNotice () {
        return this.$route.params.notice
      }
    },
    watch: {
      listenNotice: function (notice) {
        // console.log('notice = ' + JSON.stringify(notice))
        if (!notice) return
        this.dataForm.noticeId = notice.noticeId
        this.dataForm.businessId = notice.businessId
        this.queryData()
      }
    },
    data () {
      return {
        addOrUpdateVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        optionsState: [],
        optionsType: [
          {
            code: null, value: '全部'
          },
          {
            code: 'warehouse_flow_in', value: '入库申请'
          },
          {
            code: 'warehouse_flow_out', value: '出库申请'
          },
          {
            code: 'contract_review', value: '合同评审'
          },
          {
            code: 'sale_order_flow', value: '订单评审'
          },
          {
            code: 'sale_purchase_flow', value: '采购申请'
          },
          {
            code: 'wh_template_record_flow', value: '模板出库'
          },
          {
            code: 'pro_technology_flow', value: '工艺评审'
          },
          {
            code: 'pro_product_flow', value: '产品评审'
          },
          {
            code: 'wh_inventory_record_flow', value: '盘点评审'
          },
          {
            code: 'supplier_review_flow', value: '供应商评审'
          },
          {
            code: 'submit_expense_flow', value: '报销审批'
          },
          {
            code: 'pur_commission_flow', value: '委外审批'
          }
        ],
        optionsCustomer: []
      }
    },
    methods: {
      onChose () {
        this.addOrUpdateVisible = false
      },
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getApprovalList()
      },
      // 获取数据列表
      getApprovalList () {
        this.dataList = []
        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,
          'businessType': this.dataForm.businessType ? this.dataForm.businessType : null,
          'businessId': this.dataForm.businessId ? this.dataForm.businessId : 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
              })
            })
          }
        })
      },
      checkUser (row) {
        let currentUser = this.$store.state.user.id
        return checkStr(currentUser, row.currentApprover)
      },
      // 新增 / 修改
      addOrUpdateHandle (row) {
        this.addOrUpdateVisible = true
        // 已查看
        if (row.noticeIds) {
          readNotices(this, row.noticeIds)
        }
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.businessType, row.businessId)
        })
      },
      // 详情
      detailHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row.businessType, row.businessId, true)
        })
        // 已查看
        if (row.noticeIds) {
          readNotices(this, row.noticeIds)
        }
      }
    }
  }
</script>

<style scoped>

</style>
