<!--
  @description: 收货订单-订单确认 订单-订单计划退回处理
  @author: 王俊楠
  @routerPath:/procurement/procurementOrderConfirmDetail
  @date： 2020-10-30 13:50:12
-->
<template>
  <div class="p0-16">
    <section v-if="!flowStatus" class="title title_border">
      <i class="iconfont icon-fanhui pointer" @click="back()" />
      <span v-if="pageType === '01'">订单计划退回处理</span>
      <span v-else>待处理订单确认</span>
    </section>
    <!-- 订单基本信息 -->
    <procurementOrderBasicCompDetail
      :order-info-data="detailInfoData.orderInfoList"
    />
    <!-- 行项目信息 -->
    <template>
      <procurementOrderLineItemCompDetail
        ref="orderLineItem"
        :line-item-info-data="detailInfoData.lineItemInfoList"
        :line-data="lineData"
        :page-type="pageType"
        :operation-type="operationType"
      />
      <pagination :pagination-data="paginationData" @change="loadData" />
    </template>
    <el-form v-if="pageType === '01'" ref="opinionForm" :model="returnLineItemData" :rules="returnLineItemRules">
      <table class="from_table">
        <tr>
          <td>
            <el-form-item label="退回意见" prop="opinion" />
          </td>
          <td colspan="3">
            <el-input
              v-model="returnLineItemData.opinion"
              :rows="4"
              type="textarea"
              placeholder="请填写退回意见"
              maxlength="200"
              show-word-limit
            />
          </td>
        </tr>
      </table>
    </el-form>
    <!--操作按钮-->
    <div class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button v-if="pageType === '01'" :loading="confirmLoading" type="primary" @click.stop="returnLineItem('opinionForm')">
            确认退回
          </el-button>
          <el-button v-else :loading="confirmLoading" type="primary" @click.stop="sureOrder()">
            确认订单
          </el-button>
        </div>
      </div>
    </div>
    <!--工作流选择节点组件-->
    <processNodeCom
      ref="processNode"
      :process-definition-key="processDefinitionKey"
      @processNodeData="processNodeData"
    />
    <!--工作流流程节点/获取审核用户-->
    <departDialog
      ref="departDialog"
      :process-node="processNode"
      @isSingleUser="isSingleUser"
      @processInitData="processInitData"
      @submit="submitFlow"
    />
  </div>
</template>

