<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-row>
          <el-form-item label="箱号" prop="fNo">
            <el-input style="width: 200px" v-model="queryParams.fNo" placeholder="请选择客户类别" clearable size="small">

            </el-input>
          </el-form-item>

          <el-form-item label="箱类型" prop="fTypeid">
            <el-select
              v-model="queryParams.fTypeid"
              style="width: 200px"
              placeholder="请输入名称"
              clearable
              size="small"
              filterable
              :remote-method="boxTypeMethod"
            >
              <el-option
                v-for="item in typeOptions"
                :key="item.fId"
                :label="item.fNo"
                :value="item.fId"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="箱状态" prop="fCntrstatus">
            <el-select
              v-model="queryParams.fCntrstatus"
              style="width: 200px"
              placeholder="请输入或选择"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in cntrstatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="箱主" prop="fOwner">
            <el-select
              v-model="queryParams.fOwner"
              style="width: 200px"
              placeholder="请输入或选择"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            >
              <el-option
                v-for="dict in ownerOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
      </el-row>
      <div v-show="queryParamsHidden">
        <el-row>

            <el-form-item label="录入人" prop="createBy">
              <el-input
                v-model="queryParams.createBy"
                style="width: 200px"
                placeholder="请输入电话"
                clearable
                size="small"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>

          <el-form-item label="备注" prop="remark">
            <el-input
              style="width: 200px"
              type="textarea"
              clearable
              size="small"
              v-model="queryParams.remark"
            />
          </el-form-item>

          <el-form-item label="录入区间" prop="cLoadDate">
            <el-date-picker
              v-model="queryParams.cLoadDate"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 250px"
            >
            </el-date-picker>
          </el-form-item>
        </el-row>
      </div>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >取消</el-button>
      </el-col>
      <el-col  style="position: absolute;left:75%" :span="5" >
        <el-button  size="small" @click="queryParamsHidden ? (queryParamsHidden = false) : (queryParamsHidden = true)">{{ queryParamsHidden ? '隐藏' : '更多' }}</el-button>
        <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-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="corpsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection"  align="center" />
      <el-table-column type="index"  label="行号" align="center" fixed/>
      <el-table-column label="箱号" sortable fixed align="center" prop="fNo" fixed width="100px"/>
      <el-table-column label="地点" align="center"  sortable  prop="fUpdateaddress" fixed/>
      <el-table-column label="箱类型" align="center" sortable  prop="typeidName" fixed width="100px"/>
      <el-table-column label="箱状态" align="center" sortable  prop="cntrstatusName" fixed width="100px"/>
      <el-table-column label="空重" align="center" sortable  prop="updateEFName" fixed/>
      <el-table-column :show-overflow-tooltip="true" label="箱主" align="center" prop="ownerName" />
      <el-table-column :show-overflow-tooltip="true" label="箱来源" align="center"  prop="sourceName" />
      <el-table-column :show-overflow-tooltip="true" label="租赁方式" align="center" prop="rentName" />
      <el-table-column :show-overflow-tooltip="true" label="备注" align="center" prop="remark" />
      <el-table-column label="录入人" align="center" prop="createBy" />
      <el-table-column label="录入时间" align="center" prop="createTime" width="100px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最新修改人" align="center" prop="updateBy" width="100px"/>
      <el-table-column label="最新修改时间" align="center" prop="updateTime" width="120px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="120px" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basicdata:corps:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basicdata:corps: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"
    />

    <!-- 添加或修改客户详情对话框 -->
    <el-dialog
      v-dialogDrag
      :fullscreen="dialogFull"
      :title="title"
      :visible.sync="open"
      close-on-click-modal="false"
      width="60%"
      :close-on-click-modal="false"
      append-to-body>
      <template slot="title">
        <div class="avue-crud__dialog__header">
            <span class="el-dialog__title">
            <span style="display:inline-block;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
            </span>
          <div class="avue-crud__dialog__menu enlarge" @click="full">
            <i  style="cursor: pointer;display: block;width:12px;height:12px;border:1px solid #909399;border-top:3px solid #909399;margin-top: -3px;"></i>
          </div>
        </div>
      </template>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="24">
            <el-form-item label="箱类型" prop="fTypeid">
              <el-select
                style="width: 90%"
                v-model="form.fTypeid"
                placeholder="请选择箱类型"
                clearable
                :disabled="doNot"
                filterable
                :remote-method="boxTypeMethod"
              >
                <el-option
                  v-for="item in typeOptions"
                  :key="item.fId"
                  :label="item.fNo"
                  :value="item.fId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最新地点" prop="fUpdateaddress">
              <el-input v-model="form.fUpdateaddress" style="width: 80%" placeholder="请输入最新地点" :disabled="doNot"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="箱主" prop="fOwner">
              <el-select
                v-model="form.fOwner"
                style="width: 80%"
                :disabled="doNot"
                placeholder="请选择箱主"
              >
                <el-option
                  v-for="dict in ownerOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="箱号" prop="fNo">
              <el-input v-model="form.fNo" style="width: 80%" placeholder="请输入箱号" :disabled="doNot" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="箱来源" prop="fSource">
              <el-select v-model="form.fSource" style="width: 78%" placeholder="请选择箱来源" :disabled="doNot">
                <el-option
                  v-for="dict in sourceOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="租赁方式" prop="fRent">
              <el-select v-model="form.fRent" style="width: 80%" placeholder="请选择租赁方式" :disabled="doNot">
                <el-option
                  v-for="dict in fRentOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="空重" prop="fUpdateef">
              <el-select v-model="form.fUpdateef" style="width: 80%" placeholder="请请选择空重" :disabled="doNot">
                <el-option
                  v-for="dict in updateEFOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="箱状态" prop="fCntrstatus">
              <el-select v-model="form.fCntrstatus" style="width: 80%" placeholder="请选择箱状态" :disabled="doNot">
                <el-option
                  v-for="dict in cntrstatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" style="width: 90%" placeholder="请输入备注" :disabled="doNot"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" round @click="doNot = false">修 改</el-button>
        <el-button type="success" round @click="submitForm" :disabled="doNot">保 存</el-button>
        <el-button @click="cancel" round>关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCorps, getInforma, delCorps, addboxinformation, getType,updateCorps, changeCorpsStatus,exportCorps } from "@/api/kaihe/containerNews/boxInformation";
  import {queryUserVal} from "@/api/system/user";
  import { isArray } from '@/utils/validate'
  import Vue from 'vue'
  import { getportinformation } from '@/api/kaihe/basicdata/portinformation'
  Vue.directive('dialogDrag', {
    bind(el, binding, vnode, oldVnode) {
      const dialogHeaderEl = el.querySelector('.el-dialog__header')
      const dragDom = el.querySelector('.el-dialog')
      const enlarge = el.querySelector('.enlarge')
      dialogHeaderEl.style.cursor = 'move'

      // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
      const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
      if(enlarge){
        enlarge.onclick = (e) => {
          dragDom.style.top = '0px'
          dragDom.style.left = '0px'
        }
      }
      dialogHeaderEl.onmousedown = (e) => {
        // 鼠标按下，计算当前元素距离可视区的距离
        const disX = e.clientX - dialogHeaderEl.offsetLeft
        const disY = e.clientY - dialogHeaderEl.offsetTop

        // 获取到的值带px 正则匹配替换
        let styL, styT

        // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
        if (sty.left.includes('%')) {
          styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
          styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
        } else {
          styL = +sty.left.replace(/\px/g, '')
          styT = +sty.top.replace(/\px/g, '')
        }

        document.onmousemove = function(e) {
          // 通过事件委托，计算移动的距离
          const l = e.clientX - disX
          const t = e.clientY - disY

          // 移动当前元素

          if ((t + styT) >= 0){
            dragDom.style.top = `${t + styT}px`
          }
          dragDom.style.left = `${l + styL}px`
          // 将此时的位置传出去
          // binding.value({x:e.pageX,y:e.pageY})
        }

        document.onmouseup = function(e) {
          document.onmousemove = null
          document.onmouseup = null
        }
      }
    }
  })

  export default {
    name: "boxInformation",
    components: {
    },
    data() {
      return {
        //默认显示第一行
        queryParamsHidden:false,
        //字典表箱种类
        containerOptions:[],
        //集装箱主字典表
        ownerOptions:[],
        //箱来源字典表
        sourceOptions:[],
        //租赁方式字典表
        fRentOptions:[],
        //空重字典表
        updateEFOptions:[],
        //箱状态字典表
        cntrstatusOptions:[],
        //模糊查询箱类型
        typeOptions:[],
        //查看置灰
        doNot:true,

        //全屏放大
        dialogFull:false,
        contactList:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户详情表格数据
        corpsList: [],
        // 弹出层标题
        title: "",
        // 状态数据字典
        statusOptions: [],
        // 是否显示弹出层
        open: false,
        // 客户类别字典
        fTypeidOptions: [],
        // 结算表票结、月结字典
        fStltypeidOptions: [],
        // 删除状态字典
        delFlagOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          fNo:null,
          fTypeid:null,
          fCntrstatus:null,
          fOwner:null,
          createBy:null,
          cLoadDate:null,
          remark:null,
        },
        // 表单参数
        form: {
          fTypeid:null,
          fUpdateaddress:null,
          fOwner:null,
          fNo:null,
          fSource:null,
          fRent:null,
          fUpdateef:null,
          fCntrstatus:null,
          remark:null
        },
        // 表单校验
        rules: {
           fTypeid: [
            { required: true, message: "", trigger: "blur" }
          ],
          fNo: [
            { required: true, message: "箱号不能为空", trigger: "blur" }
          ],
          fUpdateaddress: [
            { required: true, message: "最新地点不能为空", trigger: "blur" }
          ],
          fOwner:[
            { required: true, message: "箱主不能为空", trigger: "blur" }
          ],
          fUpdateef: [
            { required: true, message: "空重不能为空", trigger: "blur" }
          ],
          fCntrstatus: [
            { required: true, message: "箱状态不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("f_type").then(response => {
        this.containerOptions = response.data;
      });
      this.getDicts("f_owner").then(response => {
        this.ownerOptions = response.data;
      });
      this.getDicts("f_source").then(response => {
        this.sourceOptions = response.data;
      });
      this.getDicts("f_rent").then(response => {
        this.fRentOptions = response.data;
      });
      this.getDicts("f_updateEF").then(response => {
        this.updateEFOptions = response.data;
      });
      this.getDicts("f_cntrstatus").then(response => {
        this.cntrstatusOptions = response.data;
      });
      this.boxTypeMethod()
    },
    methods: {
      //模糊查询箱类型
      boxTypeMethod(){
        let queryParams = { pageNum: 1,};
        getType(queryParams).then(response =>{
          console.log(response)
          this.typeOptions = response.rows
        })
      },
      full(){
        this.dialogFull = !this.dialogFull
      },
      /** 查询客户详情列表 */
      getList() {
        this.loading = true;
        listCorps(this.queryParams).then(response => {
          this.corpsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 客户类别字典翻译
      fTypeidFormat(row, column) {
        return this.selectDictLabel(this.fTypeidOptions, row.fTypeid);
      },
      // 结算表票结、月结字典翻译
      fStltypeidFormat(row, column) {
        return this.selectDictLabel(this.fStltypeidOptions, row.fStltypeid);
      },
      // 删除状态字典翻译
      delFlagFormat(row, column) {
        return this.selectDictLabel(this.delFlagOptions, row.delFlag);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          fTypeid:null,
          fUpdateaddress:null,
          fOwner:null,
          fNo:null,
          fSource:null,
          fRent:null,
          fUpdateef:null,
          fCntrstatus:null,
          remark:null
        };
        this.resetForm("form");
      },
      // 从表重置
      contList() {
        this.contactList = []
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.fStatus === "0" ? "启用" : "停用";
        this.$confirm('确认要"' + text + '""' + row.fName + '"吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeCorpsStatus(row.fId, row.fStatus);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.fStatus = row.fStatus === "0" ? "1" : "0";
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.fId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.doNot = false
        this.reset();
        this.open = true;
        this.title = "添加客户详情";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.doNot = true
        this.reset();
        const fId = row.fId || this.ids
        getInforma(fId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户详情";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.fId != null) {
              addboxinformation(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addboxinformation(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const fIds = row.fId || this.ids;
        this.$confirm('是否确认删除客户详情编号为"' + fIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCorps(fIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有客户详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCorps(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style lang="scss">
.avue-crud__dialog__header {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}
.el-dialog__title {
  color: rgba(0,0,0,.85);
  font-weight: 500;
  word-wrap: break-word;
}
.avue-crud__dialog__menu {
  padding-right: 20px;
  float: left;
}
.avue-crud__dialog__menu i {
  color: #909399;
  font-size: 15px;
}
.el-icon-full-screen{
  cursor: pointer;
}
.el-icon-full-screen:before {
  content: "\e719";
}

</style>
