<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :footerBtnShow="false" :title="editModel === 'E' ? $t('ladingPlanManageList.singlePush') : $t('ladingPlanManageList.batchPush')"
                 :autoSave="false" onSaved="onSaved"
                 :api="api" :form="form" :editModel="editModel">
      <data-table :operateBtnLayout="operateBtnLayout" :columns="columns" :editModel="''" :saveFlag="true"
                  :menuFlag="false" :sourceData.sync="sourceData" :selectedRows.sync="selectedRows" :showPage="false" :showSelection="true">
      </data-table>
      <template slot="footer">
        <auth-button :menuCode="menuCode" size="medium" type="primary" :label="$t('common.save')" @click="handleClick" :loading="saveLoading" :openPerms="true" perms="singlePushBtn"></auth-button>
        <auth-button size="medium" type="" :label="$t('common.close')" @click="dialogVisible = false"></auth-button>
      </template>
    </dialog-edit>
  </div>
</template>

<script>
import { DialogEdit, DataTable, authButton } from 'newepoch-components'
import BaseEditPage from '@/pages/baseEditPage'
import ladingManageApi from '@/api/ladingManageApi'
export default {
  extends: new BaseEditPage(''),
  components: {
    DialogEdit,
    DataTable,
    authButton
  },
  props: {
    menuCode: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      saveLoading: false,
      selectedRows: [],
      operateBtnLayout: '',
      sourceData: [
        {
          pushPlatform: '推送保理统筹工作台',
          pushType: '2'
        },
        {
          pushPlatform: '推送导入平台',
          pushType: '1'
        }
      ],
      columns: [
        // 推送平台
        {
          prop: 'pushPlatform',
          align: 'center',
          label: this.$i18n.messages[this.$i18n.locale].ladingPlanManageList.pushPlatform,
          width: '100px'
        }
      ]
    }
  },
  mounted () {
  },
  computed: {
    api: function () {
      return {}
    }
  },
  methods: {
    handleClick () {
      if (this.selectedRows.length > 0) {
        let pushType = []
        let data = []
        this.selectedRows.forEach(it => pushType.push(it.pushType))
        this.form.data.forEach(it => {
          pushType.forEach(it1 => {
            it.pushType = it1
            let it2 = Object.assign({}, it)
            data.push(it2)
          })
        })
        this.pushRemote(data)
      } else {
        this.warning(this.$i18n.messages[this.$i18n.locale].ladingManageList.selectData)
      }
    },
    pushRemote (putData) {
      let that = this
      that.saveLoading = true
      ladingManageApi.ladingPlanManageList.batchBack(putData).then(res => {
        if (res.success && res.data) {
          that.success(this.$i18n.messages[this.$i18n.locale].ladingManageList.pushRemoteSuccess)
          this.dialogVisible = false
          that.$emit('closeAndShow', res.data)
        } else {
          this.saveLoading = false
          that.error(this.$i18n.messages[this.$i18n.locale].ladingManageList.pushRemoteError)
        }
      }).catch(res => {
        this.saveLoading = false
      })
    }
  },
  watch: {
    dialogVisible (newvalue, oldvalue) {
      if (newvalue) {
        this.saveLoading = false
      }
    }
  }
}
</script>
