import systemTask from "../task/components/index.vue"
import FieldDrawer from '@/views/components/drawer/Field.vue'
export default{
    components: {
        systemTask,
        FieldDrawer
      },
    props:{
        importData:{
            default:()=>{
                return {}
            }
        }
    },
    data() {
        return {
            tableData: [
                {
                    name: '标引',
                    field: 'index'
                }, {
                    name: '分类',
                    field: 'classify'
                }
            ],
            form:this.importData,
            customField: [],
            customFieldId:null,
            choseField:{},
            ExcelConfig:[],
            projectDetails:{},
            treeProp:{
                children:'child',
                label: 'name'
            },
            assoImportTaskFieldVOS:[],
            importContent:['0','1','2','3']
        }
    },
    computed:{
        importContents(){
            return this.$store.state.dictMessage.dictMessage.IMPORT_CONTENT
        }
    },
    mounted() {
        if(this.form.type == 1){
            this.getExcelConfig()
        }
        if(!this.form.importToId){
            return false
        }
        if(this.form.importToId && this.form.importToType != 2){
            //获取所有栏位
            this.getAllCustomField()
        }else{
       
              this.choseField = {
                  id:this.form.importToId,
                  name:this.form.name,
                  type:6,
                  isProduct:2,
                  fieldValue:[]
                }
              this.customFieldId = this.form.importToId
              this.getProductTree()
        }
        
    },
    methods: {
        changeValue(value,data){
            if(this.choseField.type == 5 || this.choseField.type == 6){
                var index = this.assoImportTaskFieldVOS.findIndex(item=>{
                    return item.fieldValueId == value
                })
                if(index!=-1){
                    this.assoImportTaskFieldVOS.splice(index,1)
                }else{
                    this.assoImportTaskFieldVOS.push(
                        {
                            projectId:this.form.importToId,
                            fieldType:this.choseField.type,//类型
                            fieldId:this.choseField.id,//栏位ID
                            fieldValueId:value,
                            keyValue:this.choseField.keyValue,
                            fieldValueStr:data.name,
                            fieldName:this.choseField.name
                        }
                    )
                }
            }else{
                var obj = this.assoImportTaskFieldVOS.find(item=>{
                    return item.fieldId == this.choseField.id
                })
                if(obj){
                    obj.fieldValueId = value
                }else{
                    this.assoImportTaskFieldVOS.push(
                        {
                            projectId:this.form.importToId,
                            fieldType:this.choseField.type,//类型
                            fieldId:this.choseField.id,//栏位ID
                            fieldValueId:value,
                            keyValue:this.choseField.keyValue,
                            fieldValueStr:data?data.name:value,
                            fieldName:this.choseField.name
                        }
                    )
                }
            }
        },
        del(index,item){
            this.assoImportTaskFieldVOS.splice(index,1)
            var obj = this.customField.find(item=>{
                return item.keyValue == item.keyValue
            })
            if(obj){
                var index = obj.fieldValue.indexOf(item.fieldValueId)
                if(obj.type == 5 || obj.type == 6){
                    obj.fieldValue.splice(index,1)
                }else{
                    obj.fieldValue = ''
                }
            }
        },
        //修改选择的栏位
        changeCustomField(val){
            if(!val){
                return false
            }
            this.choseField = this.customField.find(item=>{
                return item.keyValue == val
            })
            if(this.choseField){
                if(this.choseField.options && this.choseField.options.length>0){
                    return false
                }else{
                    if(this.choseField.isProduct){
                        this.getProductTree()
                    }else{
                        this.getFieldOption()
                    }
                }
            }
        },
        //获取自定义选项
        getFieldOption(){
            let params = {
                customFieldId: this.choseField.id,
              }
            this.$api.queryCustomOption(params).then(response => {
                this.$set(this.choseField,'options',response.data.data)
              }).catch(error => {
              })
        },
        //获取产品架构
        getProductTree(){
            let params = {
                name: this.choseField.name,//名称
                type: this.choseField.isProduct,//类型：1产品类别，2产品，3技术分类，4自定义树
                typeId: this.choseField.id,//产品或类别id
              }
            this.$api.queryTreeNodeTree(params).then(res => {
                if (res.code == 200) {
                    this.$set(this.choseField,'options',res.data.data)
                }
              }).catch(err => {
              })
        },
        //获取所有栏位
        async getAllCustomField(){
            const [project,product] = await Promise.allSettled([this.getProjectField(),this.getProjectDetails()])
            var projectField = project.status =='fulfilled'?project.value:[]
            this.projectDetails= product.status =='fulfilled'?product.value:{}
            if(this.ods){
                this.ods.data = [
                    this.projectDetails
                ]
                this.$set(this.form,'importToId',this.form.importToId)
            }
            if(this.projectDetails.productOrCategory){
                projectField.push(
                    {
                        id:this.projectDetails.productOrCategory.id,
                        name:this.projectDetails.productOrCategory.name,
                        type:6,
                        isProduct:this.projectDetails.productOrCategory.type,
                    }
                )
            }
             projectField.forEach((item,index)=>{
                item.keyValue = (index+1)+''
                if(item.type == 5 || item.type == 6){
                    this.$set(item,'fieldValue',[])
                }else{
                    this.$set(item,'fieldValue','')
                }
            })
            this.customField =projectField
             
        },
        //获取自定义栏位
        async getProjectField(){
            var params = {
                searchQuery:`projectId=${this.form.importToId}`,
                orderDTOList: [
                    {   
                        "orderBy": "type",
                        "orderType": 1
                    }
                ],
              }
              return await this.$api.queryCustomField(params).then( async response=>await response.data.data)
        },
        //获取项目信息
        async getProjectDetails(){
            if(this.form.importToType != 0){
                return {
                    status:''
                }
            }
            var params = {
                searchQuery: `id=${this.form.importToId}`,//检索条件
                orderDTOList: [],//排序
              }
              return await this.$api.queryPatentProject(params).then( async response=>await response.data.data[0])
        },
        
        getExcelConfig() {
            this.$api.getExcelConfig().then(response => {
              this.ExcelConfig = response.data.data
            })
        },
        handleManage(){
            this.$refs.FieldDrawer.open(this.form.importToId)
        },
        closeFieldDrawer(){
            this.getAllCustomField()
        },
        //添加导入任务
        addImportTask(form){
            this.btnLoading = true
            this.loading = true
            this.$api.addImportTask(form).then(response => {
                this.$message.success('任务创建成功')
                this.form = this.importData
                this.btnLoading = false
                this.loading = false
                this.getQueueList()
            }).catch(error => {
                this.btnLoading = false
                this.loading = false
            })
        },
        getQueueList() {
            this.$refs.systemTask.getList()
        },
        // 获取时间
        handleData(val) {
            if (this.form.dateType=='') {
            this.$message.error('请先选择更新周期')
            return false
            }
            this.form.crons=val
        },
        getDateType(val) {
            this.form.crons = ''
          },
    },
}