<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 label="项目名称" prop="projectName">
        <el-select
          v-model="ruleForm.projectName"
          placeholder="请选择项目名称"
          clearable
          :disabled="basicDisabled"
          @change="projectChange"
        >
          <el-option
            v-for="item in findContractInfo"
            :key="item.projectName"
            :label="item.projectName"
            :value="item.projectName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="核心企业名称" prop="coreEnterpriseName">
        <el-input
          v-model="ruleForm.coreEnterpriseName"
          placeholder="请输入核心企业名称"
          disabled
        />
      </el-form-item>
      <el-form-item label="供应商（主体）名称" prop="mainEnterpriseName">
        <el-input
          v-model="ruleForm.mainEnterpriseName"
          placeholder="请输入供应商（主体）名称"
          disabled
        />
      </el-form-item>
      <el-form-item 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-item v-if="taskName === '变更创建'" label="变更类型">
        <el-select v-model="ruleForm.value" placeholder="请选择变更类型">
          <el-option
            v-for="item in []"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item> -->
    </el-form>
  </div>
</template>

<script>
import { getLocalPurchCompletedProject, getPurchCompleted, getFindCustomer } from '@/api/mySettlement/invoicesManagement'
export default {
  props: {
    basicDisabled: {
      type: Boolean,
      default: false
    },
    basicInformation: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      rules: {
        projectName: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
        coreEnterpriseName: [{ required: true, message: '请输入核心企业名称', trigger: 'blur' }],
        mainEnterpriseName: [{ required: true, message: '请输入供应商（主体）名称', trigger: 'blur' }],
        platformName: [{ required: true, message: '请输入贸易平台', trigger: 'blur' }],
        businessNumber: [{ required: true, message: '请输入业务编号', trigger: 'blur' }]
      },
      taskName: '',
      findContractInfo: [],
      ruleForm: {
        projectName: '',
        coreEnterpriseName: '',
        mainEnterpriseName: '',
        platformName: '',
        businessNumber: '',
        platformId: '',
        coreEnterpriseId: '',
        businessBasicInfoId: ''
      }
    }
  },
  computed: {

  },
  watch: {
    basicInformation: {
      handler(newV) {
        if (this.basicInformation && Object.keys(newV).length) {
          this.ruleForm = this.basicInformation
          this.ruleForm.coreEnterpriseId = this.basicInformation.enterpriseCustomerInfoId
          this.ruleForm.platformId = this.basicInformation.tradingPlatformId
          this.ruleForm.businessBasicInfoId = this.basicInformation.businessBasicInfoId
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    const { taskName } = this.$route.query
    this.taskName = taskName
    this.getLocalPurchCompletedProject()
  },
  methods: {
    validate() {
      this.$refs.ruleFormRef.value.validate(valid => {
        if (valid) {
          console.log(valid)
        } else {
          console.log(1111)
        }
      })
    },
    getLocalPurchCompletedProject() {
      getLocalPurchCompletedProject().then(response => {
        this.findContractInfo = response.data
      })
    },
    projectChange() {
      this.ruleForm.businessNumber = this.findContractInfo.filter(item => item.projectName === this.ruleForm.projectName)[0].businessNumber
      this.ruleForm.businessBasicInfoId = this.findContractInfo.filter(item => item.projectName === this.ruleForm.projectName)[0].id
      getPurchCompleted({ businessNumber: this.ruleForm.businessNumber }).then(response => {
        this.ruleForm.coreEnterpriseName = response.data.coreEnterpriseName
        this.ruleForm.coreEnterpriseId = response.data.coreEnterpriseId
        this.ruleForm.mainEnterpriseName = response.data.mainEnterpriseName
        this.ruleForm.platformName = response.data.platformName
        this.ruleForm.platformId = response.data.platformId
        getFindCustomer({ customerId: this.ruleForm.coreEnterpriseId, businessBasicInfoId: this.ruleForm.businessBasicInfoId }).then(data => {
          this.$emit('invoiceInformation', data.data, this.ruleForm.businessNumber)
        })
      })
      // this.ruleForm.corporateName = this.invoiceInformation.buyerName
    },
    getParams() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleFormRef.validate(valid => {
          if (valid) {
            const { coreEnterpriseId, platformId, businessNumber, businessBasicInfoId } = this.ruleForm
            const params = {
              businessNumber,
              enterpriseCustomerInfoId: coreEnterpriseId,
              tradingPlatformId: platformId,
              businessBasicInfoId
            }
            resolve(params)
          } else {
            reject()
            return false
          }
        })
      })
    }
  }
}
</script>

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