<script>
import {
  removeExternalPart,
  saveExternalPart,
  selectExternalPartList,
  selectUnitList,
  updateExternalPart
} from '../../../api/part/external'
import dayjs from 'dayjs'
import PictureModule from './picture/picture.vue'
import LinkedCustomer from './customer/linkedCustomer.vue'
import {getExternalPartCustomerList} from '../../../api/part/externalPartCustomer'
import {getExternalPartPictureList} from '../../../api/part/externalPartPicture'
import LinkedManufacturer from './manufacturer/linkedManufacturer.vue'
import {getExternalPartManufacturerList} from '../../../api/part/externalPartManufacturer'
import {getTableDefaultListLanguage, getTableUserListLanguage} from '../../../api/table'
import {Decimal} from 'decimal.js'
import {getCategoryList} from '../../../api/category/category'
import {countryList} from '../../../api/dict/country'

let part = {
  site:'',
  partNo:'',
  partDesc:'',
  umId:'',
  freeInfo:'',
  freeInfo2:'',
  rev:'',
  active:'',
  moldCode:'',
  category:'',
  qtyPerCarton:0,
  qtyPerRoll:0,
  rollsPerCarton:0,
}
export default {
  name: "ExternalPart",
  components: {LinkedManufacturer, LinkedCustomer, PictureModule},
  props:{
    height:{
      type:Number,
      default: 370
    },
    ofComponents: {
      type:Boolean,
      default: false
    }
  },
  data(){
    return{
      part: {
        ...part
      },
      savePart:{
        ...part,
        active:'Y',
      },
      partCategoryList:[

      ],
      category:{},
      partRules:{
        partNo: [{ required: true, message: 'Please input Part No', trigger: ['blur','change'] }],
        partDesc: [{ required: true, message: 'Please input Part Description', trigger: ['blur','change'] }],
        umId: [{ required: true, message: 'Please select UM', trigger: ['blur','change'] }],
        rev: [{ required: true, message: 'Please select Rev No', trigger: ['blur','change'] }],
        moldCode: [{ required: true, message: 'Please select Mold Code', trigger: ['blur','change'] }],
        qtyPerCarton: [{ required: true, message: 'Please input Qty Per Carton', trigger: ['blur','change'] }],
        qtyPerRoll: [{ required: true, message: 'Please input Qty Per Roll', trigger: ['blur','change'] }],
        rollsPerCarton: [{ required: true, message: 'Please input Rolls Per Carton', trigger: ['blur','change'] }],
        freeInfo2: [{ required: true, message: 'Please select OOC', trigger: ['blur','change'] }],
      },
      savePartVisible:false,
      partTableLoading:false,
      partList:[],
      unitList:[],
      columnList:[
        {
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TablePartNo',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "partNo",
          headerAlign: "center",
          align: "left",
          columnLabel: "Part No",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TablePartDesc',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "partDesc",
          headerAlign: "center",
          align: "left",
          columnLabel: "Part Description",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableRev',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "rev",
          headerAlign: "center",
          align: "left",
          columnLabel: "REV No",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableCategory',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "category",
          headerAlign: "center",
          align: "left",
          columnLabel: "Part Category",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },
        {
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableUmName',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "moldCode",
          headerAlign: "center",
          align: "left",
          columnLabel: "Mold Code",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableUmName',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "manufacturer",
          headerAlign: "center",
          align: "left",
          columnLabel: "Manufacturer",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableUmName',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "umName",
          headerAlign: "center",
          align: "left",
          columnLabel: "UM",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 60
        },
        {
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableQtyPerRoll',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "qtyPerRoll",
          headerAlign: "center",
          align: "left",
          columnLabel: "Qty Per Roll",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableRollsPerCarton',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "rollsPerCarton",
          headerAlign: "center",
          align: "left",
          columnLabel: "Rolls Per Carton",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableQtyPerCarton',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "qtyPerCarton",
          headerAlign: "center",
          align: "left",
          columnLabel: "Qty Per Carton",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableFreeInfo',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "freeInfo",
          headerAlign: "center",
          align: "left",
          columnLabel: "Free Info1",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableFreeInfo2',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "freeInfo2",
          headerAlign: "center",
          align: "left",
          columnLabel: "Free Info2",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableActive',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "active",
          headerAlign: "center",
          align: "left",
          columnLabel: "Active",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableCreateBy',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "createBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Created By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableCreateTime',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "createTime",
          headerAlign: "center",
          align: "left",
          columnLabel: "Created Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableUpdateBy',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "updateBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Updated By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002TableUpdateTime',
          tableId: "10002Table",
          tableName: "外部料号信息表",
          columnProp: "updateTime",
          headerAlign: "center",
          align: "left",
          columnLabel: "Updated Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },
      ],
      pictureColumnList:[
        {
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table22PictureNo',
          tableId: "10002Table2",
          tableName: "外部料号图片信息表",
          columnProp: "pictureNo",
          headerAlign: "center",
          align: "left",
          columnLabel: "Document No",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table22PictureDesc',
          tableId: "10002Table2",
          tableName: "外部料号图片信息表",
          columnProp: "pictureDesc",
          headerAlign: "center",
          align: "left",
          columnLabel: "Document Desc",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 180
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table22CreateBy',
          tableId: "10002Table2",
          tableName: "外部料号图片信息表",
          columnProp: "createBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Created By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table22CreateTime',
          tableId: "10002Table2",
          tableName: "外部料号图片信息表",
          columnProp: "createTime",
          headerAlign: "center",
          align: "center",
          columnLabel: "Created Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 180
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table22UpdateBy',
          tableId: "10002Table2",
          tableName: "外部料号图片信息表",
          columnProp: "updateBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Updated By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table22UpdateTime',
          tableId: "10002Table2",
          tableName: "外部料号图片信息表",
          columnProp: "updateTime",
          headerAlign: "center",
          align: "center",
          columnLabel: "Updated Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 180
        },
      ],
      customerColumnList:[
        {
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table3CustomerNo',
          tableId: "10002Table3",
          tableName: "外部料号客户信息表",
          columnProp: "customerNo",
          headerAlign: "center",
          align: "left",
          columnLabel: "Customer No",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table3CustomerName',
          tableId: "10002Table3",
          tableName: "外部料号客户信息表",
          columnProp: "customerName",
          headerAlign: "center",
          align: "left",
          columnLabel: "Customer Name",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table3CreateTime',
          tableId: "10002Table3",
          tableName: "外部料号客户信息表",
          columnProp: "createTime",
          headerAlign: "center",
          align: "center",
          columnLabel: "Created Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table3CreateBy',
          tableId: "10002Table3",
          tableName: "外部料号客户信息表",
          columnProp: "createBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Created By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table3UpdateTime',
          tableId: "10002Table3",
          tableName: "外部料号客户信息表",
          columnProp: "updateTime",
          headerAlign: "center",
          align: "center",
          columnLabel: "Updated Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table3UpdateBy',
          tableId: "10002Table3",
          tableName: "外部料号客户信息表",
          columnProp: "updateBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Updated By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },
      ],
      manufacturerColumnList:[
        {
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table4ManufacturerNo',
          tableId: "10002Table4",
          tableName: "外部料号供应商信息表",
          columnProp: "manufacturerNo",
          headerAlign: "center",
          align: "left",
          columnLabel: "Manufacturer No",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table4ManufacturerName',
          tableId: "10002Table4",
          tableName: "外部料号供应商信息表",
          columnProp: "manufacturerName",
          headerAlign: "center",
          align: "left",
          columnLabel: "Manufacturer Name",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 120
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table4CreateTime',
          tableId: "10002Table4",
          tableName: "外部料号供应商信息表",
          columnProp: "createTime",
          headerAlign: "center",
          align: "center",
          columnLabel: "Created Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table4CreateBy',
          tableId: "10002Table4",
          tableName: "外部料号供应商信息表",
          columnProp: "createBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Created By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table4UpdateTime',
          tableId: "10002Table4",
          tableName: "外部料号供应商信息表",
          columnProp: "updateTime",
          headerAlign: "center",
          align: "center",
          columnLabel: "Updated Time",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 140
        },{
          userId: this.$store.state.user.name,
          functionId: 10002,
          serialNumber: '10002Table4UpdateBy',
          tableId: "10002Table4",
          tableName: "外部料号供应商信息表",
          columnProp: "updateBy",
          headerAlign: "center",
          align: "left",
          columnLabel: "Updated By",
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 10,
          status: true,
          fixed: '',
          columnWidth: 80
        },
      ],
      activeName:'partPicture',
      currentPart:{},
      externalPartCustomerList:[],
      pictureList:[],
      externalPartManufacturerList:[],
      base64List:[],
      countryList:[],
    }
  },
  watch:{
    currentPart(newVal,oldVal){
      if (!this.ofComponents){
        // 调用 子组件信息查询
        this.getExternalPartCustomerList();
        this.getExternalPartPictureList();
        this.getExternalPartManufacturerList();
      }
    },
    "savePart.partNo"(newVal,oldVal){
      this.savePart.partNo = newVal.toUpperCase()
    },
    "savePart.category"(newVal,oldVal){
      const category = this.partCategoryList.find((item)=>item.categoryValue === newVal);
      if (category){
        this.savePart.manufacturerRequired = category.manufacturerRequired;
        this.savePart.moldCodeRequired = category.moldCodeRequired;
        this.savePart.serialNumberRequired = category.serialNumberRequired;
        this.savePart.rollLabelRequired = category.rollLabelRequired;
        if (this.savePart.serialNumberRequired === 'Y'){
          this.savePart.qtyPerRoll = 1;
          this.savePart.rollsPerCarton = 1;
        }
        if (category.showCartonQtyPerRoll === 'N'){
          this.savePart.qtyPerRoll = 1;
        }
        if (category.showCartonRollsPerCarton === 'N'){
          this.savePart.rollsPerCarton = 1;
        }
        this.partRules.rollsPerCarton[0].message = `Please input ${category.qtyPerRollPart}`
        this.savePart.qtyPerCarton = this.qtyPerCarton();
        this.category = JSON.parse(JSON.stringify(category))
      }
    },
    "part.partNo"(newVal,oldVal){
      this.part.partNo = newVal.toUpperCase()
    },
    "savePart.qtyPerRoll"(newVal,oldVal){
      this.savePart.qtyPerCarton = this.qtyPerCarton();
    },
    "savePart.rollsPerCarton"(newVal,oldVal){
      this.savePart.qtyPerCarton = this.qtyPerCarton();
    },
  },
  methods:{
    getExternalPartPictureList(){
      let params = {
        partNo: this.currentPart.partNo,
        site: this.currentPart.site,
      }
      getExternalPartPictureList(params).then(({data})=>{
        if (data && data.code === 0){
          this.pictureList = data.rows;
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    rowPartStyle({row}){
      if (row.partNo === this.currentPart.partNo){
        return {
          background: 'rgb(232,246,246)',
        }
      }
    },
    selectUnitList(){
      selectUnitList().then(({data})=>{
        if (data && data.code === 0){
          this.unitList = data.rows;
          if (this.unitList.length > 0){
            this.savePart.umId = this.unitList[0].umId;
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    selectExternalPartList(){
      let params = {
        ...this.part,
        site: this.$store.state.user.site,
      }
      selectExternalPartList(params).then(({data})=>{
        if (data && data.code === 0){
          this.partList = data.rows;
          if (this.partList.length > 0 && !this.ofComponents){
            this.currentPart = {...this.partList[0]}
          }
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    handleSave(){
      if (this.savePartVisible){
        this.$refs.savePartForm.validate((valid,obj) => {
          if (valid) {
            if (this.savePart.qtyPerCarton === 0){
              this.$message.warning('Qty Per Carton can not be 0')
              return
            }
            let params = {
              ...this.savePart,
              site: this.$store.state.user.site,
              createBy: this.$store.state.user.name,
              createTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
            }
            saveExternalPart(params).then(({data})=>{
              if (data && data.code === 0){
                this.selectExternalPartList()
                this.savePartVisible = false
                this.$message.success(data.msg)
              }else {
                this.$message.warning(data.msg)
              }
            }).catch((error)=>{
              this.$message.error(error)
            })
          }else {
            for (let i = 0; i < Object.keys(obj).length; i++) {
              this.$message.warning(obj[Object.keys(obj)[i]][0].message)
              break
            }
          }
        })
      }else {
        this.savePart = {
          ...part,
          active: 'Y',
          umId: this.unitList.length > 0 ? this.unitList[0].umId : '',
          category: '',
          manufacturerRequired: 'N',
          moldCodeRequired: 'N',
          serialNumberRequired: 'N',
        }
        this.getCountryList();
        this.savePart.category = this.partCategoryList.length > 0 ? this.partCategoryList[0].categoryValue : ''
        this.savePartVisible = true;
      }
    },
    closeSavePart(){
      this.savePart = {
        ...part,
        active: 'Y',
      }
      if (this.unitList.length > 0){
        this.savePart.umId = this.unitList[0].umId;
      }
      this.$refs.savePartForm.resetFields()
    },
    removeExternalPart(row){
      removeExternalPart(row).then(({data})=>{
        if (data && data.code === 0){
          this.selectExternalPartList()
          this.$message.success(data.msg)
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    handleRowClick(row, column, event,type){
      this.currentPart = {...row}
      if (type === 0){
        this.getCountryList();
        this.savePart = {...row}
        this.savePartVisible = true;
      }
    },
    handleDblRowClick(row, column, event,type){
      if (this.ofComponents){
        this.$emit('rowClick',row)
      }else {
        this.currentPart = {...row}
        if (type === 0){
          this.savePart = {...row}
          this.savePartVisible = true;
        }
      }
    },
    handleUpdate(){
      this.$refs.savePartForm.validate((valid,obj) => {
        if (valid) {
          if (this.savePart.qtyPerCarton === 0){
            this.$message.warning('Qty Per Carton can not be 0')
            return
          }
          let params = {
            ...this.savePart,
            site: this.$store.state.user.site,
            createBy: undefined,
            createTime: undefined,
            updateBy: this.$store.state.user.name,
            updateTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
          }
          updateExternalPart(params).then(({data})=>{
            if (data && data.code === 0){
              this.selectExternalPartList()
              this.savePartVisible = false
              this.$message.success(data.msg)
            }else {
              this.$message.warning(data.msg)
            }
          }).catch((error)=>{
            this.$message.error(error)
          })
        }else {
          for (let i = 0; i < Object.keys(obj).length; i++) {
            this.$message.warning(obj[Object.keys(obj)[i]][0].message)
            break
          }
        }
      })
    },
    getExternalPartCustomerList(){
      let params = {
        partNo: this.currentPart.partNo,
        site: this.currentPart.site,
      }
      getExternalPartCustomerList(params).then(({data})=>{
        if (data && data.code === 0){
          this.externalPartCustomerList = data.rows;
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    getExternalPartManufacturerList(){
      let params = {
        partNo: this.currentPart.partNo,
        site: this.currentPart.site,
      }
      getExternalPartManufacturerList(params).then(({data})=>{
        if (data && data.code === 0){
          this.externalPartManufacturerList = data.rows;
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    async convertToBase64(fileUrls) {
      this.base64List = []
      for (const fileUrl of fileUrls) {
        const base64Url = await this.getFileAsBase64(fileUrl);
        this.base64List.push(base64Url);
      }
    },
    getFileAsBase64(fileUrl) {
      return new Promise((resolve, reject) => {
        const img = new Image();
        img.crossOrigin = 'Anonymous';
        img.src = fileUrl;
        img.onload = () => {
          const canvas = document.createElement('canvas');
          const ctx = canvas.getContext('2d');
          canvas.width = img.width;
          canvas.height = img.height;
          ctx.drawImage(img, 0, 0);
          const base64data = canvas.toDataURL('image/jpeg');
          resolve(base64data);
        };
        img.onerror = (error) => {
          reject(error);
        };
      });
    },
    async getTableUserColumn (tableId, columnId) {
      let queryTableUser = {
        userId: this.$store.state.user.name,
        functionId: this.$route.meta.menuId,
        tableId: tableId,
        status: true,
        languageCode: this.$i18n.locale
      }
      await getTableUserListLanguage(queryTableUser).then(({data}) => {
        if (data.rows.length > 0) {
          this.caseTable(data.rows,columnId)
        } else {
          this.getColumnList(tableId, columnId)
        }
      })
    },

// 获取 tableDefault 列
    async getColumnList (tableId, columnId) {
      let queryTable = {
        functionId: this.$route.meta.menuId,
        tableId: tableId,
        languageCode: this.$i18n.locale
      }
      await getTableDefaultListLanguage(queryTable).then(({data}) => {
        if (!data.rows.length === 0) {
          this.caseTable(data.rows,columnId)
        } else {

        }
      })
    },
    caseTable(list,columnId){
      if (list.length > 0){
        switch (columnId) {
          case 1:
            this.columnList = list
            break;
          case 2:
            this.pictureColumnList = list
            break;
          case 3:
            this.customerColumnList = list
            break;
          case 4:
            this.manufacturerColumnList = list
            break;
        }
      }
    },
    qtyPerCarton(){
      return new Decimal(this.savePart.qtyPerRoll).mul(this.savePart.rollsPerCarton).toNumber();
    },
    getCategoryList(){
      let params = {};
      getCategoryList(params).then(({data})=>{
        if (data && data.code === 0){
          this.partCategoryList = data.rows;
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    },
    changeQtyRollCarton(){
      this.savePart.qtyPerRoll = this.savePart.qtyPerCarton;
    },
    getCountryList(){
      let params = {

      }
      countryList(params).then(({data})=>{
        if (data && data.code === 0){
          this.countryList = data.rows
        }else {
          this.$message.warning(data.msg)
        }
      }).catch((error)=>{
        this.$message.error(error)
      })
    }
  },
  computed:{

  },
  created () {
    this.selectUnitList();
    this.getCategoryList();
    // this.getCountryList();
    this.selectExternalPartList();
    this.getTableUserColumn("10002Table", 1)
    this.getTableUserColumn("10002Table2", 2)
    this.getTableUserColumn("10002Table3", 3)
    this.getTableUserColumn("10002Table4", 4)
  }
}
</script>

<template>
<div>
 <el-form :inline="true" label-position="top" :model="part" label-width="100px">
    <el-form-item label="Part No">
      <el-input v-model="part.partNo"></el-input>
    </el-form-item>
    <el-form-item label="Part Description">
      <el-input v-model="part.partDesc"></el-input>
    </el-form-item>
    <el-form-item label="Part Category">
      <el-select v-model="part.category" placeholder="">
        <el-option label="All" value=""></el-option>
        <el-option :label="item.categoryName" :value="item.categoryValue" :key="item.id" v-for="item in partCategoryList"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="Active">
      <el-select v-model="part.active" placeholder="">
        <el-option label="All" value=""></el-option>
        <el-option label="Active" value="Y"></el-option>
        <el-option label="Not Active" value="N"></el-option>
      </el-select>
    </el-form-item>
   <el-form-item label=" ">
     <el-button type="primary" @click="handleSave" v-if="!ofComponents">New</el-button>
     <el-button type="primary" @click="selectExternalPartList">Query</el-button>
   </el-form-item>
 </el-form>

  <el-table :data="partList" :height="height" :row-style="rowPartStyle" @row-click="handleRowClick" @row-dblclick="handleDblRowClick" :loading="partTableLoading" border style="width: 100%;margin-top: 5px">
    <el-table-column v-if="!ofComponents"
                     header-align="center"
                     align="center"
                     width="100"
                     fixed="left"
                     label="Actions">
      <template slot-scope="scope">
        <el-link style="cursor:pointer;" @click="handleRowClick(scope.row,null,null,0)">Edit</el-link>
        <el-link style="cursor:pointer;" @click="removeExternalPart(scope.row)">Delete</el-link>
      </template>
    </el-table-column>
    <el-table-column
      v-for="(item,index) in columnList" :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-tabs v-model="activeName" v-if="!ofComponents">
    <el-tab-pane label="Part Picture" name="partPicture">
      <picture-module :column-list="pictureColumnList" :part="currentPart" :data-list="pictureList" @refresh="getExternalPartPictureList"></picture-module>
    </el-tab-pane>
    <el-tab-pane label="Linked Customer" name="linkedCustomer">
      <linked-customer :column-list="customerColumnList" :part="currentPart" :data-list="externalPartCustomerList" @refresh="getExternalPartCustomerList"></linked-customer>
    </el-tab-pane>
    <el-tab-pane label="Linked Manufacturer" name="linkedManufacturer">
      <linked-manufacturer :column-list="manufacturerColumnList" :part="currentPart" :data-list="externalPartManufacturerList" @refresh="getExternalPartManufacturerList"></linked-manufacturer>
    </el-tab-pane>
  </el-tabs>

  <el-dialog v-drag :title="`Part`" :close-on-click-modal="false" @close="closeSavePart" width="600px" :visible.sync="savePartVisible">
    <el-form :model="savePart" label-position="top" ref="savePartForm" :rules="partRules">
      <el-row :gutter="10">
        <el-col :span="7">
          <el-form-item label="Part No" prop="partNo" :show-message="false">
            <el-input :readonly="savePart.site !== ''" :disabled="savePart.site !== ''" v-model="savePart.partNo"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="17">
          <el-form-item label="Part Description" prop="partDesc" :show-message="false">
            <el-input v-model="savePart.partDesc"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="Part Category" prop="category" :show-message="false">
            <el-select v-model="savePart.category" placeholder="" style="width: 100%">
              <el-option :label="item.categoryName" :value="item.categoryValue" :key="item.id" v-for="item in partCategoryList"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="UM" prop="umId" :show-message="false">
            <el-select v-model="savePart.umId" placeholder="" :show-message="false">
              <el-option v-for="item in unitList" :key="item.umId" :label="item.umName" :value="item.umId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7" :offset="2">
          <el-form-item v-if="savePart.moldCodeRequired === 'Y'" key="moldCode" label="Mold Code" prop="moldCode" :show-message="false">
            <el-input v-model="savePart.moldCode"></el-input>
          </el-form-item>
          <el-form-item v-else label="Mold Code">
            <el-input v-model="savePart.moldCode" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="7" v-if="category.rollLabelRequired === 'Y'">
          <el-form-item label="Qty Per Roll" prop="qtyPerRoll" :show-message="false">
            <el-input-number :step="0" :min="0" :controls="false" :disabled="this.savePart.serialNumberRequired === 'Y'" v-model="savePart.qtyPerRoll"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="7" v-if="category.rollLabelRequired === 'Y'">
          <el-form-item label="Rolls Per Carton" prop="rollsPerCarton" :show-message="false">
            <el-input-number :step="0" :min="0" :controls="false" :disabled="this.savePart.serialNumberRequired === 'Y'" v-model="savePart.rollsPerCarton"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item :label="category.qtyPerRollPart" prop="qtyPerCarton" :show-message="false">
            <el-input-number :step="0" :min="0" :controls="false" @change="changeQtyRollCarton" :disabled="category.editQtyPerCarton === 'N'" v-model="savePart.qtyPerCarton"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="7">
          <el-form-item label="Rev No" prop="rev" :show-message="false">
            <el-input v-model="savePart.rev"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="17">
          <el-form-item label="Free Info1" :show-message="false">
            <el-input v-model="savePart.freeInfo"></el-input>
          </el-form-item>
<!--          <el-form-item label="Free Info" :show-message="false">-->
<!--            <el-input v-model="savePart.freeInfo"></el-input>-->
<!--          </el-form-item>-->
        </el-col>
        <el-col :span="7">
          <el-form-item label=" " :show-message="false">
            <el-checkbox v-model="savePart.active" true-label="Y" false-label="N">Active</el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="17">
          <el-form-item label="Free Info2" :show-message="false">
            <el-input v-model="savePart.freeInfo2"></el-input>
          </el-form-item>
        </el-col>
<!--        <el-form-item label="OOC" :show-message="false" prop="freeInfo2">-->
<!--          <el-select v-model="savePart.freeInfo2" placeholder="" style="width: 100%">-->
<!--            <el-option v-for="item in countryList" :label="item.countryNo" :value="item.countryNo" :key="item.countryNo"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="savePart.site !== ''" @click="handleUpdate">Save</el-button>
      <el-button type="primary" v-else @click="handleSave">Save</el-button>
      <el-button @click="savePartVisible = false">Cancel</el-button>
    </div>
  </el-dialog>

  <div v-show="false">
    <img :src="item" v-for="(item,index) in base64List" :key="index" style="display: none">
  </div>
</div>
</template>

<style scoped>

</style>
