<template>
  <a-card :bordered="false">
    <div v-if="visible">
      <a-row :gutter="48" slot="extra">
        <a-col :md="48" :sm="48">
          <span class="table-page-search-submitButtons" style="float: right">
            <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
          </span>
        </a-col>
      </a-row>
      <title-divider title="总需求计划" width="100px"></title-divider>
      <detail-list title="" :col="3">
        <detail-list-item term="名称">{{ model.name }}</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(typeMap,model.type) }}</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.planListControlMoney)+ BaseTool.Number.divForDollar(model.planListControlMoney, model.dollarExchangeRate, 2) }}</detail-list-item>
        <detail-list-item term="已用资金">{{ BaseTool.Amount.formatter(model.usedMoney)+ BaseTool.Number.divForDollar(model.usedMoney, model.dollarExchangeRate, 2) }}</detail-list-item>
        <detail-list-item term="数量">{{ BaseTool.Amount.formatter(model.quantity) }}</detail-list-item>
      </detail-list>
      <title-divider title="增补记录" width="90px"></title-divider>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新增</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
        :alert="options.alert"
        :showSizeChanger="showSizeChanger"
        :showPagination="showSizeChanger"
        :rowSelection="options.rowSelection"
      >
        <span slot="action" slot-scope="record">
          <template>
            <a @click="handleView(record)">查看</a>
          </template>
        </span>
      </s-table>
    </div>
    <base-add-form ref="baseDemandPlanAddModal" @ok="handleOk"/>
    <detail-add ref="detailDemandPlanAddModal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import BaseAddForm from './modules/BaseAddForm'
