<!--
  @description: 订单管理-查询
  @author: lxl
  @routerPath: /procurement/procurementOrderQueryList
  @date： 2021-1-8 10:30:12
-->
<template>
  <div>
    <table-crud
      ref="tableCrud"
      :menu="true"
      :tabs="tabs"
      :search-form.sync="searchForm"
      search-label-width="100px"
      @on-load="loadData"
    >
      <!--标题-->
      <template v-slot:header>
        采购订单管理
      </template>
      <template v-slot:search="search">
        <el-form
          ref="searchForm"
          :model="searchForm"
          label-width="108px"
          label-position="left"
          inline
        >
          <!--订单编号-->
          <el-form-item ref="orderCode" label="订单编号" prop="orderCode">
            <el-input
              v-model.trim="searchForm.orderCode"
              placeholder="订单编号"
              @keyup.enter.native="search.onSearch"
            />
          </el-form-item>
          <!--采购单编号-->
          <el-form-item ref="tenderNo" label="采购单编号" prop="tenderNo">
            <el-input
              v-model.trim="searchForm.tenderNo"
              placeholder="采购单编号"
              @keyup.enter.native="search.tenderNo"
            />
          </el-form-item>
          <!--订单名称-->
          <el-form-item ref="orderName" label="订单名称" prop="orderName">
            <el-input
              v-model.trim="searchForm.orderName"
              placeholder="订单名称"
              @keyup.enter.native="search.onSearch"
            />
          </el-form-item>
          <!--供应商名称-->
          <el-form-item
            ref="winningDepartName"
            label="供应商名称"
            class="el-form-item--three"
            prop="winningDepartName"
          >
            <el-input
              v-model.trim="searchForm.winningDepartName"
              placeholder="供应商名称"
              @keyup.enter.native="search.onSearch"
            />
          </el-form-item>
          <!--采购单位-->
          <el-form-item
            ref="purchaseDepartName"
            label="采购单位"
            class="el-form-item--three"
            prop="purchaseDepartName"
          >
            <el-input
              v-model.trim="searchForm.purchaseDepartName"
              placeholder="采购单位"
              @keyup.enter.native="onSearch"
            />
          </el-form-item>
          <!--SAP推送状态-->
          <el-form-item
            v-show="searchForm.orderStatus === '99'"
            ref="sapSyncStatus"
            label="SAP推送状态"
            prop="sapSyncStatus"
          >
            <el-select v-model="searchForm.sapSyncStatus" placeholder="请选择">
              <el-option
                v-for="item in syncStatusList"
                :key="item.typecode"
                :label="item.typename"
                :value="item.typecode"
              />
            </el-select>
          </el-form-item>
          <!-- 订单总额 -->
          <el-form-item prop="min" label="订单总额(元)">
            <el-input v-model="searchForm.orderTotalMoney_begin" type="number" style="width: 12%" />
            ~
            <el-input v-model="searchForm.orderTotalMoney_end" type="number" style="width: 12%" />
          </el-form-item>
          <!--创建日期-->
          <el-form-item label="创建日期">
            <el-date-picker
              v-model="searchForm.datePicker"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 220px"
              @change="handleCreateTime"
            />
          </el-form-item>
        </el-form>
      </template>
      <template v-slot:GXTable>
        <procOrderQuePendingComp v-if="searchForm.orderStatus==='00'||searchForm.orderStatus==='01'" :table-data="tableData" :proc-result="procResult">
          <template slot-scope="scope">
            <!-- 00 待确认 01 待审核 02 进行中 03 已取消 -->
            <el-link
              type="primary"
              @click.stop="navOrderDetail(scope.row)"
            >
              [详情]
            </el-link>
            <el-link
              v-if="searchForm.orderStatus === '01'"
              type="primary"
              @click="handleFlow(scope.row)"
            >
              [流程追踪]
            </el-link>
            <template v-if="searchForm.orderStatus==='99'">
              <el-link
                v-if="scope.row.syncStatus==='1'"
                type="primary"
                @click.stop="onCompile(scope.row)"
              >
                [修改]
              </el-link>
              <el-link
                v-if="scope.row.syncStatus==='1'"
                type="primary"
                @click.stop="anewHandle(scope.row)"
              >
                [重新推送]
              </el-link>
              <el-link
                type="primary"
              >
                [日志]
              </el-link>
            </template>
          </template>
        </procOrderQuePendingComp>
        <procOrderQueFinishComp v-if="searchForm.orderStatus==='02'||searchForm.orderStatus==='99'||searchForm.orderStatus==='10'||searchForm.orderStatus==='08' || searchForm.orderStatus==='11'" :table-data="tableData" :proc-result="procResult" :search-form="searchForm">
          <template slot-scope="scope">
            <!-- 00 待确认 01 待审核 02 进行中 03 已取消 -->
            <el-link
              type="primary"
              @click.stop="navOrderDetail(scope.row)"
            >
              [详情]
            </el-link>
            <el-link
              v-if="searchForm.orderStatus === '10'&&scope.row.orderStatus !== '10'"
              type="primary"
              @click.stop="confirmOrder(scope.row)"
            >
              [计划退回]
            </el-link>
            <el-link
              v-if="searchForm.orderStatus === '02'"
              type="primary"
              @click="handleFlow(scope.row)"
            >
              [流程追踪]
            </el-link>
            <template v-if="searchForm.orderStatus==='99'">
              <template v-if="roleCode==='11'">
                <!--         <el-link
                  v-if="scope.row.syncStatus==='1'"
                  type="primary"
                  @click.stop="onCompile(scope.row)"
                >
                  [修改]
                </el-link>-->
                <el-link
                  v-if="scope.row.syncStatus==='1'"
                  type="primary"
                  @click.stop="anewHandle(scope.row)"
                >
                  [重新推送]
                </el-link>
              </template>
              <el-link
                type="primary"
                @click="openOrderLog(scope.row.id)"
              >
                [日志]
              </el-link>
            </template>
          </template>
        </procOrderQueFinishComp>
        <orderAllTable v-if="searchForm.orderStatus==='22'" :proc-result="procResult" :table-data="tableData">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click.stop="navOrderDetail(scope.row)"
            >
              [详情]
            </el-link>
            <!-- <el-link
              type="primary"
              @click="handleFlow(scope.row)"
            >
              [流程追踪]
            </el-link> -->
          </template>
        </orderAllTable>
      </template>
    </table-crud>
    <!-- 采购单编号弹窗 -->
    <procurementResultDialog :dialog-data="procResultData" />
    <!-- 订单日志 -->
    <orderLog ref="orderLog" />
    <!--流程追踪查看弹框-->
    <flow-detail-dialog ref="tracingFlow" />
  </div>
