<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="130px">
      <headerTitle title="基本信息"></headerTitle>
      <el-row :gutter="12">
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item
              label="发票类型"
              prop="type"
              style="margin-bottom: 22px">
              <el-select v-model="form.type" @change="handleSelType" disabled clearable style="width: 100%">
                <el-option label="开票" :value="1"></el-option>
                <el-option label="收票" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="金额"
              prop="amount"
              style="margin-bottom: 22px">
              <el-input-number v-model="form.amount" :precision="2" :disabled="taskDefinitionKey!='starter'"
                               :controls="false" style="width: 100%"
                               :min="0"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-col :span="12">
          <el-form-item
            label="申请部门"
            prop="applyDeptId"
            style="margin-bottom: 22px"
          >
            <ele-tree-select
              :disabled="taskDefinitionKey!='starter'"
              clearable
              :data="deptTreeList"
              v-model="form.applyDeptId"
              valueKey="id"
              labelKey="name"
              placeholder="请选择"
              @change="changeDeptInfo"
              default-expand-all
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="申请人"
            prop="applyUserId"
            style="margin-bottom: 22px"
          >
            <personSelect
              :disabled="taskDefinitionKey!='starter'"
              ref="directorRef"
              v-model="form.applyUserId"
              @selfChange="changeUserInfo"
            />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="预计开票日期"
            style="margin-bottom: 22px">
            <el-date-picker
              :disabled="taskDefinitionKey!='starter'"
              style="width: 100%"
              v-model="form.expectInvoiceDate"
              type="date"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            v-if="taskDefinitionKey!='starter'"
            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-row>
      <el-row :gutter="12" v-if="taskDefinitionKey!='starter'">
        <el-col :span="12">
          <el-form-item prop="files" label="发票附件">
            <fileUpload
              v-model="form.files"
              module="main"
              :showLib="false"
              :limit="10"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <headerTitle title="开票信息"></headerTitle>
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="公司名称"
            prop="companyName"
            style="margin-bottom: 22px">
            <el-input v-model="form.companyName" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="统一社会信用代码"
            prop="companyUsc"
            style="margin-bottom: 22px">
            <el-input v-model="form.companyUsc" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>


      </el-row>
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="开户银行"
            prop="companyBankBranchNo"
            style="margin-bottom: 22px">
            <el-input v-model="form.companyBankBranchNo" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">

          <el-form-item
            label="银行账户"
            prop="companyBankAccount"
            style="margin-bottom: 22px">
            <el-input v-model="form.companyBankAccount" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>

      </el-row>

      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="单位地址"
            prop="companyAddress"
            style="margin-bottom: 22px">
            <el-input v-model="form.companyAddress" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="电话号码"
            prop="companyTel"
            style="margin-bottom: 22px">
            <el-input v-model="form.companyTel" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <headerTitle title="收票信息"></headerTitle>


      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="收票人"
            prop="receiverName"
            style="margin-bottom: 22px"
          >
            <el-input v-model="form.receiverName" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="收票人地址"
            prop="receiverAddress"
            style="margin-bottom: 22px"
          >
            <el-input v-model="form.receiverAddress" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item
            label="联系方式"
            prop="receiverTel"
            style="margin-bottom: 22px">
            <el-input v-model="form.receiverTel" :disabled="taskDefinitionKey!='starter'"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <headerTitle title="关联信息"></headerTitle>
    <table-info v-if="form.detailList.length" :dialogType="taskDefinitionKey!='starter'?'view':'update'"
                :form="form" ref="table" @setPrice="setPrice"></table-info>
  </div>

</template>
<script>
import personSelect from "@/components/CommomSelect/person-select.vue";
import {listOrganizations} from "@/api/system/organization";
import {mapGetters} from "vuex";
import fileUpload from "@/components/upload/fileUpload.vue";
import tableInfo from "./tableInfo.vue";
import {getTreeByPid} from "@/api/classifyManage";
import {
  invoiceApplyInfoAPI,
  invoiceApplyUpdateAPI
} from "@/api/bpm/components/financialManage/invoiceManage";


export default {
  name: "add-or-edit-dialog",
  components: {
    fileUpload,
    personSelect,
    tableInfo
  },
  props: {
    businessId: {
      default: ''
    },
    taskDefinitionKey: {
      default: ''
    },
  },
  provide() {
    return {
      invoiceType: () => this.form.type
    }
  },
  computed: {
    ...mapGetters(['user'])
  },
  data() {
    return {
      dialogType: '',
      title: '',
      form: {
        accountingSubjectCode: "",
        accountingSubjectId: '',
        accountingSubjectName: "",
        amount: undefined,
        applyDeptId: '',
        applyDeptName: "",
        applyUserId: '',
        applyUserName: "",
        companyAddress: "",
        companyBankAccount: "",
        companyUsc: "",
        expectInvoiceDate: "",
        companyBankBranchNo: "",
        companyName: "",
        receiverAddress: "",
        receiverName: "",
        receiverTel: "",
        companyTel: "",
        detailList: [],
        drawerId: '',
        drawerName: "",
        files: [],
        invoiceStatus: '',
        link: [],
        remark: "",
        settlementAccountId: '',
        settlementAccountName: "",
        type: ''
      },
      feeTypeList: [],
      accountingSubjectList: [],
      deptList: [],
      deptTreeList: [],
      rules: {
        applyDeptId: {required: true, message: '请选择', trigger: 'change'},
        applyUserId: {required: true, message: '请选择', trigger: 'change'},
        accountingSubjectId: {required: true, message: '请选择', trigger: 'change'},
        feeTypeId: {required: true, message: '请选择', trigger: 'change'},
        amount: {required: true, message: '请输入', trigger: 'blur'},
        type: {required: true, message: '请选择', trigger: 'change'},
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    //初始化
    async init() {
      await this.getClassifyList(24, 'accountingSubjectList')
      await this.getDeptList()
      await this.getInfo(this.businessId)
      this.$nextTick(async () => {
        await this.getUserList(this.form.applyDeptId);
      })
    },
    //获取详情
    async getInfo(id) {
      this.form = await invoiceApplyInfoAPI(id)
    },
    //获取分类管理中的数据
    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
    },
    // 获取部门数据
    getDeptList() {
      listOrganizations().then((list) => {
        this.deptList = list;
        this.deptTreeList = this.$util.toTreeData({
          data: list,
          idField: 'id',
          parentIdField: 'parentId'
        });
      });
    },
    // 选择负责人部门
    changeDeptInfo(id) {
      const info = this.deptList.find((e) => e.id == id) || {};
      this.form.applyDeptName = info.name;
      this.form.applyUserId = '';
      this.form.applyUserName = '';
      this.getUserList(id);
    },
    // 获取人员数据
    getUserList(deptId) {
      if (deptId) {
        this.$refs.directorRef.getList({deptId});
      }
    },
    //选择人员数据
    changeUserInfo(val, info) {
      this.form.applyUserName = info.name;
    },
    //
    handleSelType(val) {
      this.$refs.table.clearTable()
    },
    setPrice(val) {
      this.form.amount = val
    },
    //
    getTableValue() {
      return new Promise((resolve, reject) => {
        this.$refs.form.validate(async valid => {
          if (!valid) return this.$message.warning('有必填项未填，请检查')
          let tableData = await this.$refs.table.getTableValidate()
          this.form.link = tableData.link
          this.form.detailList = tableData.detailList
          const API = invoiceApplyUpdateAPI
          const res = await API(this.form)
          res.code == '0' ? resolve('1') : reject(new Error(data.message))
        })
      })

    },
  }
}
</script>
<style scoped lang="scss">

</style>