import DetailAdd from './modules/DetailAdd'
import { fetchPurchaseDemandPlan } from '@/api/purchase/purchase-demand-plan'
import { getPurchaseDemandPlanAddPage, submit, fetchPurchaseDemandPlanAdd } from '@/api/purchase/purchase-demand-plan-add'
import DetailList from '@/components/tools/DetailList'
const DetailListItem = DetailList.Item
export default {
  name: 'PurchaseDemandPlan',
  components: {
    STable,
    Ellipsis,
    DetailAdd,
    DetailList,
    DetailListItem,
    BaseAddForm
  },
  data () {
    return {
      queryParam: {
        pageSize: 9999
      },
      visible: false,
      demandPlanId: '-1',
      showSizeChanger: false,
      dollarExchangeRate: this.$store.getters.rmbRate,
      isCreated: false,
      model: {
        'name': null,
        'companyId': null,
        'companyName': null,
        'projectId': null,
        'projectName': null,
        'type': null,
        'addMoney': null,
        'planMoney': null,
        'planListControlMoney': null,
        'dollarExchangeRate': null,
        'usedMoney': null,
        'progress': null,
        'quantity': null,
        'boughtQuantity': null,
        'arrivedQuantity': null,
        'notArriveQuantity': null,
        'status': null,
        'remark': null,
        'delFlag': null,
        'createdUserId': null,
        'updateUserId': null,
        'updateUserName': null,
        'updateTime': null
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return `${index + 1}`
          }
        },
        {
          title: '名称',
          dataIndex: 'name',
          checked: true,
          width: 120
        },
        {
          title: '公司名称',
          dataIndex: 'companyName',
          checked: true,
          width: 120
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          checked: true,
          width: 120
        },
        {
          title: '项目记账币种',
          dataIndex: 'projectMoneyType',
          checked: true,
          width: 120,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.moneyTypeMap, text)
          }
        },
        {
          title: '类别',
          dataIndex: 'type',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.typeMap, text)
          }
        },
        {
          title: '资金预算',
          dataIndex: 'planMoney',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            let dollarExchangeRate = 1
            if (record.projectMoneyType === this.DictCache.VALUE.MONEY_TYPE.RMB) {
              dollarExchangeRate = this.dollarExchangeRate
            }
            return this.BaseTool.Amount.formatter(text) + this.BaseTool.Number.divForDollar(text, dollarExchangeRate, 2)
          }
        },
        {
          title: '增补资金',
          dataIndex: 'addMoney',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            let dollarExchangeRate = 1
            if (record.projectMoneyType === this.DictCache.VALUE.MONEY_TYPE.RMB) {
              dollarExchangeRate = this.dollarExchangeRate
            }
            return this.BaseTool.Amount.formatter(text) + this.BaseTool.Number.divForDollar(text, dollarExchangeRate, 2)
          }
        },
        {
          title: '明细控制总价',
          dataIndex: 'planListControlMoney',
          checked: true,
          width: 130,
          customRender: (text, record, index) => {
            let dollarExchangeRate = 1
            if (record.projectMoneyType === this.DictCache.VALUE.MONEY_TYPE.RMB) {
              dollarExchangeRate = this.dollarExchangeRate
            }
            return this.BaseTool.Amount.formatter(text) + this.BaseTool.Number.divForDollar(text, dollarExchangeRate, 2)
          }
        },
        {
          title: '明细总价',
          dataIndex: 'planListMoney',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            let dollarExchangeRate = 1
            if (record.projectMoneyType === this.DictCache.VALUE.MONEY_TYPE.RMB) {
              dollarExchangeRate = this.dollarExchangeRate
            }
            return this.BaseTool.Amount.formatter(text) + this.BaseTool.Number.divForDollar(text, dollarExchangeRate, 2)
          }
        },
        {
          title: '已用资金',
          dataIndex: 'usedMoney',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            let dollarExchangeRate = 1
            if (record.projectMoneyType === this.DictCache.VALUE.MONEY_TYPE.RMB) {
              dollarExchangeRate = this.dollarExchangeRate
            }
            return this.BaseTool.Amount.formatter(text) + this.BaseTool.Number.divForDollar(text, dollarExchangeRate, 2)
          }
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '增补数量',
          dataIndex: 'addQuantity',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '进度',
          dataIndex: 'progress',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已购数量',
          dataIndex: 'boughtQuantity',
          checked: true,
          width: 120,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已到数量',
          dataIndex: 'arrivedQuantity',
          checked: true,
          width: 120,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '未到数量',
          dataIndex: 'notArriveQuantity',
          checked: true,
          width: 120,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 90
        },
        {
          title: '创建人名称',
          dataIndex: 'createdUserName',
          width: 130
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime',
          width: 120
        },
        {
          title: '审批节点',
          dataIndex: 'auditNode',
          fixed: 'right',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.statusCustomRenderDict(this, text, record,
              this.DictCache.COLOR.PURCHASE_AUDIT_NODE, this.nodeMap)
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          fixed: 'right',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.statusCustomRenderDict(this, text, record,
              this.DictCache.COLOR.PURCHASE_DEMAND_PLAN_STATUS, this.statusMap)
          }
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          checked: true,
          fixed: 'right',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 下拉框map
      typeMap: {},
      statusMap: {},
      nodeMap: {},
      moneyTypeMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          demandPlanId: this.demandPlanId,
          dataScope: {
            sortBy: 'asc,desc',
            sortName: 'status,update_time'
          }
        }
        return getPurchaseDemandPlanAddPage(parameter)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_TYPE)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_DEMAND_PLAN_STATUS)
    this.nodeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_AUDIT_NODE)
    this.moneyTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
    this.tableOption()
  },
  methods: {
    base (record) {
      this.visible = true
      this.model = record
      this.demandPlanId = record.id
      if (this.isCreated) {
        this.handleOk()
      } else {
        this.tableOption()
        this.isCreated = true
      }
    },
    getUpdateFlag (status) {
      return this.DictCache.VALUE.PURCHASE_DEMAND_PLAN_STATUS.PURCHASE_DEMAND_PLAN_STATUS_DTJ === status || this.DictCache.VALUE.PURCHASE_DEMAND_PLAN_STATUS.PURCHASE_DEMAND_PLAN_STATUS_SPBTG === status
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleAdd () {
      fetchPurchaseDemandPlan({ id: this.demandPlanId }).then(res => {
        const modal = this.$refs.baseDemandPlanAddModal
        this.visible = false
        modal.base(res.data)
      })
    },
    handleView (record) {
      fetchPurchaseDemandPlanAdd({ id: record.id }).then(res => {
        const modal = this.$refs.detailDemandPlanAddModal
        this.visible = false
        modal.base(res.data)
      })
    },
    handleSubmit (id) {
      submit(id).then(res => {
        this.$message.info('提交成功')
        this.handleOk()
      })
    },
    handleOk () {
      this.visible = true
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },
    handleCancel (values) {
      this.visible = false
      this.$emit('ok', values)
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
