<script>
import {queryProjectPartToolForApply, saveProjectToolApply} from "../../../api/tool/toolApply";
import Chooselist from '@/views/modules/common/Chooselist'
import DictDataSelect from "../sys/dict-data-select.vue"
import ChooseUser from '@/views/modules/common/ChooseUser'
export default {
  name: "toolingInfo",
  components:{
    Chooselist,DictDataSelect,ChooseUser,
  },
  data(){
    return{
      queryParams:{
        site:"",
        projectId:'',
        projectName:'',
        customerName:'',
        plmPartNo:'',
        ifsPartNo:'',
        partDesc:'',
        toolNo:'',
        toolDesc:'',
        no:1,
        size:20,
      },
      total:0,
      queryLoading:false,
      dataList:[],
      selectionDataList:[],
      columns:[
        {
          userId: this.$store.state.user.name,
          functionId: 106001001,
          serialNumber: '106001001Table1ProjectId',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "projectId",
          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: 106001001,
          serialNumber: '106001001Table1ProjectName',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "projectName",
          headerAlign: "center",
          align: "left",
          columnLabel: "项目名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 160
        },
        {
          userId: this.$store.state.user.name,
          functionId: 106001001,
          serialNumber: '106001001Table1CustomerName',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "customerName",
          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: 106001001,
          serialNumber: '106001001Table1PlmPartNo',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "testPartNo",
          headerAlign: "center",
          align: "left",
          columnLabel: "PLM物料编码",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 106001001,
          serialNumber: '106001001Table1IfsPartNo',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "finalPartNo",
          headerAlign: "center",
          align: "left",
          columnLabel: "IFS物料编码",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 106001001,
          serialNumber: '106001001Table1PartName',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "partName",
          headerAlign: "center",
          align: "left",
          columnLabel: "物料名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 160
        },
        {
          userId: this.$store.state.user.name,
          functionId: 106001001,
          serialNumber: '106001001Table1CustomerPartNo',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "customerPartNo",
          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: 106001001,
          serialNumber: '106001001Table1ToolId',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "toolId",
          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: 106001001,
          serialNumber: '106001001Table1ToolDescription',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "toolDescription",
          headerAlign: "center",
          align: "left",
          columnLabel: "工具描述",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 160
        },
        {
          userId: this.$store.state.user.name,
          functionId: 106001001,
          serialNumber: '106001001Table1RoutingType',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "routingType",
          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: 106001001,
          serialNumber: '106001001Table1RoutingRevision',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "routingRevision",
          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: 106001001,
          serialNumber: '106001001Table1AlternativeNo',
          tableId: "106001001Table1",
          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: 106001001,
          serialNumber: '106001001Table1AlternativeDescription',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "alternativeDescription",
          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: 106001001,
          serialNumber: '106001001Table1OperationNo',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "operationNo",
          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: 106001001,
          serialNumber: '106001001Table1OperationName',
          tableId: "106001001Table1",
          tableName: "项目物料信息",
          columnProp: "operationName",
          headerAlign: "center",
          align: "left",
          columnLabel: "工序名称",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 100
        },
      ],
      visible:false,
      jumpFlag:true,
      valueNull:'******',
      dataForm:{
        site: '',
        applyNo: '',
        applyBy:'',
        applyDate:'',
        applySumQty:'',
        applyReason:'',
        remark:'',
        createBy:'',
        department:'',
        quoter: '',
        quoterName: '',
        tp: '',
        tpName: '',
        purchaser: '',
        totalCost:'',
        purchaserName: '',
        detailList:[],
      },
    }
  },
  methods:{
    queryDataList(){
      this.queryLoading = true
      queryProjectPartToolForApply(this.queryParams).then(({data})=>{
        if (data && data.code === 0){
          this.dataList = data.rows
          this.total = data.total
        }else {
          this.$message.error(data.msg)
        }
        this.queryLoading = false
      }).catch((error)=>{
        this.$message.error(error)
        this.queryLoading = false
      })
    },
    selectionChangeHandle(val){
      this.selectionDataList = val;
    },
    sizeChangeHandle(val){
      this.queryParams.size = val;
      this.queryDataList();
    },
    currentChangeHandle(val){
      this.queryParams.no = val;
      this.queryDataList();
    },

    /* 列表方法的回调 */
    getUserData (val) {
      if(this.tagNo1 === 1) {
        this.dataForm.quoter = val.username
        this.dataForm.quoterName = val.userDisplay
        this.$nextTick(() => {
          this.$triggerInputEvent(this.$refs.quoterName);
        });
      }
      if(this.tagNo1 === 2) {
        this.dataForm.tp = val.username
        this.dataForm.tpName = val.userDisplay
        this.$nextTick(() => {
          this.$triggerInputEvent(this.$refs.tpName);
        });
      }
      if(this.tagNo1 === 3) {
        this.dataForm.purchaser = val.username
        this.dataForm.purchaserName = val.userDisplay
        this.$nextTick(() => {
          this.$triggerInputEvent(this.$refs.purchaserName);
        });
      }
    },
    // 获取基础数据列表S
    getBaseList (val, type) {
      this.tagNo = val
      this.tagNo1 = type
      this.$nextTick(() => {
        let strVal = ''
        let conSql = " and b.site = '" + this.$store.state.user.site + "'";
        if(val === 2002) {
          strVal = this.dataForm.quoter
        }
        if(val === 2042) {
          strVal = this.dataForm.tp
        }
        if(val === 2000) {
          strVal = this.dataForm.purchaser
        }

        this.$refs.baseList.init(val, strVal,conSql)
      })
    },
    /* 列表方法的回调 */
    getBaseData (val) {
      if(this.tagNo === 2002) {
        this.dataForm.quoter = val.username
        this.dataForm.quoterName = val.user_display
        this.$nextTick(() => {
          this.$triggerInputEvent(this.$refs.quoterName);
        });
      }
      if(this.tagNo === 2042) {
        this.dataForm.tp = val.username
        this.dataForm.tpName = val.user_display
        this.$nextTick(() => {
          this.$triggerInputEvent(this.$refs.tpName);
        });
      }
      if(this.tagNo === 2000) {
        this.dataForm.purchaser = val.username
        this.dataForm.purchaserName = val.user_display
        this.$nextTick(() => {
          this.$triggerInputEvent(this.$refs.purchaserName);
        });
      }
    },
    changeSum(row){
      if(row.standardCost<0||row.standardCost==null||row.standardCost==''){
        row.standardCost=0
        // this.$alert('成本不能为负数或者不填', '错误', {
        //   confirmButtonText: '确定'
        // })

      }
      this.dataForm.applySumQty = 0
      this.dataForm.totalCost = 0
      for (const item of this.selectionDataList) {
        // 累加之前先确保值存在，并将 null 或 undefined 转换为0
        this.dataForm.applySumQty += Number(item.applyQty != null && item.applyQty !== '' ? item.applyQty : 0);
        this.dataForm.totalCost += Number(item.applyQty != null && item.applyQty !== '' ? item.applyQty*item.standardCost : 0);
      }
    },
    saveData(){
      if(this.dataForm.applySumQty===0){
        this.$alert('未填写申请数量！', '错误', {
          confirmButtonText: '确定'
        })
        return false;
      }
      if(this.dataForm.applyBy===''||this.dataForm.applyBy==null){
        this.$alert('未选择申请人！', '错误', {
          confirmButtonText: '确定'
        })
        return false;
      }
      if(this.dataForm.applyDate===''||this.dataForm.applyDate==null){
        this.$alert('未选择申请日期！', '错误', {
          confirmButtonText: '确定'
        })
        return false;
      }
      if(this.dataForm.department===''||this.dataForm.department==null){
        this.$alert('未选择部门！', '错误', {
          confirmButtonText: '确定'
        })
        return false;
      }
      if(this.dataForm.quoter===''||this.dataForm.quoter==null){
        this.$alert('未选择报价员！', '错误', {
          confirmButtonText: '确定'
        })
        return false;
      }
      if(this.dataForm.tp===''||this.dataForm.tp==null){
        this.$alert('未选择TP！', '错误', {
          confirmButtonText: '确定'
        })
        return false;
      }
      if(this.dataForm.purchaser===''||this.dataForm.purchaser==null){
        this.$alert('未选择采购专员！', '错误', {
          confirmButtonText: '确定'
        })
        return false;
      }
      this.$confirm('确定要保存?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        for (let i = 0; i < this.selectionDataList.length; i++) {
          if (this.selectionDataList[i].applyQty===''||this.selectionDataList[i].applyQty==null){
            this.selectionDataList[i].applyQty=0
          }
        }
        this.dataForm.detailList=this.selectionDataList.filter(item => item.applyQty > 0).filter(item => item.standardCost >= 0)
        saveProjectToolApply(this.dataForm).then(({data}) => {
          if (data && data.code === 0) {
            this.visible=false;
            this.queryDataList();
            this.$message.success('操作成功')
            // 跳转
            if(this.jumpFlag){
              let inData = {
                site: this.$store.state.user.site,
                applyNo: data.applyNo,
                username: this.$store.state.user.name
              };
              localStorage.setItem('ToolApplyData', JSON.stringify(inData))
              this.$router.push('tooling-searchToolApply')
            }
          } else {
            this.$message.warning(data.msg);
          }
        })
      })
    },
    applyModel() {
      if(this.selectionDataList.length===0){
        this.$alert('请选择项目！', '错误', {confirmButtonText: '确定'})
        return false;
      }
      this.dataForm = {
        site: this.$store.state.user.site,
        applyNo: '',
        applyBy:this.$store.state.user.name,
        applyDate:new Date(),
        applySumQty:0,
        applyReason:'',
        remark:'',
        createBy:this.$store.state.user.name,
        department: '',
        quoter: '',
        quoterName: '',
        tp: '',
        tpName: '',
        purchaser: '',
        totalCost: '',
        purchaserName: '',
        detailList:[],
      };
      this.visible=true;
      this.jumpFlag=true;
    },
  },
  created() {
    // 初始化Site
    this.queryParams.site = this.$store.state.user.site;
  },
  watch:{
    queryLoading(newVal,oldVal){
      if (newVal){
        // 延时关闭,以防未关闭弹框
        setTimeout(()=>{
          this.queryLoading = false
        },3000)
      }
    }
  }
}
</script>

