<template>
  <div>
    <div>
      <p-card title="计划创建">
        <div>
          <p-grid-form ref="formTarget" :items="formItems" @submit="hanlderSubmit"> </p-grid-form>
        </div>
        <div>
          <p-table
            ref="logTable"
            style="margin-top: 10px"
            :columns="tableColumns"
            rowKey="id"
            :expandRowByClick="false"
            :dataSource.sync="tableOnelist"
            :loading="tabLoding"
            dragle
            auto-width
            bordered
          >
            <div slot="action" href="javascript:;" slot-scope="text, record">
              <a slot="action" style="margin: 0 5px" @click.stop="detail(record)">明细</a>
              <a slot="action" style="margin: 0 5px" @click.stop="IssueTask(record)">下发</a>
              <a slot="action" style="margin: 0 5px" @click.stop="cancel(record)">删除</a>
            </div>
          </p-table>
        </div>
        <a slot="extra" href="#">
          <p-button type="primary" icon="plus" @click="addTask">盘点计划</p-button>
        </a>
      </p-card>
    </div>
    <div>
      <p-modal
        title="添加盘点计划"
        :visible="visible"
        :confirm-loading="confirmLoading1"
        @ok="handleOk"
        @cancel="handleCancel('plan')"
      >
        <p style="margin: 10px">
          <p-grid-form ref="setForm" :items="setFormItems" @submit="hanlderSubmit"> </p-grid-form>
        </p>
      </p-modal>
      <p-modal
        title="盘点计划明细"
        :width="1000"
        :visible="planvisible"
        :confirm-loading="confirmLoading2"
        :footer="null"
        @ok="handleOklist"
        @cancel="handleCancel('detail')"
      >
        <div>
          <p-button type="primary" style="margin: 0 5px" icon="plus" @click="addTaskDetail">盘点明细添加</p-button>
          <p-button type="primary" style="margin: 0 5px" icon="delete" @click="deleteAll">全部删除</p-button>
          <p-button type="primary" style="margin: 0 5px" icon="delete" @click="deleteSome">批量删除</p-button>
        </div>
        <p style="margin: 10px">
          <p-table
            ref="detailTable"
            style="margin-top: 10px"
            :columns="detailColumns"
            rowKey="id"
            :remoteUrl="{ url: '/kz/check/getCheckDetail', method: 'post', params: remoteParams1 }"
            :autoLoad="false"
            :remoteData="remoteParams1"
            :remoteParams="remoteParams1"
            :doneBefore="doneBefore"
            :loading="Loding"
            @checkRow="checkRow1"
            dragle
            auto-width
            bordered
          >
            <div slot="action" href="javascript:;" slot-scope="text, record">
              <a slot="action" style="margin: 0 5px" @click.stop="deletePlan(record)">删除</a>
            </div>
          </p-table>
          <a-row>
            <a-col :span="4"> 初盘完成数量：{{ detailObj.firstCompleteNum }} </a-col>
            <a-col :span="4"> 盘点明细初盘总数：{{ detailObj.firstNum }} </a-col>
            <a-col :span="4"> 复盘完成数量：{{ detailObj.secondCompleteNum }} </a-col>
            <a-col :span="4"> 复盘差异处理完成数量：{{ detailObj.secondDiffCompleteNum }} </a-col>
            <a-col :span="2"></a-col>
            <a-col :span="4"> 盘点明细总数：{{ detailObj.sumNum }} </a-col>

          </a-row>
          <a-row>
            <a-col :span="4"> 初盘未完成数量：{{ detailObj.firstNotCompleteNum }} </a-col>
            <a-col :span="4"> 盘点明细复盘总数：{{ detailObj.secondNum }} </a-col>
            <a-col :span="4"> 复盘未完成数量：{{ detailObj.secondNotCompleteNum }} </a-col>
            <a-col :span="4"> 复盘差异未处理完成数量：{{ detailObj.secondDiffNotCompleteNum }} </a-col>
          </a-row>
        </p>
      </p-modal>
      <p-modal
        title="盘点货架明细"
        v-if="planDeatilvisible"
        :width="1300"
        :visible="planDeatilvisible"
        :confirm-loading="confirmLoading3"
        @ok="handleOkDetail"
        @cancel="OnCancel"
      >
        <template slot="footer">
          <a-radio-group v-model="value1" :options="radioOptions" />
          <a-button key="back" @click="OnCancel"> 取消 </a-button>
          <a-button type="primary" :disabled="settimeStatus" @click="handleOkDetail('all')"> 全部盘点 </a-button>
          <a-button type="primary" :disabled="settimeStatus" @click="handleOkDetail('some')"> 按条件盘点 </a-button>
          <a-button key="submit" :disabled="settimeStatus" type="primary" @click="handleOkDetail"> 确定 </a-button>
        </template>
        <p style="margin: 10px">
          <p-grid-form ref="planFormItems" :items="planFormItems" @submit="planHanlderSubmit"> </p-grid-form>
          <p-table
            ref="detailListTable"
            style="margin-top: 10px"
            :columns="Columns"
            rowKey="id"
            :autoLoad="true"
            :remoteUrl="{ url: '/kz/check/getAllCheckList', method: 'post' }"
            :remoteData="remoteParams"
            :remoteParams="remoteParams"
            :loading="Loding"
            @checkRow="checkRow"
            :selectedRowKeys="selectedRowKeys"
            dragle
            auto-width
            bordered
          >
          </p-table>
        </p>
      </p-modal>
    </div>
  </div>
