<template>
  <div class="business-situation">
    <cy-info-title type="center">企业经营情况</cy-info-title>
    <el-form
      :model="ruleForm"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item label="上游采购单据（请提供2家合作企业）" style="width: 100%;">
        <div class="table-box">
          <el-table
            :data="upstreamTableData"
            style="width: 100%"
          >
            <el-table-column prop="date" label="合作企业名称">
              <template slot-scope="scope">
                <cy-custom-select
                  :key="key"
                  :value.sync="scope.row.name"
                  :disabled="disabled"
                  @search-click="searchClick"
                >
                  <el-option
                    v-for="item in listData"
                    :key="item.corporateName"
                    :label="item.corporateName"
                    :value="item.corporateName"
                  />
                </cy-custom-select>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="上游采购单据（请上传验收单、结算单或对账单、发票、近期回款凭证）">
              <template slot-scope="scope">
                <cy-upload
                  :ref="`upfileStorage${scope.$index}`"
                  :upload-params="{
                    fileType: 'enterprise_upstream_documents'
                  }"
                  :disabled="disabled"
                  :file-data="scope.row.fileStorages"
                  default-desc="请上传上游采购合同、送货单、发票"
                />
              </template>
            </el-table-column>
            <el-table-column v-if="!disabled" prop="address" label="操作" width="150">
              <template slot-scope="scope">
                <el-tooltip effect="dark" content="添加" placement="top">
                  <span style="margin-right: 20px;">
                    <i class="iconfont icon-orange icon-a-Group812" @click="handlerOperate('add', scope, 'upstreamTableData')" />
                  </span>
                </el-tooltip>
                <el-tooltip effect="dark" content="删除" placement="top">
                  <span v-if="upstreamTableData.length > 2">
                    <i class="iconfont icon-orange icon-a-Group991" @click="handlerOperate('delete', scope, 'upstreamTableData')" />
                  </span>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form-item>
      <el-form-item label="下游销售单据（请提供2家合作企业）" style="width: 100%;">
        <div class="table-box">
          <el-table
            :data="downstreamTableData"
            style="width: 100%"
          >
            <el-table-column prop="date" label="国央企名称">
              <template slot-scope="scope">
                <cy-custom-select
                  :key="key"
                  :value.sync="scope.row.name"
                  :disabled="disabled"
                  @search-click="searchClick"
                >
                  <el-option
                    v-for="item in listData"
                    :key="item.corporateName"
                    :label="item.corporateName"
                    :value="item.corporateName"
                  />
                </cy-custom-select>
              </template>
            </el-table-column>
            <el-table-column prop="contractName" label="合同名称">
              <template slot-scope="scope">
                <el-input
                  :disabled="disabled"
                  v-model="scope.row.contractName"
                  placeholder="请输入合同名称"
                />
              </template>
            </el-table-column>
            <el-table-column prop="contractNumber" label="合同编号">
              <template slot-scope="scope">
                <el-input
                  :disabled="disabled"
                  v-model="scope.row.contractNumber"
                  placeholder="请输入合同编号"
                />
              </template>
            </el-table-column>
            <el-table-column prop="contractAmount" label="合同金额（元）">
              <template slot-scope="scope">
                <cy-amount-input
                  :disabled="disabled"
                  v-model="scope.row.contractAmount"
                  placeholder="请输入合同金额"
                />
              </template>
            </el-table-column>
            <el-table-column prop="contractDate" label="合同日期">
              <template slot-scope="scope">
                <el-date-picker
                  :disabled="disabled"
                  v-model="scope.row.contractDate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="请输入合同日期"
                />
              </template>
            </el-table-column>
            <el-table-column prop="name" label="下游销售单据（请上传验收单、结算单或对账单、发票、近期回款凭证）">
              <template slot-scope="scope">
                <cy-upload
                  :ref="`downfileStorage${scope.$index}`"
                  :upload-params="{
                    fileType: 'enterprise_sales_documents'
                  }"
                  :disabled="disabled"
                  :file-data="scope.row.fileStorages"
                  default-desc="请上传验收单、结算单或对账单、发票、近期回款凭证"
                />
              </template>
            </el-table-column>
            <el-table-column v-if="!disabled" prop="address" label="操作" width="150">
              <template slot-scope="scope">
                <el-tooltip effect="dark" content="添加" placement="top">
                  <span style="margin-right: 20px;">
                    <i class="iconfont icon-orange icon-a-Group812" @click="handlerOperate('add', scope, 'downstreamTableData')" />
                  </span>
                </el-tooltip>
                <el-tooltip effect="dark" content="删除" placement="top">
                  <span v-if="downstreamTableData.length > 2">
                    <i class="iconfont icon-orange icon-a-Group991" @click="handlerOperate('delete', scope, 'downstreamTableData')" />
                  </span>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEnterpriseInspectionInfo } from '@/api/myBusiness/supplierEnterprise'
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    businessInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {},
      listData: [],
      key: 0,
      upstreamTableData: [
        {
          name: '',
          fileStorages: []
        },
        {
          name: '',
          fileStorages: []
        }
      ],
      downstreamTableData: [
        {
          name: '',
          contractName: '',
          contractNumber: '',
          contractAmount: '',
          contractDate: '',
          fileStorages: []
        },
        {
          name: '',
          contractName: '',
          contractNumber: '',
          contractAmount: '',
          contractDate: '',
          fileStorages: []
        }
      ]
    }
  },
  watch: {
    businessInfo: {
      handler(newV) {
        if (newV && Object.keys(newV).length) {
          const { upstreamProcurementDocuments, salesDocumentsInfos } = JSON.parse(JSON.stringify(newV))
          this.upstreamTableData = upstreamProcurementDocuments.length ? upstreamProcurementDocuments : this.upstreamTableData
          this.downstreamTableData = salesDocumentsInfos.length ? salesDocumentsInfos : this.downstreamTableData
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    searchClick(val) {
      getEnterpriseInspectionInfo({ keyword: val }).then(({ data }) => {
        this.listData = data
      })
    },
    handlerOperate(optionType, { $index }, name) {
      if (optionType === 'add') {
        this[name].push({
          name: '',
          fileStorages: []
        })
      } else {
        this[name].splice($index, 1)
        this.key += 1
      }
    },
    getParams() {
      const upstreamProcurementDocuments = this.upstreamTableData.map((item, index) => {
        if (item?.name) {
          return {
            name: item.name,
            files: this.$refs[`upfileStorage${index}`].getFileLists.map(item => item.id)
          }
        }
      })
      const salesDocumentsInfos = this.downstreamTableData.map((item, index) => {
        if (item?.name) {
          return {
            name: item.name,
            contractName: item.contractName,
            contractNumber: item.contractNumber,
            contractAmount: item.contractAmount,
            contractDate: item.contractDate,
            files: this.$refs[`downfileStorage${index}`].getFileLists.map(item => item.id)
          }
        }
      })
      return {
        upstreamProcurementDocuments: upstreamProcurementDocuments.filter(item => item?.name),
        salesDocumentsInfos: salesDocumentsInfos.filter(item => item?.name)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.business-situation {

}
</style>

<style lang="scss">
.business-situation {
  .iconfont {
    font-size: 26px;
  }
}
</style>
