<template>
  <div>
    <!-- 异常日志 -->
    <p-card title="接口日志管理">
      <div slot="extra">
        <p-button type="primary" @click="again">重新执行</p-button>
      </div>
      <span slot="extra">
        <!-- <p-button type="primary" style="margin-right:10px" @click="showmodel()">添加</p-button> -->
      </span>
      <p-grid-form :span="8" :items="config.items" @submit="hanlderSubmit" />
      <p-table
        ref="table"
        row-key="logId"
        bordered
        :columns="config.columns"
        :remoteUrl="config.remoteUrl"
        :remoteParams="remoteParams"
        @selectChange="selectChange"
        :remoteBefore="remoteBefore"
        :rowSelectType="2"
        :data-source="data"
      >
      </p-table>
    </p-card>
  </div>
</template>

<script>
import config from './config.js'

import { align } from './api'

export default {
  data() {
    return {
      config,
      data: [],
      type: true,
      remoteParams: {
        etlType: '',
        transId: '',
        transFlag: '',
        warehouseId: '',
        startDate: '',
        endDate: ''
      },
      selectedRows: [],
      visible: false,
      confirmLoading: false
    }
  },
  mounted() {
    // this.getinfo()
  },
  methods: {
    remoteBefore(vnode, remoteUrl) {
      remoteUrl.params.startDate && (remoteUrl.params.startDate = remoteUrl.params.startDate + ' 00:00:00')
      remoteUrl.params.endDate && (remoteUrl.params.endDate = remoteUrl.params.endDate + ' 23:59:59')
      return remoteUrl
    },
    //重新执行
    async again() {
      let datalist = this.selectedRows.filter(item => {
          return item.transFlag == 'N'
        }).map(item => item.logId);
      //   datalist.forEach(async item => {
      if (!datalist || datalist.length == 0) {
        this.$message.warning('请勾选传输失败项')
        return;
      }
      let res = await align(datalist)
      if (res.success) {
        // this.$message.success('执行成功!')
        this.$message.success(res.message)
      } else {
        this.$exception(res)
      }
      this.remoteParams = { ...this.remoteParams }
      //   })
      //
    },
    selectChange(selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
    },
    hanlderSubmit(e, form) {
      e.preventDefault()
      form.validateFields((error, values) => {
        // this.queryName = values.name
        // this.queryType = values.type
        // this.getlistCompanyByAuthId()
        if (!error) {
          this.remoteParams = { ...this.remoteParams, ...values }
        }
      })
    },
    handleOk(e) {
      this.$refs.model.form.validateFields(async (err, value) => {
        if (!err) {
          this.confirmLoading = true
          if (this.type) {
            let res = await update(value)
            if (res.success) {
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          } else {
            let res = await add(value)
            if (res.success) {
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          }

          this.confirmLoading = false
          //
        }
      })
    },
    handleCancel(e) {
      this.visible = false
    },
    //点击躺床
    showmodel(data) {
      this.visible = true

      if (data) {
        this.$nextTick(() => {
          this.$refs.model.form.setFieldsValue(data)
          this.$refs.model.form.setFieldsValue(data)
        })
        this.type = true
      } else {
        this.$nextTick(() => {
          this.$refs.model.form.resetFields()
        })
        this.type = false
      }
    },
    del(text) {
      let that = this
      this.$confirm({
        title: '确定是否要删除?',
        content: '',
        async onOk() {
          let res = await deletes({ id: text.id })
          if (res.success) {
            that.$message.success('删除成功')
            that.remoteParams = { ...that.remoteParams }
          } else {
            that.$exception(res)
          }
        },
        onCancel() {}
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
