<!--
 * @Description: 部门经理-审核采购单-订单审核
 * @Author: wxy
 * @routerPath:/procurement/procurementOrderAudit
 * @Date: 2020-10-14 14:46:16
-->
<template>
  <div v-loading="loading" :class="editStatus?'main':'p0-16'">
    <section class="title title_border">
      <i class="iconfont icon-fanhui pointer" @click="back()" />
      <!-- 02 待审核 -->
      <span v-if="orderInfo.orderStatus === '02'">订单审核 </span>
      <span v-else>订单查看</span>
    </section>
    <el-form ref="form" :model="formData" :rules="rules" :inline-message="true">
      <!-- 订单基本信息组件 -->
      <procurementOrderBase
        :order-info-base="orderInfo"
        :order-project-click="orderProjectClick"
        :order-supplier-click="orderSupplierClick"
        :is-edit="'00'"
      />
      <!-- 行项目列表 -->
      <procurementOrderLineItemComp
        ref="procurementOrderLineItemComp"
        :line-item-info-list="tenderMaterielInfoList"
        :line-item-other="lineItemOther"
      />
      <!-- 02 待审核 -->
      <p v-if="orderInfo.orderStatus === '02'" class="page-title">
        6.审核意见
      </p>
      <table v-if="orderInfo.orderStatus === '02'" class="tableData mb16">
        <tr class="singleRow">
          <td>
            <el-form-item label="审核意见" prop="auditNotPassReason" />
          </td>
          <td>
            <el-input
              v-model="formData.auditNotPassReason"
              :autosize="{ minRows: 4, maxRows: 4 }"
              type="textarea"
              maxlength="1000"
              show-word-limit
              style="width: 100%"
            />
          </td>
        </tr>
      </table>
      <!-- 02 待审核 -->
      <p v-if="orderInfo.orderStatus === '02'" class="page-title">
        7.审核消息
      </p>
      <p v-else class="page-title">
        6.审核消息
      </p>
      <section class="mb16">
        <!-- 审核信息组件 -->
        <procurementAuditList :audit-list="procurementSignUpAuditList" />
      </section>
    </el-form>
    <!--操作按钮-->
    <!-- 02 待审核 -->
    <div v-if="orderInfo.orderStatus === '02'" class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button type="primary" :loading="confirmLoading" @click="auditPassClick('01')">
            审核通过
          </el-button>
          <el-button :loading="confirmLoading" @click="auditNoPassClick('form','02')">
            审核不通过
          </el-button>
        </div>
      </div>
    </div>
    <!-- 查看采购单弹框 -->
    <orderProjectBase :dialog-data="orderProjectDialog" />
    <!-- 查看供应商报价弹框 -->
    <procurementSuplierOffer :dialog-data="orderSupplierDialog" />
  </div>
</template>

<script>
/* api接口*/
import {
  queryProcurementOrderInfo,
  listProcurementOrderProductPages,
  queryProcurementOrderAudit,
  auditProcurementOrderInfo
} from '@/api/procurement/procurementContract'
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'
import { commonMxin } from '@/mixins/commonMxin'
import { orderManagementGroupMixin } from '../mixins/orderManagementGroupMixins'
import { orderManagementMixins } from '../mixins/orderManagementMixins'
import { filterObj, filterDictText } from '@/assets/js/utils' // 深拷贝
import { characters1_auto } from '@/assets/js/validate'// 校验输入内容
import { moneyFormat } from '@assets/js/utils'// 常用方法
/* 组件*/
import procurementOrderBase from '../procurementOrderAdd/procurementOrderBaseComp' // 订单的基本信息
import procurementOrderLineItemComp from '../procurementOrderAdd/procurementOrderLineItemTableComp' // 订单行项目列表组件
import orderProjectBase from '../procurementOrderAdd/procurementOrderProjectBase-dialog' // 采购单查看弹框
import procurementSuplierOffer from '@/views/procurement/components/procurementLookSupplierOffer-dialog' // 查看供应商报价弹框
import procurementAuditList from '@/views/procurement/components/procurementAuditComp/procurementAuditComp-detail' // 审核信息公共组件

