<template>
  <div>
    <basic-container v-show="!detailsOpen">
      <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
        <el-tab-pane label="申请明细" name="first">
          <avue-crud :option="option" :search.sync="search" v-model="form" :table-loading="loading" :data="dataList"
            ref="crud" :key="key" @on-load="onLoad" @search-change="searchChange" @row-del="rowDel"
            @selection-change="selectionChange" @expand-change="expandChange" @refresh-change="refreshChange"
            @resetColumn="resetColumnTwo('crud', 'option', 'optionList', 362)"
            @saveColumn="saveColumnTwo('crud', 'option', 'optionList', 362)" :page.sync="page">
            <!-- <template slot-scope="{type,size,row,index}" slot="menu">
              <el-button :size="size" :type="type" :disabled="row.issueStatus != 'D'"
                @click="$refs.crud.rowDel(row, index)">删除</el-button>
            </template> -->
            <template slot-scope="{type,size,row,$index}" slot="menuLeft">
              <el-button type="success" plain :size="size" @click="applicationDetail">生成调度</el-button>
            </template>
            <!-- <template slot="billNo" slot-scope="{ row, index }">
              <div style="color: #1e9fff;cursor: pointer;" @click.stop="editOpen(row)">
                {{ row.billNo }}
              </div>
            </template> -->
          </avue-crud>
        </el-tab-pane>
        <el-tab-pane label="调度单据" name="second">
          <avue-crud :option="option" :search.sync="search2" v-model="form2" :table-loading="loading" :data="dataList2"
            ref="crud2" :key="key" @on-load="onLoad2" @search-change="searchChange2" @row-del="rowDel"
            @refresh-change="refreshChange2" @resetColumn="resetColumnTwo('crud2', 'option', 'optionList', 362)"
            @saveColumn="saveColumnTwo('crud2', 'option', 'optionList', 362)" :page.sync="page2">
            <template slot-scope="{type,size,row,index}" slot="menu">
              <!-- <el-button :size="size" :type="type" @click="check(row)">查看</el-button> -->
              <el-button :size="size" :type="type" :disabled="row.issueStatus != 'D'"
                @click="$refs.crud.rowDel(row, index)">删除</el-button>
            </template>
            <!-- <template slot-scope="{type,size,row,$index}" slot="menuLeft">
              <el-button  type="success" plain :size="size" @click="detailsOpen = true">申请明细</el-button>
            </template> -->
            <template slot="billNo" slot-scope="{ row, index }">
              <div style="color: #1e9fff;cursor: pointer;" @click.stop="editOpen(row)">
                {{ row.billNo }}
              </div>
            </template>
          </avue-crud>
        </el-tab-pane>
      </el-tabs>
    </basic-container>
    <detailsPage v-if="detailsOpen" @goBack="goBack" :onLoad="form" :detailData="detailData" @backToList="backToList"
      :dispatchList="dispatchList">
    </detailsPage>
  </div>
</template>

<script>
import { getList, remove, getList2 } from "@/api/iosBasicData/release/schedulingManagement";
import detailsPage from "./detailsPage"
import { getToken } from "@/util/auth";
import { application } from "../../../../api/iosBasicData/release/jobApplication";

