<template>
  <div>
    <!-- 搬运任务调度 -->
    <p-card title="搬运任务监控">
      <span slot="extra">
        <p-button type="primary" style="margin-right: 10px" @click="showmodeladd()">添加</p-button>
      </span>
      <p-grid-form :label-col="{ span: 8 }" :span="8" :items="config.items" @submit="hanlderSubmit" />
      <p-table
        ref="table"
        row-key="id"
        bordered
        :columns="config.columns"
        :remoteUrl="config.remoteUrl"
        :remoteParams="remoteParams"
        :doneBefore="doneBefore"
        :rowSelectType="false"
        :data-source="data"
        :scroll="{ x: 1500 }"
      >
        <template slot="operation" slot-scope="text, record">
          <a @click="showmodel(text)">调整优先级</a>
          <a-divider type="vertical" />
          <!-- {{ text ? '禁用' : '启用' }} -->
          <a @click="detail(record)">查看详细</a>
          <a-divider type="vertical" />
          <a v-if="record.taskStatus == 0" @click="carryinterfacetask(record)">取消任务</a>
          <span v-else>取消任务</span>
        </template>
      </p-table>
    </p-card>

    <p-modal
      title="新增"
      width="50%"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-grid-form :label-col="{ span: 8 }" ref="model" :span="8" :items="config.Modelitems" @submit="hanlderSubmit" />
    </p-modal>
    <p-modal
      title="详情"
      width="50%"
      :visible="visibleDetails"
      :confirm-loading="confirmLoading"
      @ok="visibleDetails = true"
      @cancel="handleCancel"
    >
      <a-descriptions bordered>
        <a-descriptions-item label="是否变更任务"> {{ detailInfo.isChangeTask ? '是' : '否' }} </a-descriptions-item>
        <a-descriptions-item label="是否取消任务"> {{ detailInfo.cleanTaskStatus ? '是' : '否' }} </a-descriptions-item>
        <a-descriptions-item label="优先级"> {{ detailInfo.priority }} </a-descriptions-item>
        <a-descriptions-item label="高度"> {{ detailInfo.height || 0 }} </a-descriptions-item>
        <a-descriptions-item label="重量"> {{ detailInfo.weight }} </a-descriptions-item>
        <a-descriptions-item label="业务属性"> {{ detailInfo.businessProperty || '暂无' }} </a-descriptions-item>
        <a-descriptions-item label="错误信息"> {{ detailInfo.errorMsg || '暂无' }} </a-descriptions-item>
      </a-descriptions>
    </p-modal>
    <p-modal
      title="调整优先级"
      width="50%"
      :visible="visiblepriority"
      :confirm-loading="confirmLoading"
      @ok="handleOkpriority"
      @cancel="handleCancel"
    >
      <p-grid-form :label-col="{ span: 8 }" ref="model" :span="8" :items="Modelitems" @submit="hanlderSubmit" />
    </p-modal>
  </div>
</template>

<script>
import config from './config.js'
import { diffOption } from '@/views/kzyWMS/utils/index.js'
import { add, stationdelete, stationupdate, carrytask, updatePriority } from './api'

export default {
  data() {
    return {
      config: config(this),
      data: [],
      Modelitems: [
        {
          span: 24,
          type: 'number',
          name: 'priority',
          required: true,
          label: '优先级',
          value: '',
        },
        {
          span: 24,
          type: 'input',
          name: 'id',
          required: true,
          hidden: true,
          label: 'id',
          value: '',
        },
      ],
      type: true,
      remoteParams: {
        containerNo: '',
        id: '',
        taskType: '',
      },
      detailInfo: {},
      visible: false,
      visibleDetails: false,
      visiblepriority: false,
      confirmLoading: false,
    }
  },
  mounted() {
    // this.getinfo()
    // id
  },
  created() {
    diffOption(this.config.columns).then((res) => {
      this.config.columns = res
    })
  },
  methods: {
    handleOkpriority(e) {
      this.$refs.model.form.validateFields(async (err, value) => {
        if (!err) {
          console.log(value)
          let res = await updatePriority(value)
          console.log(res)
          if (res.success) {
            this.visiblepriority = false
            this.remoteParams = { ...this.remoteParams }
          } else {
            this.$exception(res)
          }
        }
      })
    },
    //点击弹出
    showmodel(data) {
      console.log(data)
      this.visiblepriority = true
      this.$nextTick(() => {
        this.$refs.model.form.setFieldsValue(data)
      })
    },
    //查看详情
    detail(data) {
      console.log(data)
      this.detailInfo = data
      this.visibleDetails = true
    },
    //取消调度任务
    async carryinterfacetask(record) {
      console.log(record)
      let res = await carrytask({ id: record.id })
      console.log(res)
      if (res.success) {
        this.$message.success('取消任务成功!')
        this.remoteParams = { ...this.remoteParams }
      } else {
        this.$exception(res)
      }
    },
    doneBefore(res) {
      //   res.data.list.forEach(item => {
      //     item.id = item.id
      //   })
      console.log(res)
      return res
    },
    hanlderSubmit(e, form) {
      e.preventDefault()
      form.validateFields((error, values) => {
        console.log(error, values, 'valuesvaluesvaluesvaluesvalues')
        // 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
          value["createUser"] = this.$store.getters.userInfo.userId;
          
          if (this.type) {
            let res = await stationupdate(value)
            console.log(res)
            if (res.success) {
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          } else {
            let res = await add(value)
            console.log(res)
            if (res.success) {
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          }

          this.confirmLoading = false
          //
        }
      })
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
      this.visibleDetails = false
      this.visiblepriority = false
    },
    //点击弹出
    showmodeladd(data) {
      console.log(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
      console.log(text)
      this.$confirm({
        title: '确定是否要删除?',
        content: '',
        async onOk() {
          let res = await stationdelete({ id: text.id })
          if (res.success) {
            that.$message.success('删除成功')
            that.remoteParams = { ...that.remoteParams }
          } else {
            that.$exception(res)
          }
        },
        onCancel() {},
      })
    },
  },
}
</script>

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