<template>
  <div class="app-container">
    <div v-show="show == 0">
      <SearchBlock v-model="queryParams" v-show="showSearch" size="mini" :menus="queryOption" count="6" @change="handleQuery"></SearchBlock>
      <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="单据类型" prop="billType">
          <el-select v-model="queryParams.billType" placeholder="请选择单据类型" clearable>
            <el-option
              v-for="dict in dict.type.sys_document_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="提运单号" prop="billNo">
          <el-input
            v-model="queryParams.billNo"
            placeholder="请输入提运单号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="单据编号" prop="billNumber">
          <el-input
            v-model="queryParams.billNumber"
            placeholder="请输入单据编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="航次号" prop="voyageNo">
          <el-input
            v-model="queryParams.voyageNo"
            placeholder="请输入航次号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="船舶名称" prop="vesselName">
          <el-input
            v-model="queryParams.vesselName"
            placeholder="请输入船舶名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="单据日期" prop="queryTime">
          <el-date-picker clearable
                          v-model="queryTime"
                          type="daterange"
                          :picker-options="pickerOptions"
                          value-format="yyyy-MM-dd"
                          end-placeholder="结束日期"
                          range-separator="-" start-placeholder="开始日期"
                          placeholder="请选择单据日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form> -->

      <el-row :gutter="10" class="mb8">

        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleX"
          >单据明细
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" highlight-current-row :data="ckbillList" @sort-change="handleSortChange"
                @selection-change="handleSelectionChange" border
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="单据类型" align="center" prop="billType" sortable="custom">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_document_type" :value="scope.row.billType"/>
          </template>
        </el-table-column>
        <el-table-column label="数据来源" align="center" prop="sourceType" sortable='custom' min-width="100" >
          <template slot-scope="scope">
            <el-tag>{{scope.row.sourceType=='0'?'载货清单':scope.row.sourceType=='2'?'转场':scope.row.sourceType=='3'?'混配':'直接出库'}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="单据日期" align="center" prop="billDate" width="180" sortable="custom">
          <template slot-scope="scope">

            <span>{{ parseTime(scope.row.billDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>

          </template>
        </el-table-column>
        <el-table-column label="提运单号" align="center" prop="billNo" sortable="custom"/>
        <el-table-column label="单据编号" align="center" prop="billNumber" sortable="custom"/>
        <el-table-column label="航次号" align="center" prop="voyageNo" sortable="custom"/>
        <el-table-column label="船舶名称" align="center" prop="vesselName" sortable="custom"/>


        <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="120px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleX(scope.row)"
              v-hasPermi="['jxc:ckbill:edit']"
            >单据明细
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination

        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改出入库单据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">

        <el-row>
          <el-col :span="8">
            <el-form-item label="单据类型" prop="billType">
              <el-select v-model="form.billType" placeholder="请选择单据类型" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_document_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单据日期" prop="billDate">
              <el-date-picker clearable
                              v-model="form.billDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择单据日期"
                              style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="提运单号" prop="billNo">
              <el-input v-model="form.billNo" placeholder="请输入提运单号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="单据编号" prop="billNumber">
              <el-input v-model="form.billNumber" placeholder="请输入单据编号"/>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="航次号" prop="voyageNo">
              <el-select v-model="form.voyageNo" filterable placeholder="请输入航次号" style="width: 100%" clearable
                         @change="setBmvoyageDetail"
              >
                <el-option
                  v-for="item in bmvoyageList"
                  :key="item.id"
                  :label="item.voyageNo"
                  :value="item.id"

                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="船舶名称" prop="vesselName">
              <el-select v-model="form.vesselId" filterable placeholder="请输入船舶名称" style="width: 100%" clearable
                         @change="setVesselDetail"
              >
                <el-option
                  v-for="item in vesselList"
                  :key="item.id"
                  :label="item.vesselName"
                  :value="item.id"

                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <div v-if="show == 1 ">
      <div>
        <el-button @click="cancel" style="right: 3%;position:absolute;">返 回</el-button>
        <br>
        <br>
        <el-table
          :data="tableData"
        >


          <el-table-column label="单据信息" align="center">
            <el-table-column label="单据日期" align="center" prop="billDate" width="180" sortable="custom">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.billDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="单据编号" align="center" prop="billNumber" sortable="custom" width="100"/>
            <el-table-column label="出入库类型" align="center" prop="inoutType" sortable="custom" width="120">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_inbound_outbound_type" :value="scope.row.inoutType"/>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="船舶信息" align="center">

            <el-table-column label="航次号" align="center" prop="voyageNo" sortable="custom" width="100"/>
            <el-table-column label="船舶名称" align="center" prop="vesselName" sortable="custom" width="100"/>
          </el-table-column>
          <el-table-column label="货物信息" align="center">

            <el-table-column label="HS编号" align="center" prop="hsCode" sortable="custom" width="100"/>
            <el-table-column label="货物名称" align="center" prop="cargoName" sortable="custom" width="100"/>
            <el-table-column label="提运单号" align="center" prop="billNo" sortable="custom" width="100"/>
            <el-table-column label="规格类型品质" align="center" show-overflow-tooltip prop="cargoSpec" sortable="custom" min-width="200"/>
            <el-table-column label="原产国" align="center" prop="originCountry" sortable="custom" width="100"/>
            <el-table-column label="货主名称" align="center" prop="custName" sortable="custom" width="100"/>

            <el-table-column label="业务性质" align="center" prop="opType" sortable="custom" width="100">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_business_nature" :value="scope.row.opType"/>
              </template>
            </el-table-column>
            <el-table-column label="贸易性质" align="center" prop="tradType" sortable="custom" width="100">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_trade_nature" :value="scope.row.tradType"/>
              </template>
            </el-table-column>
            <el-table-column label="计划货物件数" align="center" prop="cargoNum" sortable="custom" width="130"/>
            <el-table-column label="计划货物重量" align="center" prop="cargoWt" sortable="custom" width="130"/>
            <el-table-column label="计划货物体积" align="center" prop="cargoVol" sortable="custom" width="130"/>
            <!--              <el-table-column label="库场ID" align="center" prop="stockId" sortable='custom' />-->
            <el-table-column label="库场名称" align="center" prop="stockCode" sortable="custom" width="100">
              <template slot-scope="scope">
              <WarehouseName :stockCode="scope.row.stockCode"/>
            </template>
            </el-table-column>
            <el-table-column label="仓位名称" align="center" prop="stockName" sortable="custom" width="100"/>
          </el-table-column>


        </el-table>
        <pagination

          :total="detailTotal"
          :page.sync="detailParams.pageNum"
          :limit.sync="detailParams.pageSize"
          @pagination="handleX"
        />
      </div>
    </div>

  </div>
</template>

<script>
import { pageListCkbill, getCkbill, delCkbill, addCkbill, updateCkbill } from '@/api/jxc/ckbill'
import { listVessel } from '@/api/jxc/vessel'
import { listBmvoyage } from '@/api/jxc/bmvoyage'
import { pageListCkcargo } from '@/api/jxc/ckcargo'
import { datePickerOpts } from "@/utils";

export default {
  name: 'Ckbill',

  dicts: ['sys_document_type', 'sys_state','sys_inbound_outbound_type','sys_business_nature','sys_trade_nature'],
  data() {
    return {
      queryOption: [
        {
          label: '单据类型',
          prop: 'billType',
          type: 'select',
          getOptions: () => this.dict.type.sys_document_type,
        },
        {
          label: '单据编号',
          prop: 'billNumber',
          type: 'text',
        },
        {
          label: '提运单号',
          prop: 'billNo',
          type: 'text',
        },
        {
          label: '航次号',
          prop: 'voyageNo',
          type: 'text',
        },
        {
          label: '船舶名称',
          prop: 'vesselId',
          type: 'select',
          getOptions: () => this.vesselList,
          optionsProp: 'id',
          optionsLabel: 'vesselName',
        },
        {
          label: '单据日期',
          prop: 'queryTime',
          type: 'daterange',
          options: datePickerOpts()
        },
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      tableData: [{
        id: 3,
        cargoName: '铁',
        stockName: '1号库场',
        custName: '王晓敏',
        cargoNum: '1519 ',
        cargoWt: '11',
        cargoVol: '120',

        billDate: '2020-10-11',
        billNumber: '一号',
        inoutType: '出库',
        voyageNo: '210号航次',
        vesselName: '关下号轮船',
        hsCode: '101号货物编码',
        billNo: '101单号',
        cargoSpec: '优良品质',
        originCountry: '中国',
        opType: '业务性质1',
        tradType: '贸易性质1',
        stockCode: '1012号库场'

      }, {
        id: 31,
        cargoName: '铁',

        stockName: '1号库场',
        custName: '王晓敏',
        cargoNum: '519 ',
        cargoWt: '10',
        cargoVol: '100',
        billDate: '2020-10-11',
        billNumber: '一号',
        inoutType: '出库',
        voyageNo: '210号航次',
        vesselName: '关下号轮船',
        hsCode: '101号货物编码',
        billNo: '101单号',
        cargoSpec: '优良品质',
        originCountry: '中国',
        opType: '业务性质1',
        tradType: '贸易性质1',
        stockCode: '1012号库场'
      }
      ],
      show: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单据时间
      queryTime: [],
      // 船舶航次表格数据
      bmvoyageList: [],
      // 船舱表格数据
      vesselList: [],
      // 出入库单据表格数据
      ckbillList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billType: null,
        billDate: null,
        billNo: null,
        voyageId: null,
        voyageNo: null,
        vesselId: null,
        vesselName: null,
        status: null,
        beginDate: null,
        endDate: null
      },
      detailTotal:0,
      detailParams: {
        pageNum: 1,
        pageSize: 10,
        billId: null,
        billDate: null,
        billNo: null,
        voyageId: null,
        voyageNo: null,
        vesselId: null,
        vesselName: null,
        status: null,
        beginDate: null,
        endDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        voyageNo: [
          { required: true, message: '航次号不能为空', trigger: 'blur' }
        ],
        billType: [
          { required: true, message: '单据类型不能为空', trigger: 'blur' }
        ],
        billDate: [
          { required: true, message: '单据日期不能为空', trigger: 'blur' }
        ],

        vesselName: [
          { required: true, message: '船舶名称不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getVesselList()
    this.getBmvoyage()

  },
  methods: {
    handleX(row) {
      if(row){
        this.detailParams.billId = row.id;
      }
      pageListCkcargo(this.detailParams).then(response => {
        this.tableData = response.rows;
        this.detailTotal = response.total;
        this.show =1;
      }).catch(e => {
      });
    },
    //航次
    setBmvoyageDetail(voyageId) {
      console.log(voyageId)

      this.bmvoyageList.forEach(ele => {
        if (ele.id == voyageId) {
          this.form.voyageNo = ele.voyageNo
          this.form.vesselName = ele.vesselName
          // this.form.berthName = ele.berthName;
          this.form.deptId = ele.deptId
          this.form.vesselId = ele.vesselId
          this.form.netTon = ele.netTon
          this.form.opType = ele.opType

        }
      })
    },
    getBmvoyage() {
      let param = {}
      listBmvoyage(param).then(response => {
        this.bmvoyageList = response.data
      }).catch(e => {
      })
    },
    //船舶
    setVesselDetail(vesselId) {
      console.log(vesselId)
      this.vesselList.forEach(ele => {
        if (ele.id == vesselId) {
          console.log('aa:', ele.vesselName)
          this.form.vesselName = ele.vesselName

        }
      })
    },

    getVesselList() {
      let param = {}
      listVessel(param).then(response => {
        this.vesselList = response.data
      }).catch(e => {
      })
    },

    /** 查询出入库单据列表 */
    getList() {
      this.loading = true
      if (this.billQuery.queryTime) {
        this.billQuery.beginDate = this.billQuery.queryTime[0];
        this.billQuery.endDate = this.billQuery.queryTime[1];
      } else {
        this.queryParams.beginDate = null
        this.queryParams.endDate = null
      }
      pageListCkbill(this.queryParams).then(response => {
        this.ckbillList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.show = 0
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billType: null,
        billDate: null,
        billNo: null,
        voyageId: null,
        voyageNo: null,
        vesselId: null,
        vesselName: null,
        status: null,
        remark: null
      }

      this.resetForm('form')
    },
    //排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams)
      this.getList()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryTime = []

      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加出入库单据'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCkbill(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改出入库单据'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCkbill(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addCkbill(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      const names = row.id || this.names
      this.$modal.confirm('是否确认删除"' + row.billNumber + '"的数据项？').then(function() {
        return delCkbill(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jxc/ckbill/export', {
        ...this.queryParams
      }, `出入库单据_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
