<!-- 提单通知管理 -->
<template>
  <div>
    <up-down-layout :header="$t('ladingNoticeManage.head')" :handleFlag.sync="handleFlag" :footerFlag.sync="footerFlag">
      <search-form
        @handleFilter="handleFilter"
        @clearParam="clearParam"
        :formValue.sync="queryParas"
        @selectChanged="selectChanged"
        :componentList="componentList"
        slot="query">
      </search-form>
      <template slot="table">
        <data-table
          :menuFlag="true"
          :toolMenuFlag="true"
          :btnCreate="false"
          :queryParas="queryParas"
          :btnDels="false"
          operateBtnLayout="E"
          v-bind="elHeightObj"
          :columns="columns"
          :sourceData="sourceData"
          :api="api"
          :showSelection="true"
          :selectedRows.sync="selectedRows"
          @getTableData="getTableData"
          @onEditRow="showEdit"
          :menuCode="menuId"
          editPerms="edit"
          :openPerms="true"
          :loadCount="loadCount">
          <template slot="headButton">
            <div class="right-div">
              <auth-button class="iconfont iconquxiao" size="medium" :label="$t('ladingNoticeManage.cancelCuttingOrder')"
                           @click="cuttingCancelOrder" type="primary" :menuCode="menuId" perms="cancelOrder" :openPerms="true"></auth-button>
              <auth-button class="iconfont iconjiedan" size="medium" :label="$t('ladingNoticeManage.cuttingOrder')"
                           @click="cuttingCancelOrder('J')" type="primary" :menuCode="menuId" perms="order" :openPerms="true"></auth-button>
              <auth-button class="iconfont iconxinzeng" size="medium" :label="$t('common.create')"
                           @click="showCreate" type="primary" :menuCode="menuId" perms="add" :openPerms="true"></auth-button>
            </div>
            <div class="left-div">
              <el-row>
                <el-col>
                  <span class="head-font-size">{{$t('ladingNoticeManage.totalEstimatedLadingAmount')}}</span>
                  <el-input class="lading-head-input" disabled v-model="expressForm.planLadingAmount"></el-input>
                  <span class="head-font-size head-font-size-margin">{{$t('ladingNoticeManage.actualLadingAmount')}}</span>
                  <el-input class="lading-head-input" disabled v-model="expressForm.accountsReceivable"></el-input>
                </el-col>
              </el-row>
            </div>
          </template>
        </data-table>
      </template>
    </up-down-layout>
    <lading-notice-manage-edit :menuCode="menuId" :visible.sync="showDetail" :form="item" :editModel="editModel" @close="handleFilter"></lading-notice-manage-edit>
    <stop-time-result :menuCode="menuId" :visible.sync="stopTimeShowDetail" :form="stopTimeItem" :editModel="stopTimeEditModel"></stop-time-result>
  </div>
</template>

<script>
import BasePage from '@/pages/basePage'
import { upDownLayout, searchForm, DataTable, authButton, SelectCoreEnterpriseCascade, SelectProductName, SelectFinancialInstitutionsCascade, SelectPeriodsCascade,
  SelectAssetsServiceCascade, SelectPrimitiveRightsHolderCascade } from 'newepoch-components'
