<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <button-bar ref="btnbar" view-title="调拨列表" :btndisabled="btndisabled" @btnhandle="btnHandle" />
      <div class="main-content">
        <div class="searchcon">
          <el-button size="small" class="searchbtn" @click="clicksearchShow">
            {{ searchxianshitit }}
          </el-button>
          <div v-show="isSearchShow" class="search">
            <el-form :inline="true" class="tab-header">

              <el-form-item label="申请部门">
                <el-input v-model="queryParams.params.deptName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="申请人">
                <el-input v-model="queryParams.params.createByName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="单据编号">
                <el-input v-model="queryParams.params.billNo" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="单据日期">
                <el-date-picker v-model="queryParams.params.createStartTime" type="date" placeholder="选择日期"
                  format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
                </el-date-picker>
                至
                <el-date-picker v-model="queryParams.params.createDateStart" type="date" placeholder="选择日期"
                  format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="办结日期">
                <el-date-picker v-model="queryParams.params.finishStartTime" type="date" placeholder="选择日期"
                  format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
                </el-date-picker>
                至
                <el-date-picker v-model="queryParams.params.finishEndTime" type="date" placeholder="选择日期"
                  format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 200px;">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="调出分公司">
                <el-input v-model="queryParams.params.outOrgName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="调出站">
                <el-input v-model="queryParams.params.outPlatName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="调入分公司">
                <el-input v-model="queryParams.params.inOrgName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="调入站">
                <el-input v-model="queryParams.params.inPlatName" placeholder="" clearable />
              </el-form-item>
              <el-form-item label="经办人">
                <el-input v-model="queryParams.params.confirmName" placeholder="" clearable />
              </el-form-item>
            </el-form>
            <div class="btn" style="text-align: center;">
              <el-button type="primary" size="small" icon="el-icon-search" @click="dosearch">查询</el-button>
              <el-button type="primary" size="small" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </div>
          </div>
        </div>
        <!-- Start 项目列表头部 -->
        <div class="listtop">
          <div class="tit">调拨列表</div>
        </div>
        <!-- End 项目列表头部 -->
        <!-- Start 项目列表 -->
        <div class="">
          <el-table v-loading="tableLoading" :data="dataList" border style="width: 100%"
            @selection-change="selectionLineChangeHandle">
            <el-table-column fixed width="50" type="selection" align="center" />
            <el-table-column fixed width="60" label="序号" type="index" :index="indexMethod" align="center" />
            <el-table-column fixed label="操作" align="center" width="150">
              <template slot-scope="scope">
                <el-button type="primary" size="mini" :disabled="scope.row.nodeState!='待提交'"
                  @click="toRelevancy(scope.row)">办理</el-button>
                <el-button type="primary" size="mini" @click="toDetails(scope.row)">查看</el-button>
              </template>
            </el-table-column>
            <el-table-column prop="nodeState" label="流程状态" align="center" />
            <el-table-column prop="billNo" label="单据编号" align="center" />
            <el-table-column prop="createByName" label="申请人" align="center" />
            <el-table-column prop="createTime" label="申请日期" align="center" />
            <!-- <el-table-column prop="state" label="完成标志" align="center" /> -->
            <!--      <el-table-column label="完成标志" align="center" width="180">
              <template slot-scope="scope">
                <span>{{scope.row.state=='1'?'未完成':'已完成'}}</span>
              </template>
            </el-table-column> -->
            <el-table-column prop="finishTime" label="办结日期" align="center" />
            <el-table-column prop="createOrgName" label="调出分公司" align="center" />
            <el-table-column prop="outPlatName" label="调出站" align="center" />
            <el-table-column prop="inOrgName" label="调入分公司" align="center" />
            <el-table-column prop="inPlatName" label="调入站" align="center" />
            <el-table-column prop="confirmName" label="经办人" align="center" />
            <el-table-column prop="count" label="调拨数量" align="center" />
            <el-table-column prop="amount" label="调拨金额" align="center" />
          </el-table>
        </div>
        <!-- End 项目列表 -->
        <div class="pages">
          <div class="tit" />
          <!--  翻页  -->
          <pagination v-show="dataList.length > 0" :total="queryParams.total" :page.sync="queryParams.current"
            :limit.sync="queryParams.size" class="pagination" @pagination="loadList" />
        </div>
      </div>
    </div>
    <!-- End 查询和其列表部分 -->
    <!-- 新增修改部分组件 -->
    <divAdd v-show="viewState == 2 || viewState == 3" ref="divadd" @doback="resetState" @reloadlist="loadList" />
    <divInfo v-show="viewState == 4" ref="divinfo" @doback="resetState" @reloadlist="loadList" />

    <el-dialog title="补充物流信息" :visible.sync="dialogVisible" :before-close="handleClose"
      style="width:120%;margin-left: -10%;">
      <el-form ref="dataForm" class="formaddcopy02" style="margin-top: -40px;" :model="formobj">
        <el-row class="first_row">
          <el-col :span="24">
            <div class="span-sty">送货方式</div>
            <el-form-item> <el-select v-model="formobj.deliveryKey" placeholder="请选择送货方式" @change="selectDeliveryKey"
                class="addinputInfo">
                <el-option v-for="(item,i) in deliveryKeyList" :key="i" :label="item.name" :value="item.sid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row v-show="formobj.deliveryKey==1">
          <el-col :span="8">
            <div class="span-sty">快递单号</div>
            <el-form-item><el-input v-model="formobj.expressBillNo" placeholder="" class="addinputw addinputInfo"
                clearable /></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">快递公司</div>
            <el-form-item><el-input v-model="formobj.expressCompany" placeholder="" class="addinputw addinputInfo"
                clearable /></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">备注</div>
            <el-form-item><el-input v-model="formobj.remarks" placeholder="" class="addinputw addinputInfo"
                clearable /></el-form-item>
          </el-col>
        </el-row>

        <el-row v-show="formobj.deliveryKey==2">
          <el-col :span="8">
            <div class="span-sty">取货人</div>
            <el-form-item><el-input v-model="formobj.pickUpName" placeholder="" class="addinputw addinputInfo"
                clearable /></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">取货日期</div>
            <el-form-item> <el-date-picker v-model="formobj.pickUpDate" type="date" class="addinputw addinputInfo"
                value-format="yyyy-MM-dd" placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">备注</div>
            <el-form-item><el-input v-model="formobj.remarks" placeholder="" class="addinputw addinputInfo"
                clearable /></el-form-item>
          </el-col>
        </el-row>

        <el-row v-show="formobj.deliveryKey==3">
          <el-col :span="8">
            <div class="span-sty">送货人</div>
            <el-form-item><el-input v-model="formobj.deliveryPerson" placeholder="" class="addinputw addinputInfo"
                clearable /></el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">送达日期</div>
            <el-form-item> <el-date-picker v-model="formobj.deliveryDate" type="date" class="addinputw addinputInfo"
                value-format="yyyy-MM-dd" placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <div class="span-sty">备注</div>
            <el-form-item><el-input v-model="formobj.remarks" placeholder="" class="addinputw addinputInfo"
                clearable /></el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <div style="display: flex;flex-direction: row;width: 100%;justify-content: center;align-items: center;">
        <el-button type="primary" style="margin-top: 20px;" @click="saveData">确 定</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
  import req from '@/api/storage/allocationByBranches.js'
  import ButtonBar from '@/components/ButtonBar'
  import Pagination from '@/components/pagination'
  import pageye from '@/components/pagination/pageye'
  import divAdd from './allocationByBranchesAdd.vue'
  import divInfo from './allocationByBranchesInfo.vue'
  export default {
    components: {
      ButtonBar,
      Pagination,
      pageye,
      divAdd,
      divInfo
    },
    data() {
      return {
        dialogVisible: false,
        btndisabled: false,
        viewState: 1, // 1、列表 2、添加 3、修改 4、查看
        isSearchShow: false,
        searchxianshitit: '显示查询条件',
        tableLoading: false,
        dataList: [],
        btnList: [{
            type: 'primary',
            size: 'small',
            icon: 'plus',
            btnKey: 'toAdd',
            btnLabel: '新增'
          },

          {
            type: 'danger',
            size: 'small',
            icon: 'el-icon-delete',
            btnKey: 'doDel',
            btnLabel: '删除'
          },
          {
            type: 'primary',
            size: 'small',
            icon: 'edit',
            btnKey: 'toSupplement',
            btnLabel: '补充物流信息'
          },
          {
            type: 'info',
            size: 'small',
            icon: 'cross',
            btnKey: 'doClose',
            btnLabel: '关闭'
          }
        ],
        queryParams: {
          current: 1,
          size: 10,
          total: 0,
          params: {
            "outOrgName": "",
            "deptName": "",
            "createByName": "",
            "billNo": "",
            "createStartTime": "",
            "createEndTime": "",
            "finishStartTime": "",
            "finishEndTime": "",
            "outPlatName": "",
            "inOrgName": "",
            "inPlatName": "",
            "confirmName": "",
            "grade": "2",
          }
        },
        sids: [],
        selectionList: [],
        deliveryKeyList: [{
            name: '快递',
            sid: 1
          },
          {
            name: '对方自取',
            sid: 2
          },
          {
            name: '我方送货',
            sid: 3
          }
        ],
        formobj: {
          "sids": [],
          "remarks": "",
          "deliveryKey": "",
          "deliveryName": "",
          "expressBillNo": "",
          "expressCompany": "",
          "pickUpName": "",
          "pickUpDate": "",
          "deliveryPerson": "",
          "deliveryDate": ""
        }
      }
    },
    mounted() {
      this.$refs['btnbar'].setButtonList(this.btnList)
    },
    created() {
      this.loadList()
    },
    methods: {
      // 搜索条件效果
      clicksearchShow() {
        this.isSearchShow = !this.isSearchShow
        if (this.isSearchShow) {
          this.searchxianshitit = '隐藏查询条件'
        } else {
          this.searchxianshitit = '显示查询条件'
        }
      },
      btnHandle(btnKey) {
        switch (btnKey) {
          case 'toAdd':
            this.toAdd()
            break
          case 'doDel':
            this.doDel()
            break
          case 'toSupplement':
            this.toSupplement()
            break
          case 'doClose':
            this.doClose()
            break
          default:
            break
        }
      },
      loadList() {
        this.tableLoading = true
        this.queryParams.params.userSid = window.sessionStorage.getItem('userSid')
        this.queryParams.params.orgPath = window.sessionStorage.getItem('defaultOrgPath')
        this.queryParams.params.menuUrl = this.$route.path
        req.listPage(this.queryParams).then((resp) => {
          this.tableLoading = false
          if (resp.success) {
            const data = resp.data
            this.queryParams.total = data.total
            this.dataList = data.records
          } else {
            // 根据resp.code进行异常情况处理
            this.dataList = []
            this.queryParams.total = 0
          }
        }).catch(() => {
          this.tableLoading = false
        })
      },

      // 序号
      indexMethod(index) {
        var pagestart = (this.queryParams.current - 1) * this.queryParams.size
        var pageindex = index + 1 + pagestart
        return pageindex
      },
      selectTime1() {

      },
      selectTime2() {

      },

      dosearch() {
        this.queryParams.current = 1
        this.loadList()
      },
      resetQuery() {
        this.queryParams = {
          current: 1,
          size: 10,
          total: 0,
          params: {
            "outOrgName": "",
            "deptName": "",
            "createByName": "",
            "billNo": "",
            "createStartTime": "",
            "createEndTime": "",
            "finishStartTime": "",
            "finishEndTime": "",
            "outPlatName": "",
            "inOrgName": "",
            "inPlatName": "",
            "confirmName": "",
            "grade": "2",
          }
        }
        this.loadList()
      },
      toAdd() {
        this.viewState = 2
        this.$refs['divadd'].showAdd()
      },
      doDel() {

        if (this.sids.length > 0) {
          const tip = '请确认是否删除所选 ' + this.sids.length + ' 条记录?'
          this.$confirm(tip, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            const loading = this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            })
            req.deleteBySids(this.sids).then((resp) => {
              loading.close()
              if (resp.success) {
                this.$message({
                  type: 'success',
                  message: resp.msg,
                  showClose: true
                })
                this.loadList()
              } else {
                loading.close()
                // 根据resp.code进行异常情况处理
              }
            }).catch(e => {
              loading.close()
            })
          }).catch(() => {})
        } else {
          this.$message({
            type: "warning",
            showClose: true,
            message: '请至少选择一条记录进行删除操作'
          })
        }
      },

      selectionLineChangeHandle(val) {
        console.log("val", val);

        this.selectionList = val
        const aa = []
        val.forEach(element => {
          aa.push(element.sid)
        })
        this.sids = aa
      },
      toRelevancy(row) {
        this.viewState = 3
        this.$refs['divadd'].showEdit(row.sid)
      },
      toDetails(row) {
        this.viewState = 4
        this.$refs['divinfo'].showAdd(row.sid)
      },

      toSupplement() {
        if (this.sids.length > 0) {
          this.dialogVisible = true

          console.log("toSupplement", this.sids);

        } else {
          this.$message({
            type: "warning",
            showClose: true,
            message: '请至少选择一条记录进行操作'
          })
        }
      },
      saveData() {

        this.formobj.sids = this.sids

        req.saveLogisticsInfo(this.formobj).then((resp) => {
          if (resp.success) {
            this.dialogVisible = false
            this.$message({
              type: 'success',
              message: resp.msg,
              showClose: true
            })
            this.loadList()
          } else {
            // 根据resp.code进行异常情况处理
          }
        }).catch(e => {})


      },
      handleClose() {
        this.dialogVisible = false
      },
      selectDeliveryKey(val) {
        console.log("selectDeliveryKey", val);
        const choose = this.deliveryKeyList.filter((item) => item.sid == val)
        console.log('>>>>>>>>>selectDeliveryKey', choose)
        this.formobj.deliveryName = choose[0].name
        this.formobj.deliveryKey = choose[0].sid

      },
      doClose() {
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.go(-1)
      },

      resetState() {
        this.viewState = 1
      },

    }
  }
</script>
<style scoped>
  /deep/ .el-form-item__error {
    margin-left: 120px !important;
  }

  .must {
    color: #f00;
  }

  .span-sty {
    width: 90px !important;
  }

  .addinputInfo {
    margin-left: 80px !important;
  }

  .formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
    margin-left: 80px !important;
    width: calc(100% - 70px);
  }


  .first_row {
    border-top: 1px solid #E0E3EB;
  }

  .titleOne {
    padding: 7px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
  }

  /deep/ .el-form-item__error {
    margin-left: 180px !important;
  }

  .formaddcopy02 .el-row .el-col /deep/ .el-form-item .el-radio-group {
    display: inline;
    line-height: 1px;
    vertical-align: middle;
  }
</style>