</template>

<script>
/* api接口*/
import {
  listProcurementOrderPages,
  listProcurementSAPOrderPages, // sap列表
  handMovementPushSAPOrderCenter, // 重新推送
  listProcurementOrderReleasePages, // 订单退回记录列表
  listWaitReleaseProcurementOrderPages, // 订单退回列表
  getPageOrderInfoAll // 全部
} from '@/api/procurement/procurementOrder'
/* 公共方法 */
import { filterDictText, moneyFormat } from '@/assets/js/utils' // 深拷贝
import { procurementOrderGroupMixins } from '@/views/procurement/procurementOrder/mixins/procurementOrderGroupMixins.js'
/* 正则 */
import { checkRate } from '@/assets/js/validate'// 校验输入内容
/* 组件 */
import procurementResultDialog from './procurementResult-dialog' // 采购单编号弹窗
import procOrderQuePendingComp from './procOrderQuePendingComp.vue' // 待确认待审核组件
import procOrderQueFinishComp from './procOrderQueFinishComp.vue' // 待确认待审核表格组件
import orderLog from '../components/procurementOrderLog-dialog.vue' // 订单日志
import flowDetailDialog from '@views/work/process/flowDetail-dialog' //  流程追踪组件
import orderAllTable from './orderAllTable.vue' // 全部
export default {
  name: 'ProcurementOrderQueryList',
  components: {
    procurementResultDialog, // 采购单编号弹窗
    procOrderQuePendingComp, // 待确认待审核表格组件
    procOrderQueFinishComp, // 已生效已推送sap订单表格组件
    orderLog,
    flowDetailDialog, // 流程追踪
    orderAllTable // 全部
  },
  mixins: [procurementOrderGroupMixins],
  props: {
    // 订单基本信息 不可编辑的
    orderInfoBase: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      roleCode: '', // 角色编码
      createTime: '',
      searchForm: {
        tenderNo: '', // 采购单编号
        datePicker: [], // 选择开始时间结束时间
        orderTotalMoney_begin: '', // 订单总额开始
        orderTotalMoney_end: '', // 订单总额结束
        // tenderMethod: '', // 生成方式
        orderStatus: '00', // 订单状态
        orderCode: '', // 订单编号
        orderName: '', // 订单名称
        winningDepartName: '', // 供应商名称
        purchaseDepartName: '', // 创建人
        createTime_begin: '', // 创建日期
        createTime_end: '', // 结束日期
        purchaseDepartExecuteName: '' // 代理单位
      },
      tabs: [
        { index: '00', name: '待确认' },
        { index: '01', name: '待审核' },
        { index: '02', name: '已生效订单' },
        { index: '99', name: '推送sap订单' },
        { index: '08', name: '已作废订单' },
        { index: '11', name: '订单退回记录' },
        { index: '10', name: '订单退回' },
        { index: '22', name: '全部' }
      ],
      confirmLoading: false, // 提交按钮loading
      // 校验规则
      rules: {
        receiptNum: [
          { required: true, validator: checkRate, trigger: 'blur' }
        ] // 收货数量
      },
      loading: false,
      tableData: [],
      procResultData: { // 采购单编号弹窗数据
        dialog: false,
        projectData: {}
      }
    }
  },
  created() {
    this.handleTabShow()
  },
  methods: {
    filterDictText, // 字典值替换文本通用方法
    moneyFormat,
    // 处理订单退回显示 只有采购员显示
    handleTabShow() {
      this.roleCode = this.$store.getters.info.currentRol.roleCode
      if (this.roleCode !== '11') {
        this.tabs = this.tabs.filter(i => i.index !== '10')
      }
    },
    // 获取数据
    loadData(params, callback, tabData) {
      if (tabData) {
        params.orderStatus = tabData.index
        this.searchForm.orderStatus = tabData.index // tabs数据
      }
      if (this.searchForm.orderStatus === '99') {
        listProcurementSAPOrderPages(params).then((response) => {
          this.tableData = response.data.records
          callback(response.data.total)
        }).catch(() => {
        }).finally(() => {
          this.loading = false
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
        })
      } else if (this.searchForm.orderStatus === '11') {
        listProcurementOrderReleasePages(params).then((response) => {
          this.tableData = response.data.records
          callback(response.data.total)
        }).catch(() => {
        }).finally(() => {
          this.loading = false
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
        })
      } else if (this.searchForm.orderStatus === '10') {
        listWaitReleaseProcurementOrderPages(params).then((response) => {
          this.tableData = response.data.records
          callback(response.data.total)
        }).catch(() => {
        }).finally(() => {
          this.loading = false
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
        })
      } else if (this.searchForm.orderStatus === '22') {
        getPageOrderInfoAll(params).then((response) => {
          this.tableData = response.data.records
          callback(response.data.total)
        }).catch(() => {
        }).finally(() => {
          this.loading = false
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
        })
      } else {
        listProcurementOrderPages(params).then(response => {
          this.tableData = response.data.records
          callback(response.data.total)
        }).catch(() => {
        }).finally(() => {
          this.loading = false
          this.searchForm.column = ''
          this.searchForm.orderSort = ''
        })
      }
    },
    // 采购单编号弹窗
    procResult(row) {
      this.procResultData.dialog = true
      this.procResultData.projectData = row
    },
    // 跳转收货订单详情
    navOrderDetail(row) {
      if (this.searchForm.orderStatus === '11') {
        this.$router.push({
          name: 'orderBackHistoryDetail',
          params: {
            orderId: row.orderId,
            bizId: row.bizid,
            id: row.id
            // orderStatus: this.searchForm.orderStatus
          }
        })
      } else {
        this.$router.push({
          name: 'procurementOrderQueryDetail',
          params: {
            orderId: row.id,
            orderStatus: this.searchForm.orderStatus
          }
        })
      }
    },
    // 修改
    onCompile(row) {
      this.$router.push({
        name: 'lineItemUp',
        params: {
          orderId: row.id
        }
      })
    },
    // 重新推送
    anewHandle(row) {
      this.$confirm('您确认重新推送？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        const params = {
          orderId: row.id
        }
        handMovementPushSAPOrderCenter(params).then((res) => {
          this.$message.success(res.header.msg)
          this.$refs.tableCrud.onSearchChange()
        }).catch((error) => {
          this.$message.warning(error)
        })
      }).catch(() => {})
    },
    // 打开订单日志
    openOrderLog(id) {
      this.$refs.orderLog.openDialogInitData(id)
    },
    //  流程追踪
    handleFlow(row) {
      this.$refs.tracingFlow.openDialogInitData(row.id)
    },
    // 处理搜索时间格式
    handleCreateTime(val) {
      if (val) {
        this.searchForm.createTime_begin = this.$Moment(val[0]).format(this.momentDate)// 创建开始时间
        this.searchForm.createTime_end = this.$Moment(val[1]).format(this.momentDate)// 创建结束时间
      }
    },
    // 项目退回
    confirmOrder(row) {
      this.$router.push({
        name: 'procurementOrderLineItemReturn',
        params: {
          orderId: row.id // 订单id
        }
      })
    }
  }
}
</script>

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

