<template>
  <div class="basic-information">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item v-if="(taskName === '业务创建' && module === '招投标管理') || (module === '销售合同管理' && taskName === '销售合同创建')" label="项目名称" prop="projectInformationId">
        <el-select
          v-model="ruleForm.projectInformationId"
          placeholder="请选择项目名称"
          clearable
          :disabled="disabled"
          @change="projectChange"
        >
          <el-option
            v-for="item in entryNameList"
            :key="item.projectInformationId"
            :label="item.projectName"
            :value="item.projectInformationId"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="(taskName !== '业务创建' && module === '招投标管理') || (module === '销售合同管理' && taskName !== '销售合同创建')" label="项目名称" prop="projectName">
        <el-input
          v-model="ruleForm.projectName"
          placeholder="请输入项目名称"
          disabled
        />
      </el-form-item>
      <el-form-item label="核心企业名称" prop="enterprise">
        <el-input
          v-model="ruleForm.enterprise"
          placeholder="请输入核心企业名称"
          disabled
        />
      </el-form-item>
      <el-form-item label="供应商（主体）名称" prop="mainEnterprise">
        <el-input
          v-model="ruleForm.mainEnterprise"
          placeholder="请输入供应商（主体）名称"
          disabled
        />
      </el-form-item>
      <el-form-item v-if="module === '招投标管理' || (module === '销售合同管理' && taskName === '销售合同创建')" label="贸易平台" prop="tradingPlatformId">
        <el-select
          v-model="ruleForm.tradingPlatformId"
          placeholder="请与业务经理确认平台选择"
          clearable
          :disabled="disabled"
        >
          <el-option
            v-for="item in platformList"
            :key="item.id"
            :label="item.platformName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="(module === '销售合同管理' && taskName !== '销售合同创建')" label="贸易平台" prop="platformName">
        <el-input
          v-model="ruleForm.platformName"
          placeholder="请输入贸易平台"
          disabled
        />
      </el-form-item>
      <el-form-item label="业务编号" prop="businessNumber">
        <el-input
          v-model="ruleForm.businessNumber"
          placeholder="请输入业务编号"
          disabled
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProjectInformation, getAllPlatform } from '@/api/myTrade/biddingManagement/biddingManagement'
import { getCreditProject } from '@/api/myTrade/salesContractManagement/salesContractManagement'
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    detailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      formlabel: {
        company: '公司名称'
      },
      ruleForm: {
        projectInformationId: '',
        tradingPlatformId: '',
        enterprise: '',
        mainEnterprise: '',
        businessNumber: '',
        platformName: '',
        projectName: ''
      },
      rules: {
        projectInformationId: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
        tradingPlatformId: [{ required: true, message: '请选择贸易平台', trigger: 'change' }],
        enterprise: [{ required: true, message: '请输入核心企业名称', trigger: 'blur' }],
        mainEnterprise: [{ required: true, message: '请输入供应商（主体）名称', trigger: 'blur' }],
        businessNumber: [{ required: true, message: '请输入业务编号', trigger: 'blur' }]
      },
      type: '',
      taskName: '',
      module: '',
      entryNameList: [],
      platformList: []
    }
  },
  computed: {

  },
  watch: {
    detailsInfo: {
      handler(newV) {
        if (Object.keys(newV).length) {
          const { module } = this.$route.query
          if (module === '招投标管理') {
            const { basicInfo: { projectInformationId, enterprise, mainEnterprise, businessNumber, platform, projectName }} = JSON.parse(JSON.stringify(newV))
            this.ruleForm = {
              projectInformationId,
              enterprise,
              mainEnterprise,
              businessNumber,
              tradingPlatformId: platform.id,
              projectName
            }
          }
          if (module === '销售合同管理') {
            const { projectInformationId, enterprise, mainEnterprise, businessNumber, platformId, projectName, platformName } = JSON.parse(JSON.stringify(newV))
            this.ruleForm = {
              projectInformationId,
              projectName,
              enterprise,
              mainEnterprise,
              businessNumber,
              tradingPlatformId: platformId,
              platformName
            }
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    const { module, type, taskName } = this.$route.query
    this.type = type
    this.taskName = taskName
    this.module = module
    if (module === '招投标管理') {
      this.getProjectInformation()
      this.getAllPlatform()
    } else if (module === '销售合同管理' && taskName === '销售合同创建') {
      this.getCreditProject()
      if (type) {
        setTimeout(() => {
          this.projectChange()
        }, 200)
      }
    }
  },
  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleFormRef.validate(valid => {
          if (valid) {
            return resolve(this.ruleForm)
          } else {
            reject()
            return false
          }
        })
      })
    },
    getProjectInformation() {
      getProjectInformation().then(response => {
        this.entryNameList = response.data
      })
    },
    getCreditProject() {
      getCreditProject().then(response => {
        this.entryNameList = response.data
      })
    },
    getAllPlatform() {
      getAllPlatform().then(response => {
        this.platformList = response.data
      })
    },
    projectChange() {
      this.invoiceInformation = this.entryNameList.filter(item => item.projectInformationId === this.ruleForm.projectInformationId)[0] || []
      this.ruleForm.enterprise = this.invoiceInformation.enterprise
      this.ruleForm.mainEnterprise = this.invoiceInformation.mainEnterprise
      this.ruleForm.businessNumber = this.invoiceInformation.businessNumber
      const { module } = this.$route.query
      if (module === '销售合同管理') {
        this.platformList = this.invoiceInformation.platforms
        // 项目名称change后如果贸易平台的长度为1 默认选中
        if (this.platformList.length === 1) {
          this.ruleForm.tradingPlatformId = this.platformList[0].id
        }
      }
    },
    getParams() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleFormRef.validate(valid => {
          if (valid) {
            const params = {
              projectInformationId: this.ruleForm.projectInformationId,
              businessNumber: this.ruleForm.businessNumber,
              tradingPlatformId: this.ruleForm.tradingPlatformId
            }
            resolve(params)
          } else {
            reject()
            return false
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.basic-information {
}
</style>
