<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="1200px"
      @close="close"
      @keyup.enter.native="doSubmit"
      :visible.sync="visible">
      <el-form size="middle" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
               label-width="100px" @submit.native.prevent>

        <el-divider content-position="left"><i class="el-icon-document"></i> 基础信息</el-divider>
        <el-row :gutter="26">
          <el-col :span="12">
            <el-form-item label="经办人" prop="userName">
              <el-input v-model="inputForm.userName" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报销编号" prop="no">
              <el-input v-model="inputForm.no" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="department">
              <el-input v-model="inputForm.department" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报销日期" prop="reimDate">
              <el-date-picker
                v-model="inputForm.reimDate"
                type="date"
                value-format="yyyy-MM-dd"
                style="width: 100%"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="inputForm.remarks"
                        type="textarea"
                        :rows="5"
                        maxlength="500"
                        placeholder="请输入简介"
                        show-word-limit>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="left"><i class="el-icon-document"></i>
          报销详情
          <el-button style="margin-left: 20px" type="primary" :disabled="method==='view'" size="mini" @click="insertEvent('detail')" plain>
            新增
          </el-button>
        </el-divider>
        <el-row  :gutter="15" >
          <vxe-table
            border
            show-footer
            show-overflow
            :footer-method="footerMethod"
            ref="detailTable"
            class="vxe-table-element"
            :data="inputForm.detailInfos"
            style="margin-left: 5em"
            @cell-click=""
            @edit-closed=""
            highlight-current-row
            :edit-config="{trigger: 'click', mode: 'row', showStatus: false, autoClear: true, icon: '-'}"
          >
            <vxe-table-column field="userName" title="报销人" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.userName" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="deptName" title="报销部门" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.deptName" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="typeName" title="报销类型" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.typeName" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="projectName" title="报销项目" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.projectName" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="reportNumber" title="报告号" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.reportNumber" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="number" title="费用(元)" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.number" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="receiptNumber" title="收据张数" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.receiptNumber" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="days" title="出差天数" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.days" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="content" title="内容" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.content" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column title="操作" width="100">
              <template v-slot="scope">
                <el-button size="mini" type="danger" @click="removeEvent(scope.row,scope.$rowIndex,'detail')">删除</el-button>
              </template>
            </vxe-table-column>
          </vxe-table>
        </el-row>

        <el-divider content-position="left"><i class="el-icon-document"></i>
          专用发票信息
          <el-button style="margin-left: 20px" type="primary" :disabled="method==='view'" size="mini" @click="insertEvent('amount')" plain>
            新增
          </el-button>
        </el-divider>
        <el-row  :gutter="15" >
          <vxe-table
            border
            show-overflow
            ref="amountTable"
            class="vxe-table-element"
            :data="inputForm.amountInfos"
            style="margin-left: 5em"
            @cell-click=""
            @edit-closed=""
            highlight-current-row
            :edit-config="{trigger: 'click', mode: 'cell', showStatus: true, autoClear: true}"
          >
            <vxe-table-column field="code" title="发票代码" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.code" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="number" title="发票号" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.number" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="amount" title="金额" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.amount" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="taxAmount" title="税额" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.taxAmount" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column field="count" title="价税合计" :edit-render="{}">
              <template v-slot:edit="scope">
                <el-input v-model="scope.row.count" ></el-input>
              </template>
            </vxe-table-column>
            <vxe-table-column title="操作" width="100">
              <template v-slot="scope">
                <el-button size="mini" type="danger" @click="removeEvent(scope.row,scope.$rowIndex,'amount')">删除</el-button>
              </template>
            </vxe-table-column>
          </vxe-table>
        </el-row>

        <!-- 附件 -->
        <UpLoadComponent ref="uploadComponent"></UpLoadComponent>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="close()" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  // import ReimbursementService from '@/api/sys/ReimbursementService'
  import ReimbursementApprovalService from '@/api/cw/reimbursementApproval/ReimbursementApprovalService'
  import UpLoadComponent from '@/views/common/UpLoadComponent'
  import XEUtils from 'xe-utils'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          userName: '',
          no: '',
          department: '',
          reimDate: '',
          remarks: '',
          detailInfos: [],
          amountInfos: [],
          files: [] // 附件信息
        }
      }
    },
    // reimbursementService: null,
    reimbursementApprovalService: null,
    created () {
      // this.reimbursementService = new ReimbursementService()
      this.reimbursementApprovalService = new ReimbursementApprovalService()
    },
    components: {
      UpLoadComponent
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm = {
          userName: '',
          no: '',
          department: '',
          reimDate: '',
          remarks: '',
          detailInfos: [],
          amountInfos: [],
          files: [] // 附件信息
        }
        if (method === 'add') {
          this.title = `新建报销类型`
        } else if (method === 'edit') {
          this.inputForm.id = id
          this.title = '修改报销类型'
        } else if (method === 'view') {
          this.inputForm.id = id
          this.title = '查看报销类型'
        } else if (method === 'addChild') {
          this.title = '添加下级结构'
          this.inputForm.parentId = id
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$refs.inputForm.resetFields()
            this.reimbursementApprovalService.findById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'reimbursement')
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.loading = false
            })
            // this.reimbursementService.findById(this.inputForm.id).then(({data}) => {
            //   this.inputForm = this.recover(this.inputForm, data)
            //   this.$refs.uploadComponent.newUpload(method, this.inputForm.files, 'reimbursement')
            //   this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
            //   this.loading = false
            // })
          }
        })
      },
      footerMethod ({ columns, data }) {
        const footerData = [
          columns.map((column, columnIndex) => {
            if (columnIndex === 0) {
              return '总报销费用'
            }
            if (['number'].includes(column.property)) {
              // eslint-disable-next-line no-undef
              return XEUtils.sum(data, column.property)
            }
            return null
          })
        ]
        return footerData
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            if (this.$refs.uploadComponent.checkProgress()) {
              this.loading = false
              return
            }
            if (this.commonJS.isEmpty(this.inputForm.files)) {
              this.inputForm.files = []
            }
            this.inputForm.files = this.$refs.uploadComponent.getDataList()
            this.reimbursementApprovalTypeService.save(this.inputForm).then(({data}) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.inputForm.resetFields()
        this.inputForm.detailInfos = []
        this.inputForm.amountInfos = []
        this.$refs.uploadComponent.clearUpload()
        this.visible = false
      },
      // 删除
      removeEvent (row, rowIndex, type) {
        if (type === 'detail') {
          this.$refs.detailTable.remove(row)
          this.inputForm.detailInfos.splice(rowIndex, 1)
        }
        if (type === 'amount') {
          this.$refs.amountTable.remove(row)
          this.inputForm.amountInfos.splice(rowIndex, 1)
        }
      },
      // 新增
      async insertEvent (type) {
        if (type === 'detail') {
          await this.$refs.detailTable.insert().then((data) => {
            this.inputForm.detailInfos.push(data)
          })
        }
        if (type === 'amount') {
          await this.$refs.amountTable.insert().then((data) => {
            this.inputForm.amountInfos.push(data)
          })
        }
      }
    }
  }
</script>