export default {
  name: 'OrderManagementDetail',
  components: {
    orderProjectBase, // 采购单查看弹框
    procurementOrderBase, // 订单的基本信息
    procurementAuditList, // 审核信息公共组件
    procurementSuplierOffer, // 查看供应商报价弹框
    procurementOrderLineItemComp
  },
  mixins: [
    ListMixin,
    commonMxin,
    orderManagementGroupMixin,
    orderManagementMixins
  ],
  data() {
    return {
      moneyFormat: moneyFormat, // 金额添加千分位
      loading: true, // loading
      confirmLoading: false, // 提交按钮状态
      formData: {
        auditNotPassReason: '', // 审核意见
        auditStatus: '', // 审核状态
        orderIds: this.$route.params.orderId // 订单id
      },
      // 采购单查看弹框
      orderProjectDialog: {
        dialog: false,
        tenderId: '' // 项目id
      },
      // 订单基本信息弹框信息
      orderSupplierDialog: {
        dialog: false,
        stageId: '', // 轮次id
        packId: '', // 标段id
        supplierId: '' // 供应商id
      },
      // 行项目的其他数据
      lineItemOther: {
        currency: '', // 币种
        isEdit: '00' // 是否可编辑 01 是 00 否
      },
      procurementSignUpAuditList: [], // 审核消息列表
      tenderMaterielInfoList: [], // 行项目信息
      orderInfo: {}, // 订单基本信息
      // 行项目列表请求参数
      searchForm: {
        isOperation: '', // 业务操作
        orderBy: 0, // 排序
        orderId: this.$route.params.orderId // 订单id
      },
      // 校验规则
      rules: {
        auditNotPassReason: [{ required: true, validator: characters1_auto, trigger: 'blur' }] // 详细地址
      }
    }
  },
  created() {
    // 查询订单数据字典信息
    this.getProcurementOderInfo()
    // 获取订单详情信息
    this.getOrderInfo()
    // 行项目信息
    this.getMaterialInfo()
  },
  methods: {
    filterDictText, // 引入字典值替换文本通用方法
    /*
    审核通过
    status: 01 审核通过 02审核不通过
    */
    auditPassClick(status) {
      this.$confirm('确认审核通过吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn'
      })
        .then(() => {
          this.formData.auditStatus = status
          this.confirmLoading = true
          // 调用提交订单审核信息
          this.saveAuditProcurementOrderInfo(this.formData)
        })
        .catch(() => {})
    },
    /**
     * 审核不通过
     * formName校验内容
     * status审核字段 01 通过 02审核不通过
     */
    auditNoPassClick(formName, status) {
      this.$refs[formName].validate((valid, field) => {
        if (valid) {
          this.$confirm('确认审核不通过吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            confirmButtonClass: 'confirmBtn',
            cancelButtonClass: 'cancelBtn'
          })
            .then(() => {
              this.confirmLoading = true
              this.formData.auditStatus = status
              // 调用提交订单审核信息
              this.saveAuditProcurementOrderInfo(this.formData)
            })
            .catch(() => {})
          return
        }
        this.$message.warning({ message: '请注意页面提示！' })
      })
    },
    /*
     订单查看采购单事件
     tenderId 项目id
    */
    orderProjectClick(tenderId) {
      // 获取项目id
      this.orderProjectDialog.tenderId = tenderId
      // 打开采购单查看弹框
      this.orderProjectDialog.dialog = true
    },
    /*
     订单查看报价信息事件
     supplierClickType 布尔值 true表示点击了报价单号
    */
    orderSupplierClick(supplierClickType) {
      // 点击查看报价单返回true
      if (supplierClickType) {
        // 获取轮次id
        this.orderSupplierDialog.stageId = this.orderInfo.stageId
        // 获取标段id
        this.orderSupplierDialog.packId = this.orderInfo.packId
        // 获取供应商id
        this.orderSupplierDialog.supplierId = this.orderInfo.winningDepartId
        // 开启弹框
        this.orderSupplierDialog.dialog = true
      }
    },
    // 返回按钮
    back() {
      this.$router.push({ path: '/procurement/procurementOrderAuditList' })
    },
    /*
     获取订单详情信息请求
     入参：订单id列表跳转获取
    */
    getOrderInfo() {
      queryProcurementOrderInfo(this.$route.params.orderId)
        .then(res => {
          this.orderInfo = {
            ...res.data,
            // 订单条款默认空前端赋值
            orderTerms: res.data.orderTerms ? res.data.orderTerms : this.orderClauseContent
          }
          this.orderInfo.orderStatus = this.$route.params.orderStatus // 订单状态
          this.lineItemOther.currency = this.orderInfo.currency // 币种
          // 获取审核意见列表
          this.initAuditList()
        })
        .catch(() => {})
    },
    // 行项目查询条件
    getQueryParams() {
      const param = Object.assign({}, this.searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)
    },
    /*
     获取行项目信息请求
     入参: params搜索条件 data有注释
    */
    getMaterialInfo() {
      // 查询条件
      const params = this.getQueryParams()
      listProcurementOrderProductPages(params)
        .then(res => {
          this.tenderMaterielInfoList = res.data.records // 获取行项目列表
          this.paginationData.total = res.data.total // 总条数
        })
        .catch(() => {})
    },
    /*
     获取审核意见列表请求
     入参：订单id 订单列表跳转获取
    */
    initAuditList() {
      // 获取订单id
      const orderId = this.$route.params.orderId
      if (orderId) {
        queryProcurementOrderAudit(orderId).then(res => {
          this.procurementSignUpAuditList = res.data // 获取订单审核列表
          this.loading = false
        })
        return
      }
      this.$message.warning({
        message: '订单详情id为空'
      })
    },
    /*
     提交订单审核信息方法
     auditDate 审核数据
    */
    saveAuditProcurementOrderInfo(auditDate) {
      auditProcurementOrderInfo(auditDate).then(res => {
        this.confirmLoading = false
        this.$router.push({
          path: '/procurement/procurementOrderAuditList'
        })
      })
    },
    // 将文本域的文本进行处理让文本保存换行空格形式，传递给后端
    preText(pretext) {
      return pretext
        .replace(/\r\n/g, '<br/>')
    }
  }
}
</script>

<style lang="less" scoped>
@import url("~@/assets/style/common/public.less");
.offerInfo{
  .tip-size;
}
</style>

