<!-- 刷新提示--企业信息变更 -->
<template>
  <div>
    <dialog-edit :visible.sync="dialogVisible" :form="form" :editModel="'V'" :api="api" :appendToBody="appendToBody"
                 :footerVisible="true" :saveFlag="false"
                 :title="$t('enterpriseInfoManage.supplierInfoManage.enterpriseInfoChange')" :footerBtnShow="footerBtnShow">
      <data-table :api="api" :operateBtnLayout="operateBtnLayout" :queryParas="queryParas" :sourceData="sourceData"
                  ref="dataTableLocal" :showSelection="showSelection" @selectRowChange="selectRowChange"
                  :toolMenuFlag="toolMenuFlag" :columns="changeColumns" :showPage="showPage" :menuFlag="menuFlag"
                  :headTitle="$t('enterpriseInfoManage.supplierInfoManage.modifyContentTitle')"
                  :loadingFlag="loadingFlag">
      </data-table>
      <template slot="footer">
        <auth-button size="medium" type="primary"
                     :label="$t('common.updateButton')" @click="onSaved"
                     :menuCode="menuId" perms="refreshSave" :openPerms="true"></auth-button>
<!--        <auth-button :xhr="$xhr" :store="$store" size="" type="primary"-->
<!--                     :label="$t('common.closeButton')" @click="close"></auth-button>-->
      </template>
    </dialog-edit>
  </div>
</template>

<script>
import BaseEdit from '@/pages/baseEditPage'
import storage from '@/utils/storage'
import * as format from '@/filters/index'
import { DialogEdit, DataTable, authButton } from 'newepoch-components'
import supplierInfoManageApi from '@/api/enterpriseInfoManage/companyApi.js'
export default {
  extends: new BaseEdit(),
  name: 'refreshHintEdit',
  components: {
    DialogEdit,
    DataTable,
    authButton
  },
  data () {
    return {
      loadingFlag: false,
      footerBtnShow: true,
      showPage: false,
      appendToBody: true,
      toolMenuFlag: true,
      menuFlag: false,
      showSelection: true,
      operateBtnLayout: '',
      autoSave: false,
      queryParas: {},
      sourceData: [],
      changeColumns: [
        // 修改内容
        {
          prop: 'modifyContent',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.modifyContent,
          width: ''
        },
        // 修改前值
        {
          prop: 'modifyAfter',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.modifyBefore,
          width: '',
          format: (row) => {
            if (row.type === 'datetime') {
              return format.formatDateString(parseInt(row.modifyAfter))
            } else if (row.type === 'dict') {
              return format.formatDict(row.modifyAfter, row.code)
            } else if (row.type === 'money') {
              return format.thousandFilter(row.modifyAfter)
            } else {
              return row.modifyAfter
            }
          }
        },
        // 修改后值
        {
          prop: 'modifyBefore',
          align: 'left',
          label: this.$i18n.messages[this.$i18n.locale].enterpriseInfoManage.supplierInfoManage.modifyAfter,
          width: '',
          format: (row) => {
            if (row.type === 'datetime') {
              return format.formatDateString(parseInt(row.modifyBefore))
            } else if (row.type === 'dict') {
              return format.formatDict(row.modifyBefore, row.code)
            } else if (row.type === 'money') {
              return format.thousandFilter(row.modifyBefore)
            } else {
              return row.modifyBefore
            }
          }
        }
      ]
    }
  },
  computed: {
    api () {
      return supplierInfoManageApi.businessInformation
    }
  },
  methods: {
    // 关闭
    close () {
      this.dialogVisible = false
    },
    // 复选框勾选数据
    selectRowChange (val) {
      this.selectedRows = val
    },
    // 加载Table数据
    loadData (obj) {
      let that = this
      that.loadingFlag = true
      let pa = storage.getLocal('params')
      let enterpriseId = pa.enterpriseId.toString()
      let p = { enterpriseId: enterpriseId }
      that.api.getList(p).then(res => {
        if (res && res.data) {
          res.data.forEach(it => {
            if (it.type === 'datetime') {
              console.log(format.formatDateString(new Date(it.modifyAfter).getTime()))
              console.log(format.formatDateString(new Date(it.modifyBefore).getTime()))
            }
          })
          that.loadingFlag = false
          that.sourceData = res.data
        } else {
          that.loadingFlag = false
        }
      }).catch(() => {
        that.loadingFlag = false
      })
    },
    onSaved () {
      if (this.selectedRows && this.selectedRows.length > 0) {
        let data = {}
        data.enterpriseId = storage.getLocal('params').enterpriseId
        data.creditCode = storage.getLocal('params').creditCode
        this.selectedRows.forEach(it => {
          // if (it.type === 'datetime') {
          //   data[it.modifyField] = new Date(it.modifyBefore).getTime()
          // } else {
          //   data[it.modifyField] = it.modifyBefore
          // }
          data[it.modifyField] = it.modifyBefore
          // this.$set(data, it.modifyField, it.modifyBefore)
          // data[it.modifyField] = it.modifyBefore
        })
        supplierInfoManageApi.updateInfo.update(data).then(res => {
          if (res && res.data) {
            this.$emit('compareFun', this.selectedRows)
            this.dialogVisible = false
            this.warning(this.$i18n.messages[this.$i18n.locale].common.updateSuccessInfo)
          } else {
            this.error(this.$i18n.messages[this.$i18n.locale].common.updateError)
          }
        })
      } else {
        this.warning(this.$i18n.messages[this.$i18n.locale].common.selectOperateTip)
      }
    }
  },
  watch: {
    visible (val) {
      let that = this
      if (val) {
        that.sourceData = []
        // form内容包含模块来源
        that.$nextTick(() => {
          this.loadData(that.form)
        })
      } else {
        // this.$emit('compareFun', that.sourceData)
      }
    }
  }
}
</script>

<style scoped lang="scss">
  @import '@/styles/enterpriseInfoManage/supplierInfoManage/businessInfo.scss';
</style>
