<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      label-width="100px"
      size="mini"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="客户名称" prop="refno1">
            <el-select
              v-model="queryParams.refno1"
              placeholder="请输入客户名称"
              clearable
              size="small"
              filterable
            >
              <el-option
                v-for="(dict, index) in factIdOptions"
                :key="index.fId"
                :label="dict.fName"
                :value="dict.fId"
              />
            </el-select> </el-form-item
        ></el-col>
        <el-col :span="6">
          <el-form-item label-width="100px" label="业务类型" prop="rbillId">
            <el-select
              v-model="queryParams.rbillId"
              placeholder="请选择业务类型"
              clearable
              size="small"
              multiple
            >
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="100px" label="提单号" prop="actId">
            <el-input
              v-model="queryParams.actId"
              placeholder="请输入提单号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="max-width: 187px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="100px" label="系统编号" prop="billNo">
            <el-input
              v-model="queryParams.billNo"
              placeholder="请输入系统编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="max-width: 187px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse-transition>
        <div v-show="show">
          <el-row>
            <el-col :span="6">
              <el-form-item
                label-width="100px"
                label="请核人"
                prop="sendUserId"
              >
                <el-select
                  v-model="queryParams.sendUserId"
                  placeholder="请选择请核人"
                  clearable
                  size="small"
                  multiple
                >
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label-width="100px"
                label="审核人"
                prop="auditUserId"
              >
                <el-select
                  v-model="queryParams.auditUserId"
                  placeholder="请输入审核人"
                  clearable
                  size="small"
                  multiple
                >
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label-width="100px"
                label="提交日期"
                prop="sendTime"
              >
                <el-date-picker
                  v-model="queryParams.sendTime"
                  size="small"
                  @keyup.enter.native="handleQuery"
                  style="max-width: 187px"
                  type="datetimerange"
                  align="right"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :default-time="['00:00:00', '23:59:59']"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label-width="100px"
                label="业务日期"
                prop="billTime"
              >
                <el-date-picker
                  v-model="queryParams.billTime"
                  placeholder="请输入业务日期"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                  style="max-width: 187px"
                  type="date"
                >
                </el-date-picker> </el-form-item
            ></el-col>
          </el-row>
          <el-row>
            <el-col :span="6"
              ><el-form-item
                label-width="100px"
                label="审核日期"
                prop="auditOpTime"
              >
                <el-date-picker
                  v-model="queryParams.auditOpTime"
                  placeholder="请输入审核日期"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                  style="max-width: 187px"
                  type="date"
                >
                </el-date-picker></el-form-item
            ></el-col>
          </el-row>
        </div>
      </el-collapse-transition>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button size="mini" @click="handleQuery">全部</el-button></el-col
      >
      <el-col :span="1.5">
        <el-button size="mini" @click="handleQuery">待审业务</el-button></el-col
      >
      <el-col :span="1.5">
        <el-button size="mini" @click="handleQuery">审核中</el-button></el-col
      >
      <el-col :span="1.5">
        <el-button size="mini" @click="handleQuery">审核通过</el-button></el-col
      >

      <div class="tabSetting">
        <div style="margin-right: 20px">
          <el-button
            type="cyan"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
          <el-button
            v-show="show"
            @click="show = !show"
            icon="el-icon-arrow-up"
            size="mini"
            >展开</el-button
          >
          <el-button
            v-show="!show"
            @click="show = !show"
            icon="el-icon-arrow-down"
            size="mini"
            >展开</el-button
          >
        </div>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
        <div style="margin: 0 12px">
          <el-tooltip
            class="item"
            effect="dark"
            content="列设置"
            placement="top"
          >
            <el-button
              icon="el-icon-setting"
              size="mini"
              circle
              @click="showSetting = !showSetting"
            ></el-button>
          </el-tooltip>
        </div>
      </div>
    </el-row>
    <el-dialog title="自定义列显示" :visible.sync="showSetting" width="700px">
      <div>配置排序列数据（拖动调整顺序）</div>
      <div style="margin-left: 17px">
        <el-checkbox
          v-model="allCheck"
          label="全选"
          @change="allChecked"
        ></el-checkbox>
      </div>
      <div style="padding: 4px; display: flex; justify-content: center">
        <draggable
          v-model="setRowList"
          group="site"
          animation="300"
          @start="onStart"
          @end="onEnd"
          handle=".indraggable"
        >
          <transition-group>
            <div
              v-for="item in setRowList"
              :key="item.surface"
              class="listStyle"
            >
              <div style="width: 500px" class="indraggable">
                <div class="progress" :style="{ width: item.width + 'px' }">
                  <el-checkbox
                    :label="item.name"
                    v-model="item.checked"
                    :true-label="0"
                    :false-label="1"
                    >{{ item.name }}
                  </el-checkbox>
                </div>
              </div>
              <el-input-number
                v-model.number="item.width"
                controls-position="right"
                :min="1"
                :max="500"
                size="mini"
              ></el-input-number>
            </div>
          </transition-group>
        </draggable>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showSetting = false">取 消</el-button>
        <el-button @click="delRow" type="danger">重 置</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </span>
    </el-dialog>
    <el-table
      v-loading="loading"
      :data="selectAuditList"
      :header-cell-style="{ borderBottom: '1px dashed #dfe6ec' }"
    >
      <el-table-column
        v-for="(item, index) in getRowList"
        :key="index"
        :label="item.name"
        :width="item.width"
        :prop="item.label"
        align="center"
        :fixed="item.fixed"
        :show-overflow-tooltip="true"
        sortable
      >
        <template slot-scope="scope">
          <span v-if="item.label == 'refno3'">{{
            scope.row.refno3 ? scope.row.refno3 : scope.row.billNo
          }}</span>
          <span v-else>{{ scope.row[item.label] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        prop="refno1"
        :show-overflow-tooltip="true"
        label="委托方"
      ></el-table-column>
      <el-table-column
        prop="auditItem"
        :show-overflow-tooltip="true"
        label="日期"
      >
        <template slot-scope="scope">
          <span v-if="!scope.row.auditItem">无时间</span>
          <span v-else>{{ scope.row.auditItem.slice(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="refno3"
        :show-overflow-tooltip="true"
        label="提单号"
      ></el-table-column>
      <el-table-column
        prop="refno2"
        :show-overflow-tooltip="true"
        label="业务类型"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.refno2 === 'SJRK'">入库</span>
          <span v-else-if="scope.row.refno2 === 'SJCK'">出库</span>
          <span v-else-if="scope.row.refno2 === 'HQZY'">货转</span>
          <span v-else-if="scope.row.refno2 === 'CKDB'">调拨</span>
          <span v-else-if="scope.row.refno2 === 'CCF'">仓储费</span>
          <span v-else-if="scope.row.refno2 === 'HWTG'">货物通关</span>
          <span v-else-if="scope.row.refno2 === 'ZYF'">作业费</span>
          <span v-else-if="scope.row.refno2 === 'SF'">收费</span>
          <span v-else-if="scope.row.refno2 === 'DZ'">对账</span>
          <span v-else-if="scope.row.refno2 === 'FF'">付费</span>
          <span v-else-if="scope.row.refno2 === 'JSCCF'">计算仓储费</span>
          <span v-else-if="scope.row.refno2 === 'SE'">下单配船</span>
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="系统编号"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="审核状态"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="审核人"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="审核日期"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="提交人"
      ></el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        label="提交日期"
      ></el-table-column> -->
      <el-table-column label="操作" width="80">
        <template slot-scope="scope">
          <span style="cursor: pointer" @click="approval(scope.row)"
            >查看详情</span
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="消息提示"
      :visible.sync="dialogVisible"
      width="80%"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <span
        v-html="text"
        class="text"
        style="max-width: 100%; height: auto"
      ></span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false"
          >关 闭</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { selectAuditItems, selectAuditItemsTotal } from "@/api/morePage/stock";
import { listCorps } from "@/api/basicdata/corps";
import { addSet, select, resetModule } from "@/api/system/set";
import Cookies from "js-cookie";
import draggable from "vuedraggable";

export default {
  name: "index",
  components: { draggable },
  data() {
    return {
      selectAuditList: [],
      dialogVisible: false,
      total: 0,
      text: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        refno1: null,
        rbillId: null,
        billNo: null,
        sendUserId: null,
        auditUserId: null,
        actId: null,
        sendTime: null,
        billTime: null,
        auditOpTime: null,
      },
      show: false,
      showSearch: true,
      factIdOptions: [],
      showSetting: false,
      drag: false,
      setRowList: [],
      getRowList: [],
      tableDate: [
        {
          surface: "1",
          label: "refno1",
          name: "委托方",
          checked: 0,
          width: 100,
        },
        {
          surface: "2",
          label: Cookies.get("sysType") == 3 ? "sendTime" : "billTime",
          name: "日期",
          checked: 0,
          width: 120,
        },
        {
          surface: "3",
          label: "refno3",
          name: "提单号",
          checked: 0,
          width: 120,
        },
        {
          surface: "4",
          label: "refno2",
          name: "业务类型",
          checked: 0,
          width: 120,
        },
        {
          surface: "5",
          label: "fAmount",
          name: "出入库费",
          checked: 0,
          width: 120,
        },
        {
          surface: "6",
          label: "billNo",
          name: "系统编号",
          checked: 0,
          width: 120,
        },
        {
          surface: "7",
          label: "audit",
          name: "审核状态",
          checked: 0,
          width: 120,
        },
        {
          surface: "8",
          label: "auditUserName",
          name: "审核人",
          checked: 0,
          width: 120,
        },
        {
          surface: "9",
          label: "auditOpTime",
          name: "审核日期",
          checked: 0,
          width: 150,
        },
        {
          surface: "10",
          label: "isAccountPeriod",
          name: "提交人",
          checked: 0,
          width: 150,
        },
        {
          surface: "11",
          label: "sendTime",
          name: "提交日期",
          checked: 0,
          width: 150,
        },
      ],
      allCheck: false,
    };
  },
  created() {
    this.setRowList = this.tableDate;
    this.getRowList = this.tableDate;
    listCorps().then((response) => {
      this.factIdOptions = response.rows;
    });
    this.getList();
    this.getRow();
    console.log(Cookies.get("sysType"))
  },
  methods: {
    //列设置全选
    allChecked() {
      if (this.allCheck == true) {
        this.setRowList.map((e) => {
          return (e.checked = 0);
        });
      } else {
        this.setRowList.map((e) => {
          return (e.checked = 1);
        });
      }
    },
    //查询列数据
    getRow() {
      let that = this;
      this.data = {
        tableName: "我的审核",
        userId: Cookies.get("userName"),
      };
      select(this.data).then((res) => {
        if (res.data.length != 0) {
          this.getRowList = res.data.filter((e) => e.checked == 0);
          this.setRowList = res.data;
          this.setRowList = this.setRowList.reduce((res, item) => {
            res.push({
              surface: item.surface,
              label: item.label,
              name: item.name,
              checked: item.checked,
              width: item.width,
              fixed: item.fixed,
            });
            return res;
          }, []);
        }
      });
    },
    //重置列表
    delRow() {
      this.data = {
        tableName: "我的审核",
        userId: Cookies.get("userName"),
      };
      resetModule(this.data).then((res) => {
        if (res.code == 200) {
          this.showSetting = false;
          this.setRowList = this.tableDate;
          this.getRowList = this.tableDate;
        }
      });
    },
    //保存列设置
    save() {
      this.showSetting = false;
      this.data = {
        tableName: "我的审核",
        userId: Cookies.get("userName"),
        sysTableSetList: this.setRowList,
      };
      addSet(this.data).then((res) => {
        if (res.code == 200) {
          this.showSetting = false;
          this.getRowList = this.setRowList.filter((e) => e.checked == 0);
        }
      });
    },
    //开始拖拽事件
    onStart() {
      this.drag = true;
    },
    //拖拽结束事件
    onEnd() {
      this.drag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.queryParams);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getList() {
      this.loading = true;
      selectAuditItems(this.queryParams).then((response) => {
        response.rows.map((e) => {
          if (e.sendTime) {
            e.sendTime = e.sendTime.slice(0, 10);
          }
          if (e.refno2) {
            switch (e.refno2) {
              case "SJRK": {
                e.refno2 = "入库";
                break;
              }
              case "SJCK": {
                e.refno2 = "出库";
                break;
              }
              case "HQZY": {
                e.refno2 = "货转";
                break;
              }
              case "CKDB": {
                e.refno2 = "调拨";
                break;
              }
              case "HWTG": {
                e.refno2 = "货物通关";
                break;
              }
              case "JSCCF": {
                e.refno2 = "计算仓储费";
                break;
              }
              case "CCF": {
                e.refno2 = "仓储费";
                break;
              }
              case "ZYF": {
                e.refno2 = "作业费";
                break;
              }
              case "SF": {
                e.refno2 = "收费";
                break;
              }
              case "DZ": {
                e.refno2 = "对账";
                break;
              }
              case "FF": {
                e.refno2 = "付费";
                break;
              }
              case "SE": {
                e.refno2 = "下单配船";
                break;
              }
              case "ApplyFP": {
                e.refno2 = "凯和开票申请";
                break;
              }
              case "KHDZ": {
                e.refno2 = "凯和对账";
                break;
              }
              case "KHSF": {
                e.refno2 = "凯和收费";
                break;
              }
              case "KHFF": {
                e.refno2 = "凯和付费";
                break;
              }
              case "KHDD": {
                e.refno2 = "凯和订单";
                break;
              }
              case "SJRKITEM": {
                e.refno2 = "入库明细审批";
                break;
              }
              case "SJCKITEM": {
                e.refno2 = "出库明细审批";
                break;
              }
              case "XGDD": {
                e.refno2 = "申请变更";
                break;
              }
              case "XGTJ": {
                e.refno2 = "提交变更";
                break;
              }
              case "DDSC": {
                e.refno2 = "申请删单";
                break;
              }
              default: {
                return this.$message.error("未知错误，无状态");
              }
            }
          }
        });
        this.selectAuditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    approval(row) {
      switch (row.refno4) {
        case "SJRK": {
          this.$router.push({
            path: "/business/inStock",
            query: { id: row.billId},
          });
          break;
        }
        case "SJCK": {
          this.$router.push({
            path: "/business/outStock",
            query: { id: row.billId},
          });
          break;
        }
        case "HQZY": {
          this.$router.push({
            path: "/business/goodsTransfer",
            query: { id: row.billId},
          });
          break;
        }
        case "CKDB": {
          this.$router.push({
            path: "/business/stockTransfer",
            query: { id: row.billId},
          });
          break;
        }
        case "HWTG": {
          this.$router.push({
            path: "/business/cargoClearance",
            query: { id: row.billId},
          });
          break;
        }
        case "JSCCF": {
          this.$router.push({
            path: "/business/agreement",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "CCF": {
          this.$router.push({
            path: "/agreement/agreementStorage",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "ZYF": {
          this.$router.push({
            path: "/agreement/agreementTask",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "SF": {
          this.$router.push({
            path: "/finance/charge",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "DZ": {
          this.$router.push({
            path: "/finance/contrast",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "FF": {
          this.$router.push({
            path: "/finance/payment",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "KHDD": {
          this.$router.push({
            path: "/domesticTrade/orderInformation",
            query: { list: JSON.stringify(row) },
          });
          break;
        }
        case "ApplyFP": {
          this.$router.push({
            path: "/finance/chargeInvoice",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "KHDZ": {
          this.$router.push({
            path: "/finance/contrast",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "KHSF": {
          this.$router.push({
            path: "/finance/charge",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "KHFF": {
          this.$router.push({
            path: "/finance/payment",
            query: { data: JSON.stringify(row) },
          });
          break;
        }
        case "SJRKITEM": {
          this.$router.push({
            path: "/business/inStock",
            query: { id: row.billId},
          });
          break;
        }
        case "SJCKITEM": {
          this.$router.push({
            path: "/business/outStock",
            query: { id: row.billId},
          });
          break;
        }
        case "XGDD": {
          this.$router.push({
            path: "/domesticTrade/orderInformation",
            query: {testing:JSON.stringify(row)},
          });
          break;
        }
        case "XGTJ": {
          this.$router.push({
            path: "/domesticTrade/orderInformation",
            query: {testing:JSON.stringify(row)},
          });
          break;
        }
        case "DDSC": {
          this.$router.push({
            path: "/domesticTrade/orderInformation",
            query: {testing:JSON.stringify(row)},
          });
          break;
        }
        default: {
          console.log(row);
          return this.$message.error("未知错误，无状态");
        }
      }
    },
    querytoDo(res) {
      this.dialogVisible = true;
      this.text = res;
    },
    handleClose(done) {
      this.dialogVisible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.tabSetting {
  display: flex;
  justify-content: flex-end;
}
.listStyle {
  display: flex;
  border-top: 1px solid #dcdfe6;
  border-left: 1px solid #dcdfe6;
  border-right: 1px solid #dcdfe6;
}
.listStyle:last-child {
  border-bottom: 1px solid #dcdfe6;
}
.progress {
  display: flex;
  align-items: center;
  padding: 2px;
  background-color: rgba(0, 0, 0, 0.05);
  height: 100%;
}
</style>
