<template>
  <a-card :bordered="false" v-show="visible" class="card" title="审核">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button type="primary" @click="save()" :disabled="disabled">提交</a-button>
          <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>

    <title-divider title="基本信息" width="90px"></title-divider>
    <detail-list title="" :col="3">
      <detail-list-item term="单号">{{ model.billNo }}</detail-list-item>
      <detail-list-item term="公司名称">{{ model.companyName }}</detail-list-item>
      <detail-list-item term="项目名称">{{ model.projectName }}</detail-list-item>
      <detail-list-item term="项目记账主币">{{ BaseTool.Object.getField(moneyTypeMap,model.projectMoneyType) }}</detail-list-item>
      <detail-list-item term="类型">{{ BaseTool.Object.getField(typeMap,model.type) }}</detail-list-item>
      <detail-list-item term="呈报">{{ BaseTool.Object.getField(reportTypeMap,model.reportType) }}</detail-list-item>
      <detail-list-item term="需求日期">{{ model.demandDate }}</detail-list-item>
      <detail-list-item term="是否需要合同">{{ model.contractFlag ? '是' : '否' }}</detail-list-item>
      <detail-list-item term="是否需要发运">{{ model.dispatchFlag ? '是' : '否' }}</detail-list-item>
      <!--      <detail-list-item term="事项">{{ model.item }}</detail-list-item>-->
      <!--      <detail-list-item term="设备物资综述">{{ model.summary }}</detail-list-item>-->
      <!--      <detail-list-item term="特殊情况">{{ model.specialDescription }}</detail-list-item>-->
      <detail-list-item term="预计总金额">{{ BaseTool.Amount.formatter(model.planMoney)+ BaseTool.Number.divForDollar(model.planMoney, model.dollarExchangeRate, 2) }}</detail-list-item>
      <detail-list-item term="数量">{{ BaseTool.Amount.formatter(model.quantity) }}</detail-list-item>
      <detail-list-item term="备注">{{ model.remark }}</detail-list-item>
      <detail-list-item term="更新人名称">{{ model.updateUserName }}</detail-list-item>
      <detail-list-item term="更新时间">{{ model.updateTime }}</detail-list-item>
    </detail-list>
    <title-divider title="采购明细" width="90px"></title-divider>
    <purchase-apply-list-audit ref="applyList" :viewFlag="true" @loadOk="loadOk"></purchase-apply-list-audit>
    <title-divider title="审核信息" width="90px"></title-divider>
    <a-row class="form-row" :gutter="16">
      <a-col :lg="12" :md="12" :sm="24" v-if="reportFlagExist">

        <a-form-item
          label="是否转发上一级"
        >
          <a-checkbox v-model="reportFlag" ></a-checkbox>
        </a-form-item>
      </a-col>
      <a-col :lg="12" :md="12" :sm="24" v-if="regionGroupFlag">

        <a-form-item
          label="指定采购人员"
        >
          <a-select
            style="width: 50%"
            v-model="purchaserId"
            placeholder="请选择">
            <a-select-option
              v-for="item in users"
              :key="item.userId"
              :label="item.realName"
              :value="item.userId">{{ item.realName }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-col>
      <a-col :lg="12" :md="12" :sm="24" v-if="meetingFlag">

        <a-form-item
          label="会议纪要"
        >
          <upload-file :max-size="1" ref="upload" @catchFile="catchFile"></upload-file>
        </a-form-item>
      </a-col>
    </a-row>
    <a-row class="form-row" :gutter="16">
      <a-col :lg="12" :md="12" :sm="24">

        <a-radio-group v-model="auditFlag">
          <a-radio :value="1">同意</a-radio>
          <a-radio :value="0">不同意</a-radio>
        </a-radio-group>
      </a-col>
    </a-row>
    <a-row class="form-row" :gutter="16">
      <a-col :lg="12" :md="12" :sm="24">
        <a-form-item
          label="审核描述"
        >
          <a-textarea
            v-model="refuseReason"/>
        </a-form-item>
      </a-col>
    </a-row>

  </a-card>
</template>

<script>
import DetailList from '@/components/tools/DetailList'
import UploadFile from '@/components/Upload/UploadFile'
import { audit, fetchPurchaseApply } from '@/api/purchase/purchase-apply'
import { queryProjectRole } from '@/api/upms/user'
import { configData } from '@/api/upms/config'
import PurchaseApplyListAudit from '../PurchaseApplyListAudit'
const DetailListItem = DetailList.Item

export default {
  name: 'PurchaseApplyAudit',
  components: {
    DetailList,
    DetailListItem,
    UploadFile,
    PurchaseApplyListAudit
  },
  data () {
    return {
      confirmLoading: false,
      mdl: {},
      modalTitle: null,
      auditFlag: 1,
      users: [],
      refuseReason: '同意',
      taskId: '',
      visible: false,
      user: this.$store.getters.userInfo,
      fileUrl: null,
      purchaserId: null,
      purchaserName: null,
      userMap: {},
      fileName: null,
      reportFlagExist: false,
      disabled: true,
      regionGroupFlag: false,
      reportFlag: false,
      rmbRate: 1,
      meetingFlag: false,
      // 下拉框map
      typeMap: {},
      configMap: {},
      auditMoneyInfo: {},
      moneyTypeMap: {},
      reportTypeMap: {},
      periodTypeMap: {},
      model: {
        'billNo': null,
        'demandPlanId': null,
        'demandPlanName': null,
        'companyId': null,
        'companyName': null,
        'projectId': null,
        'projectName': null,
        'projectMoneyType': null,
        'dollarExchangeRate': null,
        'type': null,
        'regionId': null,
        'groupId': null,
        'planDate': null,
        'reportType': null,
        'demandDate': null,
        'item': null,
        'contractFlag': null,
        'dispatchFlag': null,
        'year': null,
        'periodValue': null,
        'periodType': null,
        'summary': null,
        'specialDescription': null,
        'planMoney': null,
        'money': null,
        'usedMoney': null,
        'progress': null,
        'quantity': null,
        'boughtQuantity': null,
        'arrivedQuantity': null,
        'notArriveQuantity': null,
        'totalWeight': null,
        'totalVolume': null,
        'refuseReason': null,
        'applyTime': null,
        'auditTime': null,
        'status': null,
        'remark': null,
        'delFlag': null,
        'createdUserId': null,
        'updateUserId': null,
        'updateUserName': null,
        'updateTime': null
      }
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_APPLY_TYPE)
    this.reportTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPORT_RANK)
    this.periodTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PLAN_PERIOD_TYPE)
    this.moneyTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
    configData().then(res => {
      this.configMap = res.data['config:open:value']
      const suffix = this.DictCache.TYPE._AUDIT_MONEY
      console.log('this.user', this.user)
      this.user.roleCodes.forEach((item) => {
        const value = this.configMap[item + suffix]
        if (value) {
          this.auditMoneyInfo = JSON.parse(value)
        }
      })
      this.rmbRate = parseFloat(this.configMap[this.DictCache.TYPE.MONEY_TYPE + this.DictCache.VALUE.MONEY_TYPE.RMB + '_' + this.DictCache.VALUE.MONEY_TYPE.MY])
    })
    // this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_PLAN_STATUS)
  },
  props: {

  },
  methods: {
    base (record) {
      this.disabled = true
      this.reportFlagExist = false
      this.regionGroupFlag = false
      this.reportFlag = false
      this.meetingFlag = false
      this.model = {}
      this.taskId = record.taskId
      const modal = this.$refs.applyList
      modal.base(record.targetId)
      fetchPurchaseApply({ id: record.targetId }).then(res => {
        this.model = res.data
        this.visible = true
        //
        console.log('this.auditMoneyInfo', this.auditMoneyInfo)
        if (this.auditMoneyInfo) {
          const currentNodeType = parseInt(record.auditNode / 10) * 10
          let totalMoney = record.planMoney
          const rmbFlag = record.projectMoneyType === this.DictCache.VALUE.MONEY_TYPE.RMB
          const groupFlag = this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_JT === currentNodeType
          if (rmbFlag && !groupFlag) {
            totalMoney = this.BaseTool.Number.div(totalMoney, this.rmbRate)
          } else if (!rmbFlag && groupFlag) {
            totalMoney = this.BaseTool.Number.mul(totalMoney, this.rmbRate)
          }
          const report = parseFloat(this.auditMoneyInfo.report)
          this.reportFlagExist = this.auditMoneyInfo.reportOnly === 1 && totalMoney > report
          this.reportFlag = this.auditMoneyInfo.reportOnly === 2 && totalMoney > report
        }
        if (this.model.auditNode >= this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_FGS) {
          this.meetingFlag = true
        }
        if (this.model.auditNode >= this.DictCache.VALUE.REPORT_RANK.REPORT_RANK_QYZB) {
          this.regionGroupFlag = true
        }
        this.getUsers(this.model.projectId)
      })
    },
    loadOk () {
      this.disabled = false
    },
    getUsers (deptId) {
      queryProjectRole({ deptId: deptId,
        roleCodes: [this.DictCache.VALUE.ROLE_CODE.XMB_CGY,
          this.DictCache.VALUE.ROLE_CODE.FGS_CGY, this.DictCache.VALUE.ROLE_CODE.QYZB_CGY, this.DictCache.VALUE.ROLE_CODE.JT_CGY] }).then(res => {
        this.users = res.data
        const map = {}
        this.users.forEach((item) => {
          map[item.userId] = item
        })
        this.userMap = map
      })
    },
    catchFile (fileList) {
      fileList.forEach((item) => {
        this.fileUrl = item.url
        this.fileName = item.name
      })
    },
    save () {
      const params = {
        id: this.model.id,
        refuseReason: this.refuseReason,
        auditFlag: this.auditFlag,
        taskId: this.taskId,
        reportFlag: this.reportFlag,
        purchaserId: this.purchaserId,
        purchaserName: this.purchaserId ? this.userMap[this.purchaserId].realName : null,
        fileUrl: this.fileUrl,
        fileName: this.fileName
      }
      this.BaseTool.ListForm.setOneListValues(this, params)
      console.log(params)
      audit(params)
        .then(() => {
          this.$message.info('操作成功')
          this.handleCancel()
        }).catch(() => {
          this.confirmLoading = false
        })
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.$emit('ok', {})
    }

  }
}
</script>
<style scoped>
  .image {
    width: calc(20% - 10px);
    cursor: pointer;
    margin: 5px;
    display: inline-block;
  }
</style>