import ladingNoticeManageApi from '@/api/shouji/ladingNoticeManageApi'
import stopTimeResult from '@/pages/shouji/ladingNoticeManage/stopTimeResult'
import ladingNoticeManageEdit from '@/pages/shouji/ladingNoticeManage/ladingNoticeManageEdit'
import SelectSpecialPlanPeriods from '@/components/element/shourang/SelectSpecialPlanPeriods'
import componentsApi from '@/api/componentsApi'
import * as format from '@/filters/index'
import mixin from '@/pages/mixin'
export default {
  extends: new BasePage(),
  mixins: [mixin],
  name: 'ladingNoticeManageList',
  components: {
    upDownLayout,
    searchForm,
    DataTable,
    ladingNoticeManageEdit,
    SelectSpecialPlanPeriods,
    authButton,
    stopTimeResult
  },
  computed: {
    // 查询传入API接口
    api () {
      return ladingNoticeManageApi.ladingNoticeManageListApi
    }
  },
  // activated () {
  //   console.log('activated')
  //   this.queryParas.ifFirst = '是'
  //   this.handleFilter()
  // },
  mounted () {
    // console.log('mounted')
    if (this.queryParas) {
      this.queryParas = Object.assign({}, { pageIndex: 1, pageSize: 10 })
    }
    this.queryParas.ifFirst = '是'
    this.handleFilter()
  },
  // activated () {
  //   if (this.queryParas) {
  //     this.queryParas = Object.assign({}, { pageIndex: 1, pageSize: 10 })
  //   }
  //   this.queryParas.ifFirst = '是'
  //   this.handleFilter()
  // },
  data () {
    const labelWidth = '165px' // 整体搜索文字大小
    return {
      stopTimeShowDetail: false,
      stopTimeEditModel: '',
      stopTimeItem: {},
      expressForm: {},
      selectedRows: [],
      footerFlag: true,
      sourceData: [],
      componentList: [
        // 产品名称
        {
          name: 'projectName',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.productName,
          value: 'projectId',
          clearable: true,
          componentName: SelectProductName,
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectproductName,
          labelWidth: labelWidth
        },
        // 期数
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.specialPlanPeriods,
          value: 'specialPlanPeriodsId',
          name: 'specialPlanPeriodsId',
          clearable: true,
          queryName: 'projectId',
          queryValue: 'projectId',
          componentName: SelectPeriodsCascade,
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.inputspecialPlanPeriods,
          labelWidth: labelWidth
        },
        // 核心企业
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.coreEnterprise,
          value: 'coreEnterpriseId',
          clearable: true,
          componentName: SelectCoreEnterpriseCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectcoreEnterprise,
          labelWidth: labelWidth
        },
        // 计划管理人/再保理银行
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.planningManagerName,
          value: 'planningManagerId',
          clearable: true,
          componentName: SelectFinancialInstitutionsCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectplanningManagerName,
          labelWidth: labelWidth
        },
        // 提单类型
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.ladingType,
          value: 'ladingType',
          clearable: true,
          componentName: 'SelectDictionary',
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectladingType,
          labelWidth: labelWidth,
          dictTypeCode: 'ZD0100'
        },
        // 原始权益人
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.rightsHolder,
          value: 'initialOwnersId',
          clearable: true,
          componentName: SelectPrimitiveRightsHolderCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectrightsHolder,
          labelWidth: labelWidth
        },
        // 资产服务机构
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.assetServiceOrgan,
          value: 'assetServiceId',
          clearable: true,
          componentName: SelectAssetsServiceCascade,
          queryName: 'projectId',
          queryValue: 'projectId',
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectassetServiceOrgan,
          labelWidth: labelWidth
        },
        // 操作时间
        {
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.operateTime,
          value: 'handleTime',
          clearable: true,
          componentName: 'el-date-picker',
          placeholder: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.selectoperateTime,
          labelWidth: labelWidth,
          format: 'timestamp'
        }
      ],
      columns: [
        // 产品名称
        {
          prop: 'projectName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.productName,
          width: '160px'
        },
        // 期数
        {
          prop: 'specialPlanPeriods',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.specialPlanPeriods,
          width: '60px',
          format: (row) => {
            if (!row.specialPlanPeriods || row.specialPlanPeriods.toString() === '0') {
              return ''
            } else {
              return row.specialPlanPeriods
            }
          }
        },
        // 核心企业
        {
          prop: 'coreEnterpriseName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.coreEnterprise,
          width: '180px'
        },
        // 计划管理人/再保理银行
        {
          prop: 'planningManagerName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.planningManagerName,
          width: '180px'
        },
        // 提单类型
        {
          prop: 'ladingType',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.ladingType,
          width: '100px',
          format: (row) => {
            return format.formatDict(row.ladingType, 'ZD0100')
          }
        },
        // 资产服务机构
        {
          prop: 'assetServiceName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.assetServiceOrgan,
          width: '200px'
        },
        // 原始权益人
        {
          prop: 'initialOwnersName',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.rightsHolder,
          width: '200px'
        },
        // 预计截单时间
        {
          prop: 'predictStopTime',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.estimatedCutOffTime,
          width: '150px',
          format: (row) => {
            if (row.predictStopTime) {
              return format.formatDateNull(Number(row.predictStopTime), 'yyyy-MM-dd hh:mm')
            }
            return ''
          }
        },
        // 预计提单规模
        {
          prop: 'predictLadingAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.dataTitlePlanLadingEndScale,
          width: '160px',
          format: (row) => {
            if (row.predictLadingAmount === 0) {
              return ''
            }
            return format.thousandFilter(row.predictLadingAmount, 4)
          }
        },
        // 预计提单金额
        {
          prop: 'planLadingEndAmount',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.estimatedLadingAmount,
          width: '150px',
          format: (row) => {
            if (row.planLadingEndAmount === 0) {
              return ''
            }
            return format.thousandFilter(row.planLadingEndAmount)
          }
        },
        // 实际提单金额
        {
          prop: 'accountsReceivable',
          align: 'right',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.actualLadingAmount,
          width: '150px',
          format: (row) => {
            if (row.accountsReceivable === 0) {
              return ''
            }
            return format.thousandFilter(row.accountsReceivable)
          }
        },
        // 状态
        {
          prop: 'noticeStatus',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.stat,
          width: '100px',
          format: (row) => {
            return format.formatDict(row.noticeStatus, 'ZD0108')
          }
        },
        // 操作时间
        {
          prop: 'modifyTime',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.operateTime,
          width: '150px',
          format: (row) => {
            if (row.modifyTime) {
              return format.formatDateNull(Number(row.modifyTime), 'yyyy-MM-dd hh:mm')
            }
            return ''
          }
        }
      ]
    }
  },
  methods: {
    // 根据ID获取产品类别
    getTypeById () {
      componentsApi.productenterprise.getclassify({ projectId: this.queryParas.projectId }).then(res => {
        let disabled = res.data ? res.data.productType : '2'
        this.$set(this.componentList[1], 'disabled', disabled !== '1')
      })
    },
    selectChanged (obj) {
      if (obj.specialPlanPeriodsIdInfo) {
        this.queryParas.specialPlanPeriods = obj.specialPlanPeriodsIdInfo.specialPlanPeriods
      }
      if (obj.projectNameInfo) {
        this.getTypeById()
      }
      if (Object.keys(obj).some(it => it === 'projectNameInfo')) {
        this.clearInfoList()
      }
    },
    // 获取完表格数据之后获取预计提单金额、实际提单金额
    getTableData (data) {
      this.getaccountsReceivable(this.queryParas)
    },
    // 获取预计提单金额、实际提单金额
    getaccountsReceivable (data) {
      ladingNoticeManageApi.ladingNoticeManageListApi.getLadingNotice(data).then(res => {
        this.queryParas.ifFirst = '否'
        if (res.data) {
          this.$set(this.expressForm, 'accountsReceivable', format.thousandFilter(res.data.accountsReceivable))
          this.$set(this.expressForm, 'planLadingAmount', format.thousandFilter(res.data.planLadingAmount))
        }
      })
    },
    // 截单\取消截单
    cuttingCancelOrder (model) {
      if (this.selectedRows.length > 0) {
        let putdata = []
        this.selectedRows.forEach(it => {
          putdata.push({ keyId: it.keyId, stopTime: new Date() })
        })
        if (model === 'J') {
          // 截单
          putdata.forEach(it => {
            it.stopStatus = '1'
          })
        } else {
          // 取消截单
          putdata.forEach(it => {
            // it.noticeStatus = 1
            it.stopStatus = '0'
            it.stopTime = -2209017600000
          })
        }
        ladingNoticeManageApi.ladingNoticeManageListApi.batchStatus(putdata).then(res => {
          if (res && res.success) {
            this.handleFilter()
            if (model === 'J') {
              // 截单成功文字
              this.stopTimeItem = { single: 'j', data: res.data }
              this.stopTimeShowDetail = true
              return this.success(this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.opaertSuccess)
            }
            this.stopTimeItem = { single: '', data: res.data }
            this.stopTimeShowDetail = true
            // 取消截单成功文字
            return this.success(this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.opaertSuccess)
          }
        })
      } else {
        if (model === 'J') {
          // 截单没选择文字
          return this.warning(this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.infoerrorjiean)
        }
        // 取消截单没选择文字
        return this.warning(this.$i18n.messages[this.$i18n.locale].ladingNoticeManage.infoerrorcanceljiean)
      }
    },
    clearInfoList () {
      this.$set(this.componentList[1], 'disabled', false)
      delete this.queryParas.coreEnterpriseId
      delete this.queryParas.planningManagerId
      delete this.queryParas.assetServiceId
      delete this.queryParas.initialOwnersId
      delete this.queryParas.specialPlanPeriodsId
      delete this.queryParas.specialPlanPeriods
      if (!this.queryParas.projectId) {
        delete this.queryParas.projectId
      }
    }
  }
}
</script>

<style scoped lang="scss">
  @import '~@/styles/sj/ladingNoticeManage/ladingNoticeManageList.scss';
</style>
