<script>
import {
  getRoutingTreeStructure, searchQuoteRoutingAlternativeNo, searchQuoteRoutingDetailList,
  searchQuoteRoutingList,
  updateQuoteRoutingList
} from "../../../../../api/quotation/quoteOfRouting";
import {
  searchQuoteBOMAlternativeNo,
  searchQuoteBomList,
  searchQuoteBOMVersion
} from "../../../../../api/quotation/quoteOfBom";
import {Decimal} from "decimal.js";

export default {
  name: "quoteRouting",
  props:{
    detail:{
      type:Object,
      request:true,
    },
    height:{
      type:Number,
      default:400,
    },
    loadingStatus:{
      type:Boolean,
      default:false,
    },
    allSearchFlag:{
      type:Boolean,
      default:false,
    },
  },
  computed:{
    RoutingAllSearchFlag:{
      get(){
        return this.allSearchFlag;
      },
      set(val){
        this.$emit("update:allSearchFlag",val)
      }
    }
  },
  data(){
    return{
      dataListLoading:false,
      routingAlternativeList:[],
      routingVersionList:[],
      handoffVersion:false,
      treeLoading:false,
      routingTreeStructure:[],
      routingProps:{
        label:'label',
        value:'value',
        children:'list'
      },
      selectRouting:[],
      routingDetailList:[],
      columnDetailList:[
        {
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5OperationNo',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'operationNo',
          headerAlign: "center",
          align: "center",
          columnLabel: '工序号',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 60,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5OperationName',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'operationName',
          headerAlign: "center",
          align: "left",
          columnLabel: '工序名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5PartNo',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'partNo',
          headerAlign: "center",
          align: "left",
          columnLabel: '物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5PartDesc',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'partDesc',
          headerAlign: "center",
          align: "left",
          columnLabel: '物料描述',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 200,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5Version',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'version',
          headerAlign: "center",
          align: "center",
          columnLabel: '版本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5AlternativeNo',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'alternativeNo',
          headerAlign: "center",
          align: "left",
          columnLabel: '替代编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5AlternativeDesc',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'alternativeDesc',
          headerAlign: "center",
          align: "left",
          columnLabel: '替代描述',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5WorkCenterNo',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'workCenterNo',
          headerAlign: "center",
          align: "left",
          columnLabel: '加工中心编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5WorkCenterDesc',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'workCenterDesc',
          headerAlign: "center",
          align: "left",
          columnLabel: '加工中心名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 200,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5EfficiencyFactor',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'efficiencyFactor',
          headerAlign: "center",
          align: "right",
          columnLabel: '效率%',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5MachRunFactor',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'machRunFactor',
          headerAlign: "center",
          align: "right",
          columnLabel: '机器运行速度',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5MachSetupTime',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'machSetupTime',
          headerAlign: "center",
          align: "right",
          columnLabel: '调机时长',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5RunTimeDesc',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'runTimeDesc',
          headerAlign: "center",
          align: "center",
          columnLabel: '产出单位',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5LaborRunFactor',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'laborRunFactor',
          headerAlign: "center",
          align: "right",
          columnLabel: '人工生产速度',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5LaborSetupTime',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'laborSetupTime',
          headerAlign: "center",
          align: "right",
          columnLabel: '人工调机时长',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5CrewSize',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'crewSize',
          headerAlign: "center",
          align: "right",
          columnLabel: '生产过程人数',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5SetupCrewSize',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'setupCrewSize',
          headerAlign: "center",
          align: "right",
          columnLabel: '调机过程人数',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5LaborClassNo',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'laborClassNo',
          headerAlign: "center",
          align: "right",
          columnLabel: '人员等级',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5SetupLaborClassNo',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'setupLaborClassNo',
          headerAlign: "center",
          align: "right",
          columnLabel: '调机人员等级',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },{
          userId: this.$store.state.user.name,
          functionId: 102003,
          serialNumber: '102003Table5QuoteUnitCost',
          tableId: "102003Table5",
          tableName: "报价工艺信息",
          columnProp: 'quoteUnitCost',
          headerAlign: "center",
          align: "left",
          columnLabel: '单位报价成本',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100,
        },
      ],
      versionData:{},
    }
  },
  methods:{
    nodeClick(val){
      this.selectRouting = val.value.split("-")
      this.$nextTick(()=>{
        this.$refs.tree.setCurrentKey(`${this.selectRouting[0]}-${this.selectRouting[1]}-${this.selectRouting[2]}-${this.selectRouting[3]}-${this.selectRouting[4]}-${this.selectRouting[5]}`)
      })
      this.searchQuoteRoutingList()
    },
    getRoutingTreeStructure(){
      let params = {
        site:this.detail.site,
        testPartNo:this.detail.productNo,
        quoteDetailId:this.detail.quotationDetailId
      }
      this.treeLoading = true;
      getRoutingTreeStructure(params).then(({data})=>{
        if (data && data.code === 0){
          this.routingTreeStructure = data.rows;
          this.$nextTick(()=>{
            this.$refs.tree.setCurrentKey(`${this.selectRouting[0]}-${this.selectRouting[1]}-${this.selectRouting[2]}-${this.selectRouting[3]}-${this.selectRouting[4]}-${this.selectRouting[5]}`)
          })
          this.searchQuoteRoutingList();
        }else {
          this.$message.warning(data.msg)
        }
        this.treeLoading = false;
        this.$emit("update:loadingStatus",this.treeLoading)
      }).catch((error)=>{
        this.$message.error(error)
        this.treeLoading = false;
        this.$emit("update:loadingStatus",this.treeLoading)
      })
    },
    changeSelect(){
      if (this.selectRouting.length === 0){
        this.updateQuoteRoutingList();
        return
      }
      this.$confirm('此操作将更换工艺信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.updateQuoteRoutingList();
      }).catch(() => {
        this.selectRouting = JSON.parse(JSON.stringify(this.copyRouting))
        this.$nextTick(()=>{
          this.$refs.tree.setCurrentKey(`${this.selectRouting[0]}-${this.selectRouting[1]}-${this.selectRouting[2]}-${this.selectRouting[3]}-${this.selectRouting[4]}-${this.selectRouting[5]}`)
        })
      });
    },
    updateQuoteRoutingList(){
      let params = {
        site:this.$store.state.user.site,
        testPartNo: this.detail.productNo,
        version:this.selectRouting[0],
        routingType:this.selectRouting[1],
        alternativeNo:this.selectRouting[2],
        quoteDetailId:this.detail.quotationDetailId
      }
      updateQuoteRoutingList(params).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg)
          this.searchQuoteRoutingList();
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    setSelectRouting(val){
      if (val.length === 0){
        this.getRoutingTreeStructure();
        return
      }
      this.selectRouting = JSON.parse(JSON.stringify(val))
      this.getRoutingTreeStructure();
    },
    searchQuoteRoutingList(){
      let params = {
        site:this.$store.state.user.site,
        partNo: this.selectRouting[3],
        version:this.selectRouting[0],
        routingType:this.selectRouting[2],
        alternativeNo:this.selectRouting[1],
        id:this.selectRouting[4],
        quoteDetailId:this.detail.quotationDetailId
      }
      this.routingDetailList = [];
      this.dataListLoading = true;
      if (this.RoutingAllSearchFlag){
        searchQuoteRoutingList(params).then(({data})=>{
          this.dataListLoading = false;
          if (data && data.code === 0){
            this.routingDetailList = data.rows
            this.computedQuoteRoutingCost();
          }else {
            this.$message.warning(data.msg)
          }
          this.$emit("update:loadingStatus",this.dataListLoading)
        }).catch((error)=>{
          this.dataListLoading = false;
          this.$emit("update:loadingStatus",this.dataListLoading)
          this.$message.error(error)
        })
      }else {
        searchQuoteRoutingDetailList(params).then(({data})=>{
          this.dataListLoading = false;
          if (data && data.code === 0){
            this.routingDetailList = data.rows
            this.computedQuoteRoutingCost();
          }else {
            this.$message.warning(data.msg)
          }
          this.$emit("update:loadingStatus",this.dataListLoading)
        }).catch((error)=>{
          this.dataListLoading = false;
          this.$emit("update:loadingStatus",this.dataListLoading)
          this.$message.error(error)
        })
      }
    },
    computedQuoteRoutingCost(){
      for (let i = 0; i < this.routingDetailList.length; i++) {
        this.setRunTimeDesc(i);
        let detail = this.routingDetailList[i];
        // detail.machineCost 机器成本
        // detail.laborCosts 人工成本
        detail.laborCosts = new Decimal(detail.laborClassCost)
      }
    },
    setRunTimeDesc(i){
      switch (this.routingDetailList[i].runTimeCode){
        case 'A':
          this.routingDetailList[i].runTimeDesc = '单位/小时'
          break
        case 'B':
          this.routingDetailList[i].runTimeDesc = '小时/单位'
          break
        case 'C':
          this.routingDetailList[i].runTimeDesc = '小时'
          break
        default:
          this.routingDetailList[i].runTimeDesc = this.routingDetailList[i].runTimeCode;
      }
    },
    alternativeRowStyle({row}){
      if (row.version === this.versionData.version && row.routingType === this.versionData.routingType && row.alternativeNo === this.versionData.alternativeNo){
        return {'background-color': '#E8F7F6', cursor: 'pointer'}
      }
    },
    versionRowStyle({row}){
      if (row.version === this.versionData.version && row.routingType === this.versionData.routingType){
        return {'background-color': '#E8F7F6', cursor: 'pointer'}
      }
    },
    clickAlternative(row){
      let params = {
        id:this.selectRouting[4],
        site:this.$store.state.user.site,
        partNo: row.partNo,
        version:row.version,
        routingType:row.routingType,
        alternativeNo:row.alternativeNo,
        quoteDetailId:this.detail.quotationDetailId
      }
      updateQuoteRoutingList(params).then(({data})=>{
        if (data && data.code === 0){
          this.$message.success(data.msg)
          this.searchQuoteRoutingList();
          this.handoffVersion = false
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    clickVersionCheck(){
      this.routingAlternativeList = []
      this.routingVersionList = []
      this.versionData = {};
      this.searchQuoteRoutingVersion();
    },
    searchQuoteRoutingVersion(){
      let params = {
        site:this.detail.site,
        quoteDetailId:this.detail.quotationDetailId,
        partNo:this.selectRouting[3],
        id:this.selectRouting[4]
      }
      this.routingVersionList = [];
      searchQuoteRoutingAlternativeNo(params).then(({data})=>{
        if (data && data.code === 0){
          this.routingVersionList = data.rows;
          this.handoffVersion = true;
          if (data.row !== undefined && data.row !== null){
            this.versionData = {
              partNo:data.row.partNo,
              site:data.row.site,
              version:data.row.version,
              routingType:data.row.routingType,
              alternativeNo:data.row.alternativeNo
            }
          }else {
            this.selectionVersion();//默认选择项
          }
          // 查询替代
          this.searchRoutingAlternative(this.versionData);
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    selectionVersion(){
      for (let i = 0; i < this.routingVersionList.length; i++) {
        let routingVersion = this.routingVersionList[i]
        if (routingVersion.version === this.versionData.version && routingVersion.routingType === this.versionData.routingType){
          this.versionData = routingVersion;
          return
        }
      }
    },
    searchRoutingAlternative(row){
      let params = {
        site:this.detail.site,
        quoteDetailId:this.detail.quotationDetailId,
        partNo:row.partNo,
        version:row.version,
        routingType:row.routingType,
        alternativeNo:row.alternativeNo,
      }
      searchQuoteRoutingAlternativeNo(params).then(({data})=>{
        if (data && data.code === 0){
          this.routingAlternativeList = data.rows;
        }else {
          this.$message.warning(data.msg)
        }
      })
    },
    clickVersionTable(row){
      this.versionData = row;
      this.searchRoutingAlternative(row)
    }
  }
}
</script>

<template>
<div>
  <div style="margin-bottom: 5px">
    <el-link style="cursor:pointer;" v-if="this.selectRouting.length === 6" @click="clickVersionCheck">切换版本</el-link>
    <span style="display: inline-block;width: 20px"></span>
    <el-checkbox v-model="RoutingAllSearchFlag">全级Routing结构</el-checkbox>
  </div>
  <el-container>
    <el-aside width="24%" style="padding: 0;" :style="{height: height}" v-loading="treeLoading">
      <el-tree :data="routingTreeStructure"
               :props="routingProps"
               @node-click="nodeClick"
               :expand-on-click-node="false"
               node-key="value" style="height: 100%"
               default-expand-all
               highlight-current ref="tree"
      ></el-tree>
    </el-aside>
    <el-main style="padding: 0">
      <el-table :data="routingDetailList" :height="height"
                stripe border element-loading-text = "数据正在加载中"
                v-loading="dataListLoading">
        <el-table-column
          v-for="(item,index) in columnDetailList" :key="index"
          :sortable="item.columnSortable"
          :prop="item.columnProp"
          :header-align="item.headerAlign"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :align="item.align"
          :fixed="item.fixed===''?false:item.fixed"
          :min-width="item.columnWidth"
          :label="item.columnLabel">
          <template slot-scope="scope">
            <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
            <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
  </el-container>
  <el-dialog :visible.sync="handoffVersion" title="工艺版本切换" v-drag append-to-body>
    <el-table :data="routingVersionList" :row-style="versionRowStyle" ref="routingVersionTable" style="margin-top: 8px" border :height="240" @row-click="clickVersionTable">
      <el-table-column label="物料编码" prop="partNo" header-align="center" align="center" show-overflow-tooltip min-width="140"/>
      <el-table-column label="物料描述" prop="partDesc" header-align="center" align="left" show-overflow-tooltip min-width="200"/>
      <el-table-column label="版本" prop="version" header-align="center" align="center" show-overflow-tooltip min-width="60"/>
      <el-table-column label="类型" prop="routingType" header-align="center" align="center" show-overflow-tooltip min-width="100"/>
    </el-table>

    <el-table :data="routingAlternativeList" :row-style="alternativeRowStyle" border :height="240" style="margin-top: 8px">
      <el-table-column label="物料编码" prop="partNo" header-align="center" align="center" show-overflow-tooltip min-width="140"/>
      <el-table-column label="物料描述" prop="partDesc" header-align="center" align="left" show-overflow-tooltip min-width="200"/>
      <el-table-column label="版本" prop="version" header-align="center" align="center" show-overflow-tooltip min-width="60"/>
      <el-table-column label="替代编码" prop="alternativeNo" header-align="center" align="left" show-overflow-tooltip min-width="60"/>
      <el-table-column label="类型" prop="routingType" header-align="center" align="center" show-overflow-tooltip min-width="100"/>
      <el-table-column label="操作" min-width="80" header-align="center" align="center">
        <template slot-scope="{row,$index}">
          <span style="color: #888;cursor:no-drop;" v-if="row.version === versionData.version && row.routingType === versionData.routingType && row.alternativeNo === versionData.alternativeNo">选择</span>
          <el-link style="cursor:pointer;" v-else @click="clickAlternative(row)">选择</el-link>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</div>
</template>

<style scoped>

</style>
