<template>
  <!-- 订货清单审核 -->
  <el-container>
    <el-header style="width: 100%">
      <el-row>
        <el-col :span="18">
          <el-col :span="4">
            <el-select v-model="request.dcCh" placeholder="船号" clearable>
              <el-option
                v-for="item in ShipNoList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button type="primary" @click="initList()">查询</el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="18">
          <el-col :span="4">
            <el-select v-model="request.Zt" placeholder="状态" clearable>
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button type="primary" @click="toolbarButtonClickEvent('back')"
            >退回</el-button
          >
          <el-button type="success" @click="toolbarButtonClickEvent('check')"
            >审核</el-button
          >
        </el-col>
      </el-row>
    </el-header>
    <el-container style="padding-top: 10px">
      <el-container>
        <el-main>
          <div class="mytable">
            <vxe-grid
              id="id"
              ref="tableRef"
              border
              resizable
              keep-source
              :height="height"
              :auto-resize="true"
              :columns="tableColumn"
              :data="list"
              :custom-config="{ storage: true }"
              @checkbox-all="selectAllEvent"
              @checkbox-change="selectChangeEvent"
              highlight-hover-row
            >
              <template #dcCh_default="{ row }">
                {{ row.dcCh }}
              </template>
              <template #Zt_default="{ row }">
                <div v-for="(item, index) in statusOptions" :key="index">
                  <div v-if="row.zt === item.value">{{ item.label }}</div>
                </div>
              </template>
            </vxe-grid>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { getList, tj, save, sh, upload } from "@/api/jhzx/dhqdSH";
export default {
  name: "dhqdSH",
  data() {
    return {
      request: {
        dcCh: "",
        Zt: "1",
      },
      list: [],
      height: "500px",
      tableColumn: [
        { type: "checkbox", width: 50, fixed: "left" },
        { type: "seq", width: 60, title: "序号", fixed: "left" },
        {
          field: "dcCh",
          title: "船号",
          slots: { default: "dcCh_default" },
          minWidth: "70",
        },
        {
          field: "qdh",
          title: "订货清单号",
          minWidth: "80",
        },
        {
          field: "qc",
          title: "期次",
          minWidth: "70",
        },
        {
          field: "wpxh",
          title: " 材质",
          minWidth: "70",
        },
        {
          field: "thickness",
          title: "厚度mm",
          minWidth: "70",
        },
        {
          field: "width",
          title: "宽度mm",
          minWidth: "70",
        },
        {
          field: "length",
          title: "长度mm",
          minWidth: "70",
        },
        {
          field: "weight",
          title: "重量吨/张",
          minWidth: "70",
        },
        {
          field: "sjsl",
          title: "设计数量",

          minWidth: "70",
        },
        {
          field: "yl",
          title: "裕量",
          minWidth: "70",
        },
        {
          field: "zsl",
          title: "总数量",
          minWidth: "70",
        },
        {
          field: "zzl",
          title: "总重量",
          minWidth: "70",
        },
        {
          field: "lksl",
          title: "利库数量",
          minWidth: "70",
        },
        {
          field: "lkzl",
          title: "利库重量",
          minWidth: "70",
        },
        {
          field: "sdzs",
          title: "实订数量",
          minWidth: "70",
        },
        {
          field: "sdzl",
          title: "实订重量",
          minWidth: "70",
        },
        {
          field: "Zt",
          title: "状态",
          slots: { default: "Zt_default" },
          minWidth: "50",
          fixed: "right",
        },
      ],
      statusOptions: [
        {
          value: "0",
          label: "编辑",
        },
        {
          value: "1",
          label: "提交",
        },
        {
          value: "2",
          label: "审核",
        },
      ],
      fileList: [],
      ShipNoList: [],
      checkedList: [],
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
    this.initList();
  },
  methods: {
    // 获取列表接口(船号)
    initList() {
      let params = {
        Zt: this.request.Zt,
        dcCh: "",
      };
      if (this.ShipNoList.length > 0 && this.request.dcCh) {
        params.dcCh = this.ShipNoList[this.request.dcCh - 1].label;
      } else {
        params.dcCh = "";
      }
      getList(params).then((res) => {
        this.list = res.data;
        this.checkedList = [];
        this.ShipNoList = [];
        var arrList = [];
        if (this.list.length > 0) {
          this.list.forEach((item, index) => {
            arrList.push(item.dcCh);
            this.ShipNoList = []
            Array.from(new Set(arrList)).forEach((em, eindex) => {
              this.ShipNoList.push({
                label: em,
                value: eindex + 1,
              });
            });
          });
        }
      });
    },
    // 上传excel
    actionUpload(file) {
      const formData = new FormData();
      formData.append("file", file.file);
      upload(formData).then((res) => {
        if (res.success) {
        }
      });
    },
    // 全选
    selectAllEvent() {
      const records = this.$refs.tableRef.getCheckboxRecords();
      this.checkedList = records;
    },
    // 单选
    selectChangeEvent() {
      const records = this.$refs.tableRef.getCheckboxRecords();
      this.checkedList = records;
    },
    // 操作
    async toolbarButtonClickEvent(code) {
      switch (code) {
        case "back":
          if (this.checkedList.length > 0) {
            var status = 1;
            this.checkedList.forEach((item) => {
              if (item.zt !== "1") {
                status = 0;
              }
            });

            if (status == 1) {
              this.$confirm("是否确认提交?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  tj(this.checkedList).then((res) => {
                    if (res.success) {
                      const xTable = this.$refs.tableRef;
                      xTable.loadData(this.initList());
                      this.$message.success("退回成功！");
                    }
                  });
                })
                .catch(() => {});
            } else {
              this.$message.warning("请选择提交状态的数据!");
            }
          } else {
            this.$message.warning("请选择数据！");
          }
          break;
        case "check":
          if (this.checkedList.length > 0) {
            this.$confirm("是否确认审核选择的数据?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            })
              .then(() => {
                sh(this.checkedList).then((res) => {
                  if (res.success) {
                    const xTable = this.$refs.tableRef;
                    xTable.loadData(this.initList());
                    this.$message.success("审核成功！");
                  }
                });
              })
              .catch(() => {});
          } else {
            this.$message.warning("请选择数据！");
          }
          break;
        default:
          break;
      }
    },
    getHeight() {
      this.height = window.innerHeight - 160;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-header {
  margin: 0;
  padding: 5px 0;
  height: auto;
}
.el-container {
  padding: 0;
  margin: 0;
}
.el-main {
  padding: 0;
}
.el-col {
  padding: 0;
}
.el-aside {
  background: #fff;
  padding: 0;
}
.el-container {
  padding: 0;
}
.el-row {
  margin-bottom: 5px;
}
.el-row:last-child {
  margin-bottom: 0;
}
.tableStyles {
  background: #0a76a4;
}
.my-dropdown {
  width: 450px;
  height: 250px;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  box-shadow: 0 0 6px 2px rgba(0, 0, 0, 0.1);
  font-size: 12px;
}
::v-deep .mytable .vxe-input {
  display: flex !important;
}
.upload-demo {
  display: inline-flex;
  margin-right: 10px;
}
::v-deep .el-upload-list {
  display: none !important;
}
</style>