<script>
/* api接口*/
import { submitReleaseProcurementOrderProduct } from '@/api/procurement/procurementContract'
import {
  queryProcurementOrderInfo, // 基本信息
  listProcurementOrderProductPages, // 行项目
  pushToConfirmedOrder // 推送待确认
} from '@/api/procurement/procurementOrder'
/* 公用方法*/
import { characters1_auto } from '@/assets/js/validate'// 校验输入内容
import { orderWormMixins } from '../mixins/orderWormMixins' // 工作流配置
/* 组件 */
import procurementOrderBasicCompDetail from '@/views/procurement/procurementOrder/procurementOrderInfo/procurementOrderBasicComp-detail.vue' // 订单的基本信息组件
import procurementOrderLineItemCompDetail from '@/views/procurement/procurementOrder/procurementOrderInfo/procurementOrderLineItemComp-detail.vue' // 订单行项目信息列表组件
/*  公共*/
import { ListMixin } from '@/mixins/ListMixin' // 分页
import pagination from '@comp/common/pagination/pagination' // 分页组件
/* ==========工作流===========*/
import { workCommonMixins } from '@views/work/mixins/workCommonMixins' // 工作流公共配置
import workconstant from '@/assets/js/workutils/workconstant/purOrder' // 配置流程key
import processNodeCom from '@views/work/processNodeCom' // 工作流选择节点组件
import departDialog from '@views/work/depart-dialog' // 选择审核人员
/* ============================*/
export default {
  name: 'ProcurementOrderInfoCompDetail',
  components: {
    procurementOrderBasicCompDetail, // 订单基本信息
    procurementOrderLineItemCompDetail, // 行项目列表组件
    pagination, // 分页
    processNodeCom,
    departDialog
  },
  mixins: [ListMixin, workCommonMixins, orderWormMixins],
  data() {
    return {
      flowStatus: this.$route.query.flowStatus, // 是否工作流
      pageType: this.$route.meta.typeCode, // 00 订单确认页 01 订单行项目退回页
      operationType: '00', // 是否审批
      returnLineItemData: {
        opinion: '' // 意见
      },
      returnLineItemRules: {
        opinion: [{ required: true, validator: characters1_auto, trigger: 'blur' }] // 意见
      },
      // 页面数据
      detailInfoData: {
        orderInfoList: {}, // 订单基本信息
        lineItemInfoList: [] // 行项目信息
      },
      lineData: {
        orderTotalMoney: '', // 订单净总价
        orderTotalTaxMoney: '', // 订单含税总价
        currency: '' // 币种
      },
      processDefinitionKey: '', // 工作流流程key
      orderId: '', // 获取路由传参 订单Id
      confirmLoading: false, // 确认按钮加载
      purchaseUnitList: [], // 计量单位字典列表
      typeList: [] // 类别
    }
  },
  created() {
    if (this.flowStatus) {
      this.orderId = this.$route.query.bizId
      this.operationType = '01'
    } else {
      this.orderId = this.$route.params.orderId
      this.operationType = '00'
    }
    this.procurementOrderPushAudit() // 推送确认获取工作流配置
    this.releaseAuditConfig() // 提交释放或退回获取工作流配置
    this.initPage()
    this.getBaseInfoData() // 基础信息
    this.loadData() // 行项目信息
  },
  methods: {
    initPage() {
      // 确认订单
      if (this.pageType === '00') {
        this.processDefinitionKey = workconstant.orderFormConfirm // 工作流流程key
      } else if (this.pageType === '01') {
      // 订单行项目退回
        this.processDefinitionKey = workconstant.orderFormBack // 工作流流程key
      }
    },
    // 返回收货订单处理页面
    back() {
      // 确认订单
      if (this.pageType === '00') {
        this.$router.push({
          path: '/procurement/procurementOrderReceiveList'
        })
      } else if (this.pageType === '01') {
      // 订单行项目退回
        this.$router.push({
          path: '/procurement/procurementOrderQueryList'
        })
      }
    },
    // 确认订单
    sureOrder() {
      this.$confirm('确认订单吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        // 判断是否进入工作流
        if (this.procurementOrderPushAuditConfig) {
          // 判断审核人员是否是多个
          if (JSON.stringify(this.processNode) !== '{}' && this.processNode.nodeId) {
            this.$refs.departDialog.getUserDepart(this.processNode)
          }
        } else {
          // 直接提交
          this.submitFlow()
        }
      })
      this.confirmLoading = false
    },
    // 审批流提交
    submitFlow() {
      // 确认订单
      if (this.pageType === '00') {
        this.pushToConfirmed()
      } else if (this.pageType === '01') {
        // 订单行项目退回
        this.lineItemReturn()
      }
    },
    // 确认订单
    async pushToConfirmed() {
      const params = {
        orderId: this.orderId
        // processDefinitionKey: this.processDefinitionKey, // 工作流节点key
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      // 判断是否进入工作流
      if (this.procurementOrderPushAuditConfig) {
        params.processDefinitionKey = this.processDefinitionKey // 工作流节点key
        params.processInitDto = this.processInitDto // 审核人信息传回得信息
      }
      pushToConfirmedOrder(params).then(() => {
        this.back()
      })
    },
    // 计划退回
    async lineItemReturn() {
      const params = {
        ids: this.$refs.orderLineItem.selectedItem().join(','),
        reason: this.returnLineItemData.opinion,
        orderId: this.orderId
        // processInitDto: this.processInitDto // 审核人信息传回得信息
      }
      // 判断是否进入工作流
      if (this.releaseAuditConfigValue) {
        params.processInitDto = this.processInitDto // 审核人信息传回得信息
      }
      submitReleaseProcurementOrderProduct(params).then(() => {
        this.back()
      })
    },
    // 获取行项目信息
    loadData() {
      const params = {
        orderId: this.orderId,
        pageNo: this.paginationData.pageNo,
        pageSize: this.paginationData.pageSize
      }
      if (this.pageType === '01') {
        params.releaseFlag = '02'
      }
      listProcurementOrderProductPages(params).then(response => {
        this.loading = false
        this.detailInfoData.lineItemInfoList = response.data.records
        this.paginationData.total = response.data.total
      }).catch(() => {
      })
    },
    // 基本信息
    getBaseInfoData() {
      const params = {
        orderId: this.orderId
      }
      queryProcurementOrderInfo(params).then((response) => {
        this.detailInfoData.orderInfoList = response.data
        this.lineData.currency = response.data.currency // 币种
        this.lineData.orderTotalTaxMoney = response.data.orderTotalTaxMoney // 订单含税总价
        this.lineData.orderTotalMoney = response.data.orderTotalMoney // 订单净总价
      })
    },
    // 行项目选择校验
    valiItem() {
      const selectedItem = this.$refs.orderLineItem.selectedItem()
      if (selectedItem.length < 1) {
        this.$message({
          message: '请选择要退回的行项目',
          type: 'warning'
        })
        return false
      }
      return true
    },
    // 行项目退回
    returnLineItem(formName) {
      this.valiItem() && this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('确认退回？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn',
            type: 'warning'
          }).then(() => {
            // 判断是否进入工作流
            if (this.releaseAuditConfigValue) {
              // 判断审核人员是否是多个
              if (JSON.stringify(this.processNode) !== '{}' && this.processNode.nodeId) {
                this.$refs.departDialog.getUserDepart(this.processNode)
              }
            } else {
              this.valiItem() && this.lineItemReturn()
            }
          })
        } else {
          this.$message({
            message: '注意页面提示',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