</template>
<script>
import { formItems, setFormItems, tableColumns, detailColumns, Columns, planFormItems } from './js/config'
import {
  getlist,
  creatPlan,
  addCheckDetail,
  IssueTask,
  deleteCheckPlan,
  deleteCheckDetail,
  deleteWholeCheckDetail,
} from './api'
export default {
  components: {},
  data() {
    return {
      detailColumns: detailColumns(this),
      Columns: Columns(this),
      formItems: formItems(this),
      setFormItems: setFormItems(this),
      planFormItems: planFormItems(this),
      tableColumns: tableColumns,
      tableOnelist: [],
      tabLoding: false,
      visible: false,
      confirmLoading1: false,
      confirmLoading2: false,
      confirmLoading3: false,
      planRemark: '',
      planvisible: false,
      Loding: false,
      detailData: [],
      planDeatilvisible: false,
      tableData: [],
      checkTaskNo: '',
      selectedRows: [],
      agvFlag: '1',
      selectedRowKeys: [],
      value1: 1,
      radioOptions: [
        { label: '明盘', value: 1 },
        { label: '暗盘', value: 2 },
      ],
      remoteParams: {},
      remoteParams1: {},
      selectedRowKeys1: [],
      detailObj: {
        firstCompleteNum: 0,
        firstNotCompleteNum: 0,
        firstNum: 0,
        secondCompleteNum: 0,
        secondDiffCompleteNum: 0,
        secondDiffNotCompleteNum: 0,
        secondNotCompleteNum: 0,
        secondNum: 0,
        sumNum: 0,
      },
      tiemID: null,
      settimeStatus: false
    }
  },
  created() {
    this.getlist()
  },
  watch: {
    agvFlag: {
      handler(val) {
        this.detailColumns = detailColumns(this)
        this.Columns = Columns(this)
        this.setFormItems = setFormItems(this)
        this.planFormItems = planFormItems(this)
      },
      immediate: true
    }
  },
  methods: {
    getlist(data) {
      let params
      if (data) {
        params = data
      } else {
        params = {}
      }
      this.tabLoding = true
      // params.agvFlag = 1
      getlist(params).then((res) => {
        this.tableOnelist = res.data.list
        this.tabLoding = false
      })
    },
    hanlderSubmit(evnet, form) {
      evnet.preventDefault()
      this.getlist(form.getFieldsValue())
    },
    planHanlderSubmit(evnet, form) {
      evnet.preventDefault()
      let data = form.getFieldsValue()
      this.remoteParams = { ...this.remoteParams, ...data }
    },
    edit(text) {
      console.log(text)
    },
    handleOk(e) {
      this.confirmLoading = true
      this.$refs.setForm.form.validateFields((error, values) => {
        creatPlan(values).then((res) => {
          this.confirmLoading = false
          if (res.success) {
            this.visible = false
            this.$nextTick(() => {
              this.$refs.setForm.form.resetFields()
            })
            // this.$refs.setForm.form.resetFields()
            this.getlist(this.$refs.formTarget.form.getFieldsValue())
            this.$message.success(res.message)
          } else {
            this.$message.error(res.message)
          }
        })
      })
    },
    addTask() {
      this.visible = true
    },
    addTaskDetail(data) {
      this.planDeatilvisible = true
    },
    handleCancel(data) {
      this.planvisible = false
      if (data == 'plan') {
        this.$nextTick(() => {
          this.$refs.setForm.form.resetFields()
          this.visible = false
        })
      }
      this.planRemark = ''
    },
    OnCancel() {
      this.planDeatilvisible = false
      this.remoteParams = {}
    },
    detail(data) {
      this.checkTaskNo = data.checkTaskNo
      this.agvFlag = data.agvFlag
      this.planvisible = true
      this.remoteParams1.checkTaskNo = this.checkTaskNo
      this.$nextTick(() => {
        this.$refs.detailTable.reload()
      })
    },
    deleteAll() {
      deleteWholeCheckDetail({ checkTaskNo: this.checkTaskNo }).then((res) => {
        this.planvisible = true
        if (res.success) {
          this.detailData = []
          this.$refs.detailTable.reload()
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    deleteSome() {
      const { checkTaskNo } = this
      if (this.selectedRowKeys1 < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      this.deleteCheckDetail({ checkDetailIds: this.selectedRowKeys1, checkTaskNo }).then((res) => {
        if (res.success) {
          this.$refs.detailTable.reload()
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    deletePlan(record) {
      const { checkTaskNo } = this
      this.deleteCheckDetail({ checkDetailIds: [record.id], checkTaskNo })
      // deleteCheckDetail({ checkDetailIds: [record.id] }).then(res => {
      //   if (res.success) {
      //     this.$message.success(res.message)
      //     this.$refs.detailTable.reload()
      //   } else {
      //     this.$message.error(res.message)
      //   }
      // })
    },
    deleteCheckDetail(data) {
      deleteCheckDetail(data).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.$refs.detailTable.reload()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    checkRow(selectedRowKeys, selectedRows) {
      console.log(selectedRows, selectedRowKeys)
      this.selectedRows = selectedRows
    },
    checkRow1(selectedRowKeys, selectedRows) {
      console.log(selectedRows, selectedRowKeys)
      this.selectedRowKeys1 = selectedRowKeys
    },
    handleOkDetail(key) {
      clearTimeout(this.tiemID);
			this.tiemID = setTimeout(() => {
        this.settimeStatus = true
				console.log('执行节流函数');
        let params = {
          checkTaskNo: this.checkTaskNo,
          containerStore: key == 'all' ? '' : this.selectedRows,
          isAll: key == 'all' ? 1 : '',
          checkType: this.value1,
        }
        this.confirmLoading3 = true
        if(key == 'some') {
          params = {
            checkTaskNo: this.checkTaskNo,
            getAllCheckListVO: this.$refs.planFormItems.form.getFieldsValue(),
            checkType: this.value1,
            isAllByCondition: 1,
          }
        }
        addCheckDetail(params).then((res) => {
          this.settimeStatus = false
          this.confirmLoading3 = false
          if (res.success) {
            this.planDeatilvisible = false
            this.$message.success(res.message)
            this.remoteParams = {}
            this.$refs.detailTable.reload()
          } else {
            this.$message.error(res.message)
          }
        })
			}, 1000);
    },
    IssueTask(data) {
      IssueTask({ checkTaskNo: data.checkTaskNo }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.getlist(this.$refs.formTarget.form.getFieldsValue())
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleOklist() {
      this.planvisible = false
    },
    cancel(data) {
      deleteCheckPlan({ checkTaskNo: data.checkTaskNo }).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.getlist(this.$refs.formTarget.form.getFieldsValue())
        } else {
          this.$message.error(res.message)
        }
      })
    },
    doneBefore(res) {
      for (const key in res.data) {
        if (Object.hasOwnProperty.call(res.data, key)) {
          if (key !== 'list') {
            this.detailObj[key] = res.data[key]
          }
        }
      }
      let listData = res.data.list
      res.data = listData
      console.log(this.detailObj, 'this.detailObj')
    },
    //盘点计划弹框
  },
}
</script>
<style lang="less" scoped></style>
