<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-width="100px" label="系统编号" prop="fBillno">
            <el-input
              v-model="queryParams.fBillno"
              placeholder="请输入系统编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="max-width: 230px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="100px" label="制单日期" prop="createTime">
            <el-date-picker
              type="daterange"
              value-format="yyyy-MM-dd"
              clearable
              unlink-panels
              style="width: 230px"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @keyup.enter.native="handleQuery"
              v-model="queryParams.createTimeList"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="100px" label="制单人" prop="">
            <el-select
              v-model="queryParams.createBy"
              placeholder="请输入制单人"
              clearable
              size="small"
              filterable
            >
              <el-option
                v-for="(dict, index) in userOptions"
                :key="index"
                :label="dict.nickName"
                :value="dict.userName"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label-width="100px" label="原业务类型" prop="fBilltype">
            <el-select
              v-model="queryParams.fBilltype"
              placeholder="请选择原业务类型"
              clearable
              size="small"
            >
              <el-option
                v-for="(dict, index) in billTypeList"
                :key="index.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-collapse-transition>
        <div v-show="show">
          <el-row>
            <el-col :span="6">
              <el-form-item label="原客户名称" prop="fCorpid">
                <el-select
                  v-model="queryParams.fCorpid"
                  placeholder="请输入原客户名称"
                  clearable
                  size="small"
                  filterable
                >
                  <el-option
                    v-for="(dict, index) in fMblnoOptions"
                    :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="">-->
