<template>
  <div>
    <!-- 区域资料 -->
    <a-spin :spinning="spinning" size="large" tip="数据下载中...">
      <p-card title="监管码自动导出">
        <span slot="extra">
          <a-space>
            <p-button type="primary" style="margin-right: 10px" @click="creatTask()">创建导出任务</p-button>
            <p-button type="primary" style="margin-right: 10px" @click="allList()">开始导出</p-button>
          </a-space>
        </span>
        <p-grid-form
          ref="form"
          :label-col="{ span: 8 }"
          :span="8"
          :wrapperCol="{ span: 8, offset: 2 }"
          :items="config.DocumentInfo.items"
          @submit="hanlderSubmit"
        />

        <p-tabs default-active-key="1" @change="callback">
          <p-tab-pane key="1" tab="单据信息">
            <p-table
              ref="table"
              row-key="id"
              bordered
              auto-width
              :autoLoad="false"
              :filterDropdownColum="false"
              :colorTransformRow="false"
              :scroll="{ x: true }"
              :columns="config.DocumentInfo.columns"
              :remoteUrl="config.DocumentInfo.remoteUrl"
              :remoteParams="remoteParams"
              :rowSelectType="false"
              :data-source.sync="DocumentInfodata"
            >
            </p-table>
          </p-tab-pane>
          <p-tab-pane key="2" tab="待导出任务">
            <p-table
              ref="table"
              row-key="id"
              bordered
              :autoLoad="false"
              auto-width
              :filterDropdownColum="false"
              :colorTransformRow="false"
              :scroll="{ x: true }"
              :columns="config.TasksExported.columns"
              :remoteUrl="config.TasksExported.remoteUrl"
              :remoteParams="TasksExportedremoteParams"
              :rowSelectType="false"
              :data-source.sync="TasksExporteddata"
            >
            </p-table>
          </p-tab-pane>
        </p-tabs>
      </p-card>
    </a-spin>
  </div>
</template>

<script>
import config from './config.js'
import moment from 'moment'
import pick from 'lodash.pick'
import { add, deletes, update, createSupervisionTask, downloadALL } from './api'
import { diffOption } from '@/views/kzyWMS/utils/index.js'
import { getCargoOwner } from '../../utils/index'
export default {
  data() {
    return {
      spinning: false,
      key: 1,
      config: config(this),
      TasksExporteddata: [],
      DocumentInfodata: [],
      type: true,
      remoteParams: {
        num: 'query_supervision_code4xml',
        params: {
          status: '10'
        }
      },
      TasksExportedremoteParams: {
        num: 'query_supervision_code4xml',
        params: {
          status: '15'
        }
      },
      visible: false,
      confirmLoading: false
    }
  },
  mounted() {
    this.getAuthBycargoOwner()
    this.$nextTick(() => {
      let startTime = moment()
        .subtract(1, 'months')
        .format('YYYY-MM-DD')
      let endTime = moment().format('YYYY-MM-DD')
      console.log(startTime, endTime)
      let warehouse = this.$store.state.user.warehouse
      this.$refs.form.form.setFieldsValue({ ...warehouse, startTime, endTime })
    })
  },
  created() {
    diffOption(this.config.DocumentInfo.columns).then(res => {
      this.config.DocumentInfo.columns = res
    })
  },
  methods: {
    allList() {
      let that = this
      // 全部导出
      this.$refs.form.form.validateFields(async (errors, values) => {
        if (!errors) {
          const datas = {
            warehouseId: values.warehouseId,
            cargoOwnerId: values.cargoOwnerId,
            supervisionType: 1,
            status: '15'
          }
          const datas1 = {
            warehouseId: values.warehouseId,
            cargoOwnerId: values.cargoOwnerId,
            supervisionType: 0,
            status: '15'
          }
          this.spinning = true
          //   downloadALL(datas, res => {
          //     this.spinning = false
          //   })
          let res = await downloadALL(datas, function(type, fileName) {})
          let res1 = await downloadALL(datas1, function(type, fileName) {})
          this.spinning = false
        }
      })
    },

    async getAuthBycargoOwner() {
      await getCargoOwner().then(res => {
        if (res.success) {
          this.$nextTick(() => {
            this.$refs.form.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerName: res.data.list[0].companyName,
              cargoOwnerCode: res.data.list[0].companyCode
            })
          })
        }
        if (!res.success) {
          this.$exception(res)
          return
        }
      })
    },

    async creatTask() {
      //   console.log(this.DocumentInfodata)
      let p = this.DocumentInfodata.map(item => item.id).join(',')
      //   console.log(data)
      let data = { ids: p }

      try {
        let res = await createSupervisionTask(data)
        if (res.success) {
          this.$message.success('创建成功')
          this.remoteParams = { ...this.remoteParams }
        } else {
          this.$exception(res)
        }
      } catch (error) {
        this.$exception(error)
      }
    },
    callback(key) {
      this.key = key
    },

    hanlderSubmit(e, form) {
      let that = this
      e.preventDefault()
      form.validateFields((error, values) => {
        if (!error) {
          console.log(error, values, 'valuesvaluesvaluesvaluesvalues')
          if (that.key == 1) {
            this.remoteParams = { ...this.remoteParams, params: { ...values, status: '10' } }
          } else {
            this.TasksExportedremoteParams = { ...this.TasksExportedremoteParams, params: { ...values, status: '15' } }
          }
        }
      })
    },
    handleOk(e) {
      console.log(this.$refs.model)
      this.$refs.model.form.validateFields(async (err, value) => {
        if (!err) {
          console.log(value)
          this.confirmLoading = true
          if (this.type) {
            let res = await update(value)
            console.log(res)
            if (res.success) {
              this.visible = false
              this.$message.success(res.message)
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
              this.visible = false
            }
          } else {
            let res = await add(value)
            console.log(res)

            if (res.success) {
              this.$message.success(res.message)
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
              this.visible = false
            }
          }

          this.confirmLoading = false
          //
        }
      })
    },

    //点击弹出
    showmodel(data) {},
    del(text) {
      let that = this
      console.log(text)
      this.$confirm({
        title: '确定是否要删除?',
        content: '',
        async onOk() {
          let res = await deletes({ areaNo: text.areaNo })
          if (res.success) {
            that.$message.success('删除成功')
            that.remoteParams = { ...that.remoteParams }
          } else {
            that.$exception(res)
          }
        },
        onCancel() {}
      })
    }
  }
}
</script>

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