<template>
  <div class="customer-edit-content">
    <cy-info-title>客户信息</cy-info-title>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="120px" class="rule-form-orange">
      <div style="width: 100%">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="ruleForm.customerName" placeholder="请输入客户名称" :disabled="disabled" />
        </el-form-item>
      </div>
    </el-form>
    <cy-info-title>基础资料清单</cy-info-title>
    <div class="table-box">
      <el-table v-loading="loading" :data="tableData">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="profileName" label="资料名称">
          <template slot-scope="scope">
            <span>{{ scope.row.profileName }}</span>
            <span v-if="scope.$index < tableData.length - 3" style="color: #F56C6C"> *</span>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="说明"></el-table-column>
        <el-table-column prop="address" label="附件" width="300">
          <template slot-scope="scope">
            <div :style="{'padding-top': disabled ? '10px' : '10px', 'padding-bottom': disabled ? '0px' : '10px'}">
              <cy-upload :ref="'fileList'+scope.$index" :disabled="disabled" :upload-params="uploadParams" size="mini" :file-data="scope.row.fileStorageList"></cy-upload>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
export default {
  name: 'CustomerEditContent',
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    detailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        customerName: ''
      },
      rules: {
        customerName: [{ required: true, message: '请输入客户名称', trigger: 'blur' }]
      },
      loading: false,
      tableData: [],
      uploadParams: {
        mouldName: 'FinancialCustomer'
      }
    }
  },
  watch: {
    detailsInfo: {
      handler(newV) {
        if (newV.id) {
          const data = JSON.parse(JSON.stringify(newV))
          const { customerName, financialBasicFileVos } = data
          this.ruleForm.customerName = customerName
          this.tableData = financialBasicFileVos
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    getParams() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate(valid => {
          if (valid) {
            const { id, taskId } = this.$route.query
            const basicFiles = []
            this.tableData.forEach((item, index) => {
              const fileList = this.$refs['fileList' + index].getFileLists
              basicFiles.push({
                id: item.id,
                financialBasicFileDicInfoId: item.financialBasicFileDicInfoId,
                fileStorageIdList: fileList.map(item => item.id)
              })
            })
            const params = {
              financialCustomerInfoId: id,
              taskId,
              basicFiles
            }
            resolve(params)
          } else {
            reject()
          }
        })
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