export default {
  name: "index",
  components: {
    detailsPage
  },
  data() {
    return {
      dispatchList: [],
      selectionList: [],
      activeName: 'first',
      detailsOpen: false,
      loading: false,
      search: {},
      form: {},
      dataList: [],
      detailData: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      search2: {},
      form2: {},
      dataList2: [],
      page2: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      key: 0,
      option: {},
      optionList: {
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        index: true,
        span: 8,
        border: true,
        height: "auto",
        searchMenuPosition: "right",
        align: "center",
        size: "small",
        menuWidth: 50,
        searchSpan: 8,
        searchIcon: true,
        searchIndex: 2,
        highlightCurrentRow: true,
        dialogWidth: "70%",
        showSummary: true,
        tip: false,
        selection: true,
        column: [
          // {
          //   label: '*年度',
          //   prop: "ordNo",
          //   search: true,
          //   overHidden: true,
          //   type: "year",
          // },
          // {
          //   label: '*月份',
          //   prop: "customerName",
          //   search: true,
          //   overHidden: true,
          //   type: "month",
          // },
          {
            label: '单据编号',
            prop: "billNo",
            search: true,
            overHidden: true,
          },
          {
            label: 'MBL NO',
            prop: 'mblno',
            search: true,
            overHidden: true,
          },
          {
            label: '客户简称',
            prop: "corpCnName",
            search: true,
            overHidden: true,
            filterable: true,
            type: 'select',
            props: {
              label: 'cnName',
              value: 'cnName',
              res: 'data.records'
            },
            dicUrl: '/api/blade-los/bcorps/listByType?current=1&size=10&shortName={{key}}',
          },
          {
            label: '单据日期',
            prop: "billDate",
            searchProp: "billDateList",
            type: "date",
            overHidden: true,
            search: true,
            width: 100,
            searchRange: true,
            searchDefaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd HH:mm:ss"
          },
          {
            label: '单据状态',
            prop: "issueStatus",
            overHidden: true,
            type: "select",
            dicData: [{
              id: 'N',
              name: "新建",
            }, {
              id: 'S',
              name: "提交",
            }, {
              id: 'D',
              name: "跑单中",
            }, {
              id: 'O',
              name: "操作确认",
            }, {
              id: 'I',
              name: "调度确认",
            }, {
              id: 'T',
              name: "放单",
            }, {
              id: 'B',
              name: "退回",
            }],
            props: {
              label: "name",
              value: "id",
            },
          },
          {
            label: '提交时间',
            prop: "applyTime",
            searchProp: "applyTimeList",
            type: "date",
            overHidden: true,
            search: true,
            width: 100,
            searchRange: true,
            searchDefaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd HH:mm:ss"
          },
          {
            label: '安排时间',
            prop: "requireTime",
            searchProp: "requireTimeList",
            type: "date",
            overHidden: true,
            search: true,
            width: 100,
            searchRange: true,
            searchDefaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd HH:mm:ss"
          },
          {
            label: '制单人',
            prop: "createUserName",
            searchProp: "createUser",
            overHidden: true,
            width: 100,
            filterable: true,
            remote: true,
            type: "select",
            dicUrl: "/api/blade-user/page?size=20&current=1&account={{key}}",
            props: {
              label: "account",
              value: "id",
              res: 'data.records'
            }
          },
          // {
          //   label: ' *审核状态',
          //   prop: "status",
          //   search: true,
          //   overHidden: true,
          // },
          {
            label: '备注',
            prop: "remarks",
            overHidden: true,
          },
          {
            label: '提单份数',
            prop: "numberOfObl",
            overHidden: true,
          }
        ]
      }
    }
  },
  activated() {
    setTimeout(() => {
    }, 100);
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(362), this.optionList);
    this.key++
    let i = 0;
    this.option.column.forEach(item => {
      if (item.search) i++
    })
    if (i % 3 !== 0) {
      const num = 3 - Number(i % 3)
      this.option.searchMenuSpan = num * 8;
      this.option.searchMenuPosition = "right";
    }
    this.getWorkDicts("sales_Status").then(res => {
      this.orderStatusList = res.data.data
    });
  },
  methods: {
    selectionChange(list) {
      this.selectionList = list
    },
    handleClick(tab, event) {
      if (tab == 'first') {
        this.onLoad(this.page, this.search)
      } else {
        this.onLoad2(this.page2, this.search2)
      }
    },
    check(row) {
      this.form = row
      this.detailsOpen = true
    },
    backToList(type) {
      this.form = {}
      this.detailsOpen = false
      if (type === 0) {
        this.detailData = {}
      }
      this.onLoad(this.page, this.search)

    },
    editOpen(row, status) {
      this.form = row
      this.detailData = {
        id: row.id,
        status: status
      };
      this.detailsOpen = true;
    },
    //刷新
    refreshChange() {
      this.onLoad(this.page, this.search)
    },
    rowDel(form, index) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove({ id: form.id }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.dataList2.splice(index, 1);
          this.onLoad2(this.page2)
        })
      }).catch(() => {
      });
    },
    searchChange(params, done) {
      this.page.currentPage = 1
      done();
      this.onLoad(this.page, params)
    },
    onLoad(page, params = {}) {
      params = {
        issueStatus: 'O',
        ...params,
        current: page.currentPage,
        size: page.pageSize,
        ...Object.assign(params, this.search)
      }
      this.loading = true
      getList2(params).then(res => {
        if (res.data.data.records) {
          res.data.data.records.forEach(e => {
            e.itemLoading = true;
          });
        }
        this.dataList = res.data.data.records
        this.page.total = res.data.data.total
        this.$nextTick(() => {
          this.$refs.crud.doLayout()
        })
        this.loading = false
      }).finally(() => {
        this.loading = false
      })
    },
    //刷新
    refreshChange2() {
      this.onLoad2(this.page2, this.search)
    },
    searchChange2(params, done) {
      this.page2.currentPage = 1
      done();
      this.onLoad2(this.page2, params)
    },
    onLoad2(page, params = {}) {
      params = {
        ...params,
        current: page.currentPage,
        size: page.pageSize,
        ...Object.assign(params, this.search2),
        billType: 'D',
      }
      this.loading = true
      getList(params).then(res => {
        if (res.data.data.records) {
          res.data.data.records.forEach(e => {
            e.itemLoading = true;
          });
        }
        this.dataList2 = res.data.data.records
        this.page2.total = res.data.data.total
        this.$nextTick(() => {
          this.$refs.crud.doLayout()
        })
        this.loading = false
      }).finally(() => {
        this.loading = false
      })
    },
    //申请明细
    applicationDetail() {
      if (this.selectionList.length == 0) {
        this.$message({
          type: 'error',
          message: '请选择单据!'
        });
      } else {
        this.$confirm('确定生成调度?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = []
          this.dispatchList = this.selectionList
          this.detailsOpen = true
        })
      }

    },
    //自定义列保存
    async saveColumnTwo(ref, option, optionBack, code) {
      /**
       * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
       * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
       * 一定要执行异步操作，要等接口成功返回，才能执行下一行代码
       */
      const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    },
    //自定义列重置
    async resetColumnTwo(ref, option, optionBack, code) {
      this[option] = this[optionBack];
      const inSave = await this.delColumnData(this.getColumnName(code), this[optionBack]);
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    }
  }
}
</script>

<style scoped>
.itemTable ::v-deep .el-table {
  margin-left: 50px;
  width: 100%;
}

.bottomBox {
  padding: 3px 6px;
  border-radius: 12px;
  color: #fff;
  font-size: 10px;
}
</style>
