<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" :inline="true">
        <el-row>
          <el-form-item class="query-form-item" label="条码:">
            <el-input v-model="filterQuery.code" placeholder="条码"
                      clearable="true"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button-group>
              <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
              <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
              <el-button type="primary" icon="el-icon-edit" @click="editTime">编辑</el-button>
            </el-button-group>
          </el-form-item>
        </el-row>
      </el-form>
      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="条码" prop="code" show-overflow-tooltip></el-table-column>
        <el-table-column label="扫码数量" prop="count"></el-table-column>
        <el-table-column label="实际数量" prop="reCount"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteDialog(scope.row)"
            >减一
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>


    <el-dialog
      title="编辑时间"
      :visible.sync="editDialogVisible"
      width="45%"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-if="editDialogVisible"
    >
      <el-form label-width="100px">
        <el-row>
          <el-col :span="14">
            <el-form-item label="创建时间:">
              <el-date-picker
                v-model="createTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions"
                placeholder="选择日期时间"
                style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="14">
            <el-form-item label="审核时间:">
              <el-date-picker
                v-model="auditTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions"
                placeholder="选择日期"
                style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div style="text-align: right; margin-top: 10px;">
        <el-button type="primary" size="small" icon="search" @click="updateOrderInfo" :loading="updateOrderInfoLoading"
        >提交
        </el-button
        >
        <el-button type="primary" size="small" icon="search" @click="editDialogVisible = !editDialogVisible"
        >取消
        </el-button
        >
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  deleteInvByCode, updateOrderInfo
} from "../../api/inout/order";
import {getCodeListForEdit} from "@/api/inout/code";
import {isBlank} from "@/utils/strUtil";


const formJson = {
  site_id: "",
  site_name: "",
  describe: "",
  ads: [],
};

export default {
  name: "IoEditOrderCodeDetail",
  props: {
    order: {
      type: Object,
      required: true
    },
    getOrderList: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      filterQuery: {
        orderId: null,
        code: null,
        page: 1,
        limit: 10
      },
      list: [],
      total: 0,
      loading: false,
      index: null,
      deleteData: {
        id: "",
      },
      formLoading: false,
      formData: formJson,
      deleteLoading: false,
      editDialogVisible: false,
      createTime: null, //创建时间
      auditTime: null, //审核时间
      pickerOptions: {
        disabledDate: time => {
          let endDate = new Date().getTime();
          let nowDate = new Date(time).getTime();
          return (nowDate > endDate);
        }
      },
      updateOrderInfoLoading: false
    };
  },
  components: {},
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: this.order.billNo,
        code: null,
        page: 1,
        limit: 10
      };
      this.getList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    //获取订单列表
    getList() {
      this.loading = true;
      getCodeListForEdit(this.filterQuery)
        .then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
        })
        .catch((error) => {
          this.$message.error(error.message)
          this.loading = false;
          this.list = [];
        });
    },
    editTime() {
      this.editDialogVisible = true;
    },
    updateOrderInfo() {
      if (isBlank(this.createTime)) {
        this.$message.warning("创建时间不能为空");
        return;
      }
      if (isBlank(this.auditTime)) {
        this.$message.warning("审核时间不能为空");
        return;
      }
      this.editDialogVisible = false;
      let params = {
        createTime: this.createTime,
        auditTime: this.auditTime,
        orderIds: [this.filterQuery.orderId]
      };
      this.updateOrderInfoLoading = true
      updateOrderInfo(params).then((res) => {
        this.updateOrderInfoLoading = false
        if (res.code === 20000) {
          this.$message.success("修改成功");
          this.getList();
          this.getOrderList();
        }
      }).catch((error) => {
        this.updateOrderInfoLoading = false
        this.$message.error(error.message);
      })
    },
    deleteDialog(row) {
      this.$confirm("是否确定库存减一?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteInvByCode(row).then((res) => {
            if (res.code === 20000) {
              this.$message.success("操作成功");
              this.getList();
            } else {
              this.$message.error(res.message);
            }
          }).catch((error) => {
            this.$message.error(error.message);
          });
        })
        .catch(() => {
        });
    },
  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        false: "success",
        true: "warning",
      };
      return statusMap[status];
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.filterQuery.orderId = this.order.billNo;
    this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>