<!--                <el-input-->
<!--                  placeholder="请输入原系统编号"-->
<!--                  clearable-->
<!--                  size="small"-->
<!--                  @keyup.enter.native="handleQuery"-->
<!--                  style="max-width: 199px"-->
<!--                  v-model="queryParams.fBillno"-->
<!--                />-->
<!--              </el-form-item>-->
<!--            </el-col>-->
            <el-col :span="6">
              <el-form-item label-width="100px" label="原提单号" prop="">
                <el-input
                  placeholder="请输入原提单号"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
                  style="max-width: 199px"
                  v-model="queryParams.fMblno"
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label-width="100px" label="原业务日期" prop="">
                <el-date-picker
                  type="daterange"
                  value-format="yyyy-MM-dd"
                  clearable
                  style="width: 199px"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  unlink-panels
                  @keyup.enter.native="handleQuery"
                  v-model="queryParams.bstimeList"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-collapse-transition>
    </el-form>
    <el-row :gutter="10" style="padding-bottom: 10px">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['fleet:plans:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['warehouseBusiness:inStock:edit']"-->
<!--        >修改-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['warehouseBusiness:inStock:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <div class="tabSetting">
        <div style="margin-right: 20px">
          <el-button
            type="cyan"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            v-hasPermi="['warehouse:modify:list']"
          >搜索</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-button
            icon="el-icon-setting"
            size="mini"
            circle
            @click="showSetting = !showSetting"
          ></el-button>
        </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="costModifyList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
      ref="table"
      :height="tableHeight"
    >
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column
        v-for="(item, index) in getRowList"
        :key="index"
        :label="item.name"
        :width="item.width"
        :prop="item.label"
        align="center"
        :show-overflow-tooltip="true"
        sortable
        :fixed="item.fixed"
      >
        <template slot-scope="scope">
          <span v-if="item.label == 'fCorpid'">
            {{ scope.row.fCorpid | corpNameFormat(fMblnoOptions) }}
          </span>
          <span v-else-if="item.label == 'fGoodsid'">
            {{ scope.row.fGoodsid | goodsFormat(goodsOptions) }}
          </span>
          <span v-else-if="item.label == 'fBilltype'">
            {{ scope.row.fBilltype | billTypeFormat(billTypeList) }}
          </span>
          <span v-else-if="item.label == 'fWarehouseid'">
            {{ scope.row.fWarehouseid | warehouseFormat(warehouseOptions) }}
          </span>
          <span v-else>
            {{ scope.row[item.label] }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="150px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['warehouse:modify:query']"
          >查看</el-button
          >
          <!-- <el-button
            v-if="scope.row.billStatus != 6"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fleet:plans:edit']"
            >修改</el-button
          > -->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['fleet:plans:remove']"-->
<!--          >删除</el-button-->
<!--          >-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <add-or-update
      ref="addOrUpdateRef"
      :title="title"
      :isDisabled="formDisabled"
      :addOrUpdateVisible="addOrUpdateVisible"
      @changeShow="showAddOrUpdate"
      :form="form"
      :feesList="feeList"
      :formfeesList="formfeesList"
      :goodsOptions="goodsOptions"
      :warehouseOptions="warehouseOptions"
      :fDc="fDc"
      :warehouseId="warehouseId"
      :disabledtwo="disabledtwo"
      :approVal="approVal"
    ></add-or-update>
  </div>
</template>

<script>
import { listCorps } from "@/api/basicdata/corps";
import { addSet, resetModule, select } from '@/api/system/set';
import Cookies from 'js-cookie';
import draggable from "vuedraggable";
import AddOrUpdate from "./AddOrUpdate";
import {listWarehouseModify,passWarehouse,detailWarehouseModify} from "@/api/warehouseBusiness/wareHouseModify";
import {listGoods} from "@/api/basicdata/goods";
import {
  listWarehouse,
  treeselect,
  listWarehousesss,
} from "@/api/basicdata/warehouse";
import { listUser } from "@/api/system/user";

export default {
  name: "costModify",
  components: {
    draggable,
    AddOrUpdate
  },
  data() {
    return {
      // 表格高度
      tableHeight: 0,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      showSearch: true,
      // 搜索展开/隐藏
      show: false,
      billTypeList: [],
      fMblnoOptions: [],
      // 遮罩层
      loading: false,
      // 主表格数据
      costModifyList: [],
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 非单个禁用
      single: true,
      // 自定义列弹窗显示
      showSetting: false,
      //自定义列宽
      allCheck: false,
      setRowList: [],
      getRowList: [],
      drag: false,
      tableDate: [
        {
          surface: "1",
          label: "fBillno",
          name: "系统编号",
          checked: 0,
          width: 100,
        },
        {
          surface: "2",
          label: "createTime",
          name: "制单日期",
          checked: 0,
          width: 100,
        },
        {
          surface: "3",
          label: "createBy",
          name: "制单人",
          checked: 0,
          width: 100,
        },
        {
          surface: "4",
          label: "fBilltype",
          name: "原业务类型",
          checked: 0,
          width: 150,
        },
        {
          surface: "5",
          label: "fCorpid",
          name: "原客户",
          checked: 0,
          width: 100,
        },
        // {
        //   surface: "6",
        //   label: "fBillno",
        //   name: "原系统编号",
        //   checked: 0,
        //   width: 150,
        // },
        {
          surface: "7",
          label: "fMblno",
          name: "原提单号",
          checked: 0,
          width: 100,
        },
        {
          surface: "8",
          label: "fBstime",
          name: "原业务日期",
          checked: 0,
          width: 150,
        },
        {
          surface: "9",
          label: "fWarehouseid",
          name: "原仓库",
          checked: 0,
          width: 100,
        },
        {
          surface: "10",
          label: "fGoodsid",
          name: "原品名",
          checked: 0,
          width: 100,
        },
        // {
        //   surface: "11",
        //   label: "mdLoadDate",
        //   name: "最终审核人",
        //   checked: 0,
        //   width: 150,
        // },
        // {
        //   surface: "12",
        //   label: "mdLoadDate",
        //   name: "最终审核日期",
        //   checked: 0,
        //   width: 180,
        // },
        // {
        //   surface: "13",
        //   label: "mdLoadDate",
        //   name: "审批意见",
        //   checked: 0,
        //   width: 100,
        // },
      ],
      // 子组件弹窗title
      title: '',
      // 子组件弹窗显示开关
      addOrUpdateVisible: false,
      // 子组件表格是否禁用
      formDisabled: true,
      form: {},
      feeList: [],
      formfeesList: [],
      goodsOptions: [],
      warehouseOptions: [],
      fDc: '',
      warehouseId: null,
      disabledtwo: false,
      approVal: false,
      userOptions: [],
    }
  },
  created() {
    this.setRowList = this.tableDate;
    this.getRowList = this.tableDate;
    this.getList();
    this.getDicts("data_billtype_type").then((response) => {
      this.billTypeList = response.data;
    });
    listCorps().then((response) => {
      this.fMblnoOptions = response.rows;
    });
    listGoods({fStatus: 0, delFlag: 0}).then((response) => {
      this.goodsOptions = response.rows;
    });
    listWarehousesss({fStatus: 0, delFlag: 0}).then((response) => {
      this.warehouseOptions = response.rows;
    });
    listUser().then((response) => {
      this.userOptions = response.rows;
    });
  },
  activated() {
    if (this.$route.query.data) {
      this.reset();
      this.warehouseId = this.$route.query.data.form.fId
      this.fDc = this.$route.query.data.fDc
      this.disabledtwo = false
      passWarehouse(this.fDc, this.warehouseId).then(res => {
        this.form = res.data.data
        this.feeList = res.data.data.tWarehousebillsfees
      })
      this.$nextTick(() => {
        this.openDialog()
        this.$refs.addOrUpdateRef.init()
      })
    }
    if (this.$route.query.id) {
      let id = this.$route.query.id;
      console.log(id)
      this.reset();
      detailWarehouseModify(id).then(res => {
        this.form = res.data
        this.feeList = res.data.tWarehousebillsfees
        this.formfeesList = res.data.warehousebillsfeesModifyList
      })
      this.disabledtwo = true
      this.title = "查看订单";
      this.addOrUpdateVisible = true;
      this.form.id = id;
      this.formDisabled = true
      this.approVal = true
      this.$refs.addOrUpdateRef.init()
      this.$router.push({query: {}});
    }
  },
  mounted() {
    this.$nextTick(() => {
      // 监听浏览器高度变化，改变表格高度
      window.onresize = () => {
        this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 70
      }
    })
  },
  filters: {
    corpNameFormat(row, fMblnoOptions) {
      let name;
      fMblnoOptions.map((e) => {
        if (row == e.fId) {
          name = e.fName;
        }
      });
      return name;
    },
    goodsFormat(row, goodsOptions) {
      let name;
      goodsOptions.map((e) => {
        if (row == e.fId) {
          name = e.fName;
        }
      });
      return name;
    },
    billTypeFormat(row, billTypeList) {
      let type;
      billTypeList.map((e) => {
        if (row == e.dictValue) {
          type = e.dictLabel;
        }
      });
      return type;
    },
    warehouseFormat(row, warehouseOptions) {
      let name;
      warehouseOptions.map((e) => {
        if (row == e.fId) {
          name = e.fName;
        }
      });
      return name;
    },
  },
  methods: {
    openDialog(){
      this.formDisabled = true;
      this.title = "查看订单";
      // this.$nextTick(() => {
      //   this.$refs.addOrUpdateRef.init();
      // })
      this.addOrUpdateVisible = true;
    },
    getList() {
      listWarehouseModify(this.queryParams).then(res => {
        this.costModifyList = res.rows
        this.total = res.total
        this.loading = false;
        console.log(this.costModifyList)
      })
      // 根据浏览器高度设置初始高度
      setTimeout(() => {
        this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 70
      }, 300)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      if (this.queryParams.createTimeList) {
        this.queryParams.createTimeList[0] = this.queryParams.createTimeList[0]+ " " + "00:00:00"
        this.queryParams.createTimeList[1] = this.queryParams.createTimeList[1]+ " " + "23:59:59"
      }
      if (this.queryParams.bstimeList) {
        this.queryParams.bstimeList[0] = this.queryParams.bstimeList[0]+ " " + "00:00:00"
        this.queryParams.bstimeList[1] = this.queryParams.bstimeList[1]+ " " + "23:59:59"
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billNo: null,
        actId: null,
        corpId: null,
        transUserName: null,
        planUserName: null,
        billType: null,
        transType: null,
        transProp: null,
        ifContracted: null,
        contractNo: null,
        refNo: null,
        accstlType: null,
        accdays: null,
        goodsId: null,
        goodsPrice: null,
        goodsLossType: null,
        goodsLossStd: null,
        mblno: null,
        ysl: null,
        voy: null,
        pol: null,
        pod: null,
        qtyPlan: null,
        qtyDisPatch: null,
        weightPlan: null,
        cntrDesc: null,
        carNoList: null,
        loadAddr: null,
        loadAttn: null,
        loadAttntel: null,
        loadDate: null,
        mdLoadAddr: null,
        mdLoadAttn: null,
        mdLoadAttnTel: null,
        mdLoadDate: null,
        unLoadAddr: null,
        unLoadAttn: null,
        unLoadAttnTel: null,
        unLoadDate: null,
        routeId: null,
        routeDesc: null,
        ifWaybill: null,
        ifNeedinvDr: null,
        postDate: null,
        billStatus: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remarks: null,
      };
      this.disabled = false;
      this.resetForm("form");
      this.relevantAttachments = [];
      this.allfMblnoOptions = [];
      this.feesList = [];
      this.formfeesList = [];
    },
    /** 查看按钮操作 */
    handleUpdate(row) {
      let id = row.fId;
      this.reset();
      detailWarehouseModify(id).then(res => {
        this.form = res.data
        this.feeList = res.data.tWarehousebillsfees
        this.formfeesList = res.data.warehousebillsfeesModifyList
        this.$nextTick(() => {
          this.$refs.addOrUpdateRef.init()
        })
      })
      this.disabledtwo = true
      this.title = "查看订单";
      this.addOrUpdateVisible = true;
      this.form.id = id;
      this.formDisabled = true
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单主编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delFtmsorderbills(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    // 新增操作
    handleAdd() {
      this.reset();
      this.title = "新增订单";
      this.addOrUpdateVisible = true;
      this.formDisabled = false;
    },
    // 监听 子组件弹窗关闭后触发，有子组件调用
    showAddOrUpdate(data) {
      if (data === "false") {
        this.addOrUpdateVisible = false;
        this.$router.push({
          path: "/business/costModify",
          query: {},
        });
        this.getList()
      } else {
        this.addOrUpdateVisible = true;
      }
    },
    // 导出
    handleExport() {},

    //列设置全选
    allChecked() {
      if (this.allCheck == true) {
        this.setRowList.map((e) => {
          return (e.checked = 0);
        });
      } else {
        this.setRowList.map((e) => {
          return (e.checked = 1);
        });
      }
    },
    //开始拖拽事件
    onStart() {
      this.drag = true;
    },
    //拖拽结束事件
    onEnd() {
      this.drag = false;
    },
    //重置列表
    delRow() {
      this.data = {
        tableName: "费用变更",
        userId: Cookies.get("userName"),
      };
      resetModule(this.data).then((res) => {
        if (res.code == 200) {
          this.showSetting = false;
          this.setRowList = this.tableDate;
          console.log(this.setRowList)
          this.getRowList = this.tableDate;
        }
      });
    },
    //保存列设置
    save() {
      this.showSetting = false;
      this.data = {
        tableName: "船舶信息",
        userId: Cookies.get("userName"),
        sysTableSetList: this.setRowList,
      };
      addSet(this.data).then((res) => {
        this.getRowList = this.setRowList.filter((e) => e.checked == 0);
      });
    },
  }
}
</script>

<style scoped>

</style>
