<template>
  <div v-loading="loading" class="add-block">
    <baseTitle title="研发资金信息录入"></baseTitle>
    <el-form :model="formData" ref="fundForm" :rules="rules" label-width="130" style="margin-left: 5px;margin-bottom: -18px">
      <el-row gutter="30">
        <el-col :span="24" >
          <el-form-item label="研发公司名称" :required="true" prop="" class="company-select">
            <div style="width: 100%">
              <el-button color="#DED0B2" @click="showCompany">{{ selectedCompanyList.length === 0 ? '请选择研发公司' : '更改' }}
              </el-button>
            </div>
            <div :class="showExpendClass(showMoreCompany)">{{ getName(selectedCompanyList) }}</div>
            <div style="color: #2a99ff;text-align: center;width: 100%;font-size: 15px;cursor: pointer"
                 @click="handleExpend">
              {{ showExpendText }}
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="研发资金金额(元)" prop="rdAmount" >
            <el-input
                v-model="formData.rdAmount"
                placeholder="请输入研发资金金额"
                clearable
                :formatter="(value) => value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')"
                :parser="(value) => value.replace(/\$\s?|(,*)+[^0-9.]/g, '')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="研发资金日期" prop="rdDate">
            <el-date-picker
                v-model="formData.rdDate"
                type="month"
                format="YYYY-MM"
                value-format="YYYY-MM"
                placeholder="请选择研发资金日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6" >
          <el-form-item label="备注" prop="remarks" style="margin-left: -70px">
            <el-input v-model="formData.remarks" placeholder="请输入备注" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="oper-page-btn">
      <el-button color="#DED0B2" v-if="routerName === 'Devfund/add'" @click="handleSubmit(fundForm)">提交</el-button>
      <el-button color="#DED0B2" v-else @click="handleResubmit">提交</el-button>
      <el-button @click="handleBack">返回</el-button>
    </div>
    <company-picker :multiple="false" ref="companyRef" title="请选择研发公司" @ok="sureSelectedCompany" @cancelOrClear="cancelSelectedCompany"
                    v-model:value="selectedCompanyList"/>
  </div>
</template>

<script setup lang="jsx">
import {ElNotification} from "element-plus";
import {addResearchFund, editResearchFund, getResearchFundDetail} from "@/api/research-fund";
import {useTagsView} from '@/stores/tagsview.js'
import {downloadFile, deleteFile} from "@/api/project-demand";

const tagsViewStore = useTagsView()
const router = useRouter()
const route = useRoute()
const loading = ref(false)
const fundForm = ref()
const companyRef = ref()
const showMoreCompany = ref(false)
const showExpendText = ref('')
const selectedCompanyList = ref([])
const formData = ref({})
const routerName = ref(router.currentRoute.value.name)
const rules = reactive({
  rdAmount: [{required: true, message: '请输入研发资金金额', trigger: ['blur', 'change']}],
  rdDate: [{required: true, message: '请选择研发资金日期', trigger: ['blur', 'change']}],
})

const getName = (list) => {
  return list.map(item => item.label).join('，')
}
const showCompany = () => {
  companyRef.value.show()
}

const sureSelectedCompany = (select) => {
  if(select&&select.length>0){
    formData.value.rdCompanyId = select[0].value
    formData.value.rdCompany = select[0].label
    selectedCompanyList.value = select
  }
}

const cancelSelectedCompany=(select)=>{
  // formData.value.rdCompanyId =''
  // formData.value.rdCompany =''
  selectedCompanyList.value = select
}
const handleExpend = () => {
  showMoreCompany.value = !showMoreCompany.value;
  showExpendClass(showMoreCompany.value)
}

const showExpendClass = (showMoreCompany) => {
  if (!showMoreCompany) {
    if (selectedCompanyList.value.length === 0) {
      showExpendText.value = ''
      return ''
    } else if (selectedCompanyList.value.length > 14) {
      showExpendText.value = '展开'
      return 'company-style'
    }
  } else {
    showExpendText.value = '收缩'
    return ''
  }
}
const handleBack = () => {
  history.back()
}

const submitParam = (item) => {
  return {
    rdCompanyId: item.rdCompanyId,
    rdCompany: item.rdCompany,
    rdAmount: item.rdAmount,
    rdDate: item.rdDate,
    remarks: item.remarks,
  }
}
const handleSubmit = async (instance) => {
  if (!instance) return
  instance.validate(async (valid) => {
    if (!valid) {
      ElNotification({
        title: '提示',
        message: '请完善数据,再提交!',
        type: 'error'
      })
      return;
    }
    if (!formData.value.rdCompanyId) {
      ElNotification({
        title: '提示',
        message: '请选择研发公司',
        type: 'error'
      })
      return;
    }
    const {msg, code} = await addResearchFund(submitParam(formData.value))
    ElNotification({
      title: '提示',
      message: msg,
      type: code === 1000 ? 'success' : 'error'
    })
    if (code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      await router.push({
        name: 'Dev'
      })
    }
  })
}
const handleResubmit = () => {
  if (!route.query.id) return
  let params = {
    rdFundId: route.query.id,
    ...submitParam(formData.value)
  }
  editResearchFund(params).then(res => {
    ElNotification({
      title: '提示',
      message: res.msg,
      type: res.code === 1000 ? 'success' : 'error'
    })
    if (res.code === 1000) {
      tagsViewStore.delVisitedViews(router.currentRoute.value.path)
      router.push({
        name: 'Dev'
      })
    }
  })
}
const getDetailInfo = async () => {
  getResearchFundDetail(route.query.id).then(res => {
    if (res.code === 1000) {
      console.info("🚀 ~method:res.data -----", res.data)
      if (res.data.rdCompanyId == -1) {
        selectedCompanyList.value = [
          {
            value: -1,
            label: '所有公司'
          }
        ]
      } else {
        selectedCompanyList.value = [ {
          value: res.data.rdCompanyId,
          label: res.data.rdCompany
        }]
      }
      formData.value = res.data
    }else{
      ElNotification({
        title: '提示',
        message: res.msg,
        type: 'error'
      })
    }
  })
}
onMounted(async () => {
  loading.value = true
  if (route.query.id) {
    await getDetailInfo()
  }
  loading.value = false
})
</script>

<style scoped lang="scss">
.company-select {
  :deep(.el-form-item__content .el-select__wrapper ) {
    width: 750px;
  }

  .company-style {
    //width: 98%;
    min-height: 30px;
    max-height: 60px;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
  }

  .company {
    color: #fff;
    height: auto;
  }
}
:deep(.el-table--fit ) {
  height: 300px !important;
}

.add-block {
  padding: 0 30px;
}

:deep(.el-input-number) {
  width: 88.5%;
  .el-input__wrapper{
    padding: 1px 11px!important;
  }
  .el-input__inner {
    text-align: left;
  }
}

:deep(.el-table__header) {
  .is-leaf:first-child {
    .cell {
      margin-left: -20px !important;
    }
  }
}

:deep(.el-table__body) {
  .el-table__cell:first-child {
    .cell {
      margin-left: -10px !important;
    }
  }
}
:deep(.el-date-editor--month) {
  width: 100%;
}
</style>