<template>
<div class="box-container">
  <el-form :model="queryParams" label-position="top" style="width: 1200px">
    <el-row :gutter="10">
      <el-col :span="3">
        <el-form-item label="项目号">
          <el-input v-model="queryParams.projectId" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="项目名称">
          <el-input v-model="queryParams.projectName" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="客户名称">
          <el-input v-model="queryParams.customerName" clearable></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="10">
      <el-col :span="3">
        <el-form-item label="PLM物料编码">
          <el-input v-model="queryParams.plmPartNo" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="IFS物料编码">
          <el-input v-model="queryParams.ifsPartNo" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="物料名称">
          <el-input v-model="queryParams.partDesc" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="工具编码">
          <el-input v-model="queryParams.toolNo" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label="工具描述">
          <el-input v-model="queryParams.toolDesc" clearable></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="3">
        <el-form-item label=" ">
          <el-button type="primary" @click="queryDataList">查询</el-button>
          <el-button type="primary" @click="applyModel">工具申请</el-button>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>

  <div style="height: calc(100% - 120px);">
    <el-table
      :data="dataList"
      height="100%"
      border
      @selection-change="selectionChangeHandle"
      v-loading="queryLoading">
      <el-table-column
        type="selection"
        align="center"
        width="55">
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columns" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        show-overflow-tooltip
        :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>
  </div>

  <el-pagination
    @size-change="sizeChangeHandle"
    @current-change="currentChangeHandle"
    :current-page="queryParams.no"
    :page-sizes="[20, 50, 100, 1000]"
    :page-size="queryParams.size"
    :total="total"
    layout="total, sizes, prev, pager, next, jumper">
  </el-pagination>


  <el-dialog width="1000px" v-drag title="刀模申请" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" label-position="top" label-width="100px" >
      <el-row :gutter="10">
        <el-col :span="4.8">
          <el-form-item label="申请编号">
            <el-input v-model="dataForm.applyNo" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item label="申请总数">
            <el-input v-model="dataForm.applySumQty" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item label="总成本">
            <el-input v-if="accessField('10601001')" v-model="dataForm.totalCost" disabled></el-input>
            <el-input v-if="!accessField('10601001')" v-model="valueNull"  disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item label="申请人" required>
            <el-input v-model="dataForm.applyBy" ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item label="申请日期" required>
            <el-date-picker
              value-format="yyyy-MM-dd"
              style="width: 100%"
              v-model="dataForm.applyDate"
              type="date"
              placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item label="部门" required>
            <dict-data-select :site="$store.state.user.site"  v-model="dataForm.department" dict-type="tool_application_department"></dict-data-select>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item required>
            <span slot="label" style="" @click="getBaseList(2002,1)"><a herf="#">报价专员</a></span>
            <el-input  v-model="dataForm.quoterName" ref="quoterName" placeholder="请选择人员" readonly  ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item required>
            <span slot="label" style="" @click="getBaseList(2042,2)"><a herf="#">TP</a></span>
            <el-input  v-model="dataForm.tpName" ref="tpName" placeholder="请选择人员" readonly  ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item required>
            <span slot="label" style="" @click="getBaseList(2000,3)"><a herf="#">采购专员</a></span>
            <el-input  v-model="dataForm.purchaserName" ref="purchaserName" placeholder="请选择人员" readonly  ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4.8">
          <el-form-item label=" " >
            <el-checkbox v-model="jumpFlag">跳转至申请记录</el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="申请原因" class="auto">
            <el-input type="textarea"  resize="none" :autosize="{minRows: 3, maxRows: 3}" v-model="dataForm.applyReason"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注"  class="auto">
            <el-input type="textarea"   resize="none" :autosize="{minRows: 3, maxRows: 3}" v-model="dataForm.remark"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="rq" style="margin-top: 10px">
      <el-table
        height="250"
        :data="selectionDataList"
        border
        ref="toolTable"
        style="width: 100%;">
        <el-table-column label="项目号" header-align="center" width="90" prop="projectId" show-overflow-tooltip/>
        <el-table-column label="工具编码"  header-align="center" width="100" prop="toolId" show-overflow-tooltip/>
        <el-table-column label="工具描述"  header-align="center" width="120" prop="toolDescription" show-overflow-tooltip/>
        <el-table-column label="工具数量" align="right" header-align="center" width="80" prop="toolQty" show-overflow-tooltip/>
        <el-table-column label="申请数量" align="right" header-align="center" width="80" prop="applyQty" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input v-model="scope.row.applyQty" type="number" @change="changeSum(scope.row)"  placeholder="请输入数量" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')" style="width:98%"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="工具成本" align="right" header-align="center" width="80" prop="standardCost" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-input  v-if="accessField('10601001')" v-model="scope.row.standardCost" type="number" @change="changeSum(scope.row)"  placeholder="请输入成本" style="width:98%"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="PLM物料编码"  header-align="center" width="140" prop="partNo" show-overflow-tooltip/>
        <el-table-column label="IFS物料编码"  header-align="center" width="140" prop="finalPartNo" show-overflow-tooltip/>
        <el-table-column label="物料名称"  header-align="center" width="160" prop="partName" show-overflow-tooltip/>
        <el-table-column label="客户料号"  header-align="center" width="100" prop="customerPartNo" show-overflow-tooltip/>
        <el-table-column label="工艺版本"  header-align="center" width="80" prop="routingRevision" show-overflow-tooltip/>
        <el-table-column label="工艺类型"  header-align="center" width="100" prop="routingType" show-overflow-tooltip/>
        <el-table-column label="替代编码"  header-align="center" width="80" prop="alternativeNo" show-overflow-tooltip/>
        <el-table-column label="替代名称"  header-align="center" width="80" prop="alternativeDescription" show-overflow-tooltip/>
        <el-table-column label="工序号"  header-align="center" width="60" prop="operationNo" show-overflow-tooltip/>
        <el-table-column label="工序名称"  header-align="center" width="80" prop="operationName" show-overflow-tooltip/>
      </el-table>
    </div>
    <span slot="footer" class="dialog-footer" style="height: 30px;line-height: 30px">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button type="primary" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>


  <Chooselist ref="baseList"  @getBaseData="getBaseData"></Chooselist>
  <ChooseUser ref="userList"  @getBaseData="getUserData"></ChooseUser>
</div>
</template>

<style scoped>

.auto /deep/ .el-form-item__content{
  height: auto;
  line-height: 1.5;
}
</style>
