<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="130px">
      <headerTitle title="基本信息"></headerTitle>
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="应收日期"
            prop="receivableDate"
            style="margin-bottom: 22px">
            <el-date-picker
              style="width: 100%"
              v-model="form.receivableDate"
              type="date"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="应收金额"
            prop="receivableTotalPrice"
            style="margin-bottom: 22px">
            <el-input-number disabled v-model="form.receivableTotalPrice" :precision="2" :controls="false"
                             style="width: 100%"
                             :min="0"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="会计科目"
            prop="accountingSubjectId"
            style="margin-bottom: 22px">
            <ele-tree-select
              clearable
              filterable
              ref="treeSelect"
              :data="accountingSubjectList"
              v-model="form.accountingSubjectId"
              valueKey="id"
              labelKey="name"
              placeholder="请选择"
              @change="changeSubjectInfo"
              default-expand-all
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="发票编码"
            prop="invoiceCode"
            style="margin-bottom: 22px">
            <el-input disabled v-model="form.invoiceCode"></el-input>
          </el-form-item>
        </el-col>


      </el-row>
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="来源类型"
            prop="sourceType"
            style="margin-bottom: 22px">
            <el-input value="销售订单" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="来源编码"
            prop="sourceCode"
            style="margin-bottom: 22px">
            <el-input v-model="form.sourceCode" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="公司名称"
            prop="contactName"
            style="margin-bottom: 22px">
            <el-input disabled v-model="form.contactName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="附件">
            <fileUpload
              v-model="form.files"
              module="main"
              :showLib="false"
              :limit="10"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {getFile} from '@/api/system/file';
import dictMixins from '@/mixins/dictMixins';
import fileUpload from "@/components/upload/fileUpload.vue";

import personSelect from "@/components/CommomSelect/person-select.vue";

import {invoiceApplyInfoAPI} from "@/api/bpm/components/financialManage/invoiceManage";
import tableInfo from "@/views/bpm/handleTask/components/financialManage/invoiceManage/components/tableInfo.vue";
import {finReceivableUpdateAPI, getFinReceivableInfoAPI} from "@/api/bpm/components/financialManage/receivableManage";
import {getTreeByPid} from "@/api/classifyManage";


export default {
  mixins: [dictMixins],
  components: {
    tableInfo,
    personSelect,
    fileUpload
  },
  data() {
    return {
      dialogType: '',
      form: {
        accountingSubjectCode: "",
        accountingSubjectId: '',
        accountingSubjectName: "",
        contactId: '',
        contactName: "",
        files: [],
        id: '',
        invoiceCode: "",
        receivableDate: "",
        receivableTotalPrice: '',
        receivedTotalPrice: '',
        remark: "",
        sourceCode: "",
        sourceId: "",
        sourceType: '',
        unreceiveTotalPrice: 0
      },
      accountingSubjectList: [],
      rules: {
        accountingSubjectId: {required: true, message: '请选择', trigger: 'change'},
        receivableDate: {required: true, message: '请选择', trigger: 'change'},
      }
    };
  },
  props: {
    businessId: {
      default: ''
    },
    taskDefinitionKey: {
      default: ''
    },
  },
  async created() {
    await this.getInfo(this.businessId)
  },
  methods: {
    //获取详情
    async getInfo(id) {
      await this.getClassifyList(24, 'accountingSubjectList')
      let data = await getFinReceivableInfoAPI(id)
      this.form = data
      let invoiceData = await invoiceApplyInfoAPI(data.invoiceId)
      this.form.accountingSubjectCode = invoiceData.accountingSubjectCode
      this.form.accountingSubjectId = invoiceData.accountingSubjectId
      this.form.accountingSubjectName = invoiceData.accountingSubjectName
    },
    downloadFile(file) {
      getFile({objectName: file.storePath}, file.name);
    },

    //获取分类管理中的数据
    async getClassifyList(id, listName) {
      let res = await getTreeByPid(id)
      this[listName] = res.data
    },
    //获取会计科目选中数据
    changeSubjectInfo(val) {
      if (!val) return this.form.accountingSubjectName = ''
      let data = this.$refs.treeSelect?.$refs?.tree?.getCurrentNode() || {}
      this.form.accountingSubjectName = data.name
    },
    getTableValue() {
      return new Promise((resolve, reject) => {
        this.$refs.form.validate(async valid => {
          if (!valid) return this.$message.warning('有必填项未填，请检查')
          const API = finReceivableUpdateAPI
          const res = await API(this.form)
          res.code == '0' ? resolve('1') : reject(new Error(data.message))
        })
      })

    },
    cancel() {
      this.$emit('update:detailDialogFlag', false)
    }
  }
};
</script>

<style scoped lang="scss">
.ele-dialog-form {
  .el-form-item {
    margin-bottom: 10px;
  }
}

.headbox {
  display: flex;
  justify-content: flex-start;
  align-items: center;

  .amount {
    font-size: 14px;
    font-weight: bold;
    margin-right: 20px;
  }
}
</style>
