<!--钢材-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-row style="display: inline-block;width: 260px">
        <el-col :span="24">
          <el-form-item label="磅点" prop="poundid" label-width="40">
            <search-select v-model="queryParams.poundid"
                           params = "t_data_truepound;id;concat(name,' ',usrcode)"
                           where="poundtype = '3'"
                           orderby="usrcode asc"
            ></search-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="类型" prop="poundname" label-width="40">
            <el-select v-model="queryParams.poundclass" placeholder="类型">
              <el-option
                v-for="dict in dict.type.weighttype"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item style="width: 70px;color: #606266;margin: 0 0 0 0;">
        <label  style="margin-left: 10px;">过磅方式</label>
        <el-radio-group v-model="queryParams.weighttype">
          <el-radio v-for="dict in dict.type.weighttype" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="实时重量" prop="1aaa" label-width="90px">
        <el-input
          style=""
          clearable
        />
      </el-form-item>
      <el-form-item label="稳定重量" prop="1aaa">
        <el-input
          style=""
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleSearchMstBillFirst">自动过磅</el-button>
        <el-button type="primary" size="mini" @click="handleSearchMstBillFirst">手动过磅</el-button>
        <el-button type="primary" size="mini" @click="handleSearchMstBillFirst">完成</el-button>
        <el-button type="primary" size="mini" @click="handleSearchMstBillFirst">查询</el-button>
        <el-button type="primary" size="mini" @click="handleSearchMstBillFirst">打印</el-button>
      </el-form-item>
    </el-form>
    <!-- 主磅单信息 -->
        <el-card>
          <el-row>
            <el-col :span="18">
              <el-button class="transition-all duration-200" type="primary" size="mini">主磅单信息</el-button>
              <el-button class="transition-all duration-200" size="mini" @click = "getMstBillData">新增</el-button>
              <el-button class="transition-all duration-200" size="mini" @click = "getMstBillDataById">修改</el-button>
              <el-button class="transition-all duration-200" size="mini" @click="handleSearchMstBillFirst">查询</el-button>
              <el-table v-loading="mstBillLoading"
                        :data="mstBillList"
                        border
                        stripe
                        @selection-change="handleMstSelectionChange"
                        ref = "mstBillTable"
              >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="单号" align="center" prop="usrcode" />
                <el-table-column label="榜单日期" align="center" prop="bsndt" width="180">
                  <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.bsndt, '{y}-{m}-{d}') }}</span>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
            <el-col :span="6">
              <el-button class="transition-all duration-200" type="primary" size="mini">主磅单打印</el-button>
              <el-table v-loading="loading"
                        :data="operationpoundList"
                        border
                        stripe
              >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="按钮1" align="center" prop="id" />
                <el-table-column label="状态" align="center" prop="billstate">
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.pound_status" :value="scope.row.billstate"/>
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
        </el-card>

    <!-- 底部列表区域 -->
    <el-card>
      <el-row>
        <el-col :span="20">
        <el-button v-for="tab in tabs" :key="tab.name" :type="activeTab === tab.name ? 'primary' : 'default'"
                   @click="activeTab = tab.name" size="mini" > {{ tab.label }}</el-button>
          <el-table
                    v-loading="loading"
                    :data="operationpoundList"
                    v-if="activeTab === 'weighingRequest'"
                    border
                    stripe
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="按钮1" align="center" prop="id" />
            <el-table-column label="状态" align="center" prop="billstate">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.pound_status" :value="scope.row.billstate"/>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col :span="4">
              <el-button type="primary" size="mini" @click="handleSearchMstBillFirst">操作</el-button>
              <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                <el-row>
                  <el-col :span="6">
                    <el-form-item prop="poundid" label-width="40">
                      <p></p>
                    </el-form-item>
                  </el-col>
                  <el-col :span="18">
                    <el-form-item prop="poundid" label-width="40">
                      <el-button type="info" style="width: 200px;font-size: 20px">增明细</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item prop="poundid" label-width="40">
                      <p></p>
                    </el-form-item>
                  </el-col>
                  <el-col :span="18">
                    <el-form-item prop="poundname" label-width="40">
                      <el-button type="info" style="width: 200px;font-size: 20px">取消增明细</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item prop="poundid" label-width="40">
                      <span style="font-size: 20px">捆号</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="18">
                    <el-form-item prop="poundname" label-width="40">
                      <el-input
                        v-model="queryParams.poundname" style="width: 200px"
                        clearable
                        @keyup.enter.native="handleSearchMstBillFirst"
                      />
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item prop="poundid" label-width="40">
                      <p></p>
                    </el-form-item>
                  </el-col>
                  <el-col :span="18">
                    <el-form-item prop="poundid" label-width="40">
                      <el-button type="info" style="width: 200px;font-size: 20px">称 重</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item prop="poundid" label-width="40">
                      <p></p>
                    </el-form-item>
                  </el-col>
                  <el-col :span="18">
                    <el-form-item prop="poundname" label-width="40">
                      <el-button type="info" style="width: 200px;font-size: 20px">修改</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item prop="poundid" label-width="40">
                      <p></p>
                    </el-form-item>
                  </el-col>
                  <el-col :span="18">
                    <el-form-item prop="poundname" label-width="40">
                      <el-button type="info" style="width: 200px;font-size: 20px">删除</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="6">
                    <el-form-item prop="poundid" label-width="40">
                      <p></p>
                    </el-form-item>
                  </el-col>
                  <el-col :span="18">
                    <el-form-item prop="poundname" label-width="40">
                      <el-button type="info" style="width: 200px;font-size: 20px">打回复磅</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
        </el-col>
        </el-row>
    </el-card>



    <!-- 添加或修改主榜单信息对话框 -->
    <el-dialog :title="mstBillFormData.mstBillTitle" :visible.sync="mstBillFormData.mstBillOpen" width="1500px" append-to-body>
      <el-form ref="mstBillFormData.mstBillForm" :model="mstBillFormData.mstBillForm" :rules="mstBillFormData.mstBillRules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="主榜单编号" prop="usrcode" readonly>
              <el-input v-model="mstBillFormData.mstBillForm.usrcode" placeholder="请输入主榜单编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务日期" prop="bsndt">
              <el-date-picker clearable
                              v-model="mstBillFormData.mstBillForm.bsndt"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择业务日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务磅点" prop="poundid">
              <search-select
                v-model="mstBillFormData.mstBillForm.poundid"
                params="t_data_buspound;id;concat(name,' ',usrcode)"
                where = ""
                orderby="usrcode asc"
                :clearable = "true"
                :disabled = "true"
              ></search-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="发货单位" prop="sendunitid">
              <search-select v-model="mstBillFormData.mstBillForm.sendunitid"
                             params = "t_data_pounddata;usrcode;concat(name,' ',usrcode)"
                             where="type = 'shippingCompany'"
                             orderby="usrcode asc"
                             clearable
              ></search-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品名" prop="itmcode">
              <search-select v-model="mstBillFormData.mstBillForm.itmcode"
                             params = "t_data_pounddata;usrcode;concat(name,' ',usrcode)"
                             where="type = 'productName'"
                             orderby="usrcode asc"
                             clearable
              ></search-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="收货单位" prop="recunitid">
              <search-select v-model="mstBillFormData.mstBillForm.recunitid"
                             params = "t_data_pounddata;usrcode;concat(name,' ',usrcode)"
                             where="type = 'receivingCompany'"
                             orderby="usrcode asc"
                             clearable
              ></search-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="炉次" prop="boilerno">
              <el-input v-model="mstBillFormData.mstBillForm.boilerno" placeholder="请输入炉次" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="承运单位" prop="trnunitid">
              <search-select v-model="mstBillFormData.mstBillForm.trnunitid"
                             params = "t_data_pounddata;usrcode;concat(name,' ',usrcode)"
                             where="type = 'teamOrCarrier'"
                             orderby="usrcode asc"
                             clearable
              ></search-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计量单位" prop="msrunit">
              <el-input v-model="mstBillFormData.mstBillForm.msrunit" placeholder="请输入计量单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="usrcode">
              <el-input v-model="mstBillFormData.mstBillForm.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="mstBillFormSubmit">确 定</el-button>
        <el-button @click="mstBillFormCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSteelpound, getSteelpound, delSteelpound, addSteelpound, updateSteelpound } from "@/api/measurement/operation/steelpound"
import {getTruepound} from "@/api/measurement/basedata/truepound";
import SearchSelect from "@/components/SearchSelect";
import {addPoundmst, getNumber, getPoundmst, listPoundmst, updatePoundmst} from "@/api/measurement/bill/poundmst";
import {listPoundbill} from "@/api/measurement/bill/poundbill";

export default {
  name: "Steelpound",
  components: {SearchSelect},
  dicts: ['pound_status', 'pound', 'sys_yes_no','weighttype'],
  data() {
    return {
      // 遮罩层
      loading: false,
      //主榜单遮罩层
      mstBillLoading:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 钢材磅表格数据
      operationpoundList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        poundid:"",
        weighttype:  "2",//过磅方式
        poundtype: "",//磅点类型,用来存储出厂或入厂磅等。根据此存在不同的查询逻辑。
        poundclass:"",//磅点种类，只做展示，无实际作用。
        timeoutId:null,//存储定时器，用来校验车号输入后的2秒再自动执行历史皮重查询。
      },
      // 表单参数
      form: {},
      tabs: [
        { name: 'weighingRequest', label: '磅单明细' }
      ],
      activeTab: 'weighingRequest',
      // 表单校验
      rules: {
      },
      truepoundData:{},
      queryPageParams:{
        mstBill: {
          total: 0,
          pageNum: 1,
          pageSize: 10,
          isinuse:"Y",
          recunitid:"",//收货单位
          itmcode:"",//物料编码
          trnunitid:"",//运输单位
          sendunitid:"",//发货单位
          poundid:"",//业务磅点,java对象中存储为该字段。
        },
        poundBillDetail: {
          total: 0,
          pageNum: 1,
          pageSize: 10
        },
        poundBillFull: {
          total: 0,
          pageNum: 1,
          pageSize: 10,
          poundid: "",
          queryStart: "",
          queryEnd: "",
          carno: "",
          usrcode: "",
        },
        historyPound:{
          total:0,
          pageNum:1,
          pageSize:10,

        },
      },
      //主榜单数据列表
      mstBillList:[],
      //主榜单相关界面参数
      mstBillFormData:{
        selectedIds:[],
        mstBillForm:{},
        mstBillRules:{},
        mstBillTitle:"主榜单信息",
        mstBillOpen:false,
        thisSelectedRow:{},
      },
    }
  },
  created() {
    // this.getList()
  },
  watch: {
    //监听磅点变化
    'queryParams.poundid': function(newVal, oldVal) {
      if(newVal!= null && newVal!== ""){
        //如果磅点不为空，则根据id查询数据库，获取值将磅单种类赋值到poundtype
        getTruepound(newVal).then(response => {
          this.queryParams.poundtype = response.data.poundtype;
          this.queryParams.poundclass = response.data.poundclass;
          //将获取到的物理磅点所有数据存储下来
          this.truepoundData = response.data;
          //触发查询主榜单的逻辑。
          this.handleSearchMstBillFirst();
        });

      }else {
        this.queryParams.poundtype = "";
        this.queryParams.poundclass = "";
        this.truepoundData = {};
      }
    },
  },
  methods: {
    /** 查询钢材磅列表 */
    getList() {
      this.loading = true
      listSteelpound(this.queryParams).then(response => {
        this.operationpoundList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billstate: null,
        usrcode: null,
        carno: null,
      }
      this.resetForm("form")
    },
    // 主榜单表单重置
    resetMstBillForm() {
      this.mstBillFormData.mstBillForm = {
        id: null,
        bsndt: null,
        poundid: null,
        sendunitid: null,
        itmcode: null,
        recunitid: null,
        boilerno: null,
        trnunitid: null,
        msrunit: null,
        usrcode: null,
      }
      this.resetForm("mstBillFormData.mstBillForm");
    },
    // 主榜单多选框选中数据
    handleMstSelectionChange(selection) {
      if (selection.length > 0){
        this.mstBillFormData.selectedIds = selection.map(item => item.id)
        var mstId = this.mstBillFormData.selectedIds[0];
        this.queryPoundBillDetail(mstId);
      }
    },
    //根据主榜单ID查询过磅明细。
    queryPoundBillDetail(id){
      var thisQueryParams = {
        billstate:"1",
        isinuse:"Y"
      }
      thisQueryParams.appliid = id;
      listPoundbill(thisQueryParams).then(response => {
        this.queryPageParams.poundBillDetail.total = response.total;
        this.poundBillDetailList = response.rows;
      })
    },
    //检查是否选中磅点。
    checkPoundSelected() {
      if (this.queryParams.poundid == null || this.queryParams.poundid === "") {
        this.$message.error("请选择磅点！");
        return false;
      }
      return true;
    },
    //查询主榜单数据,分页设置为1
    handleSearchMstBillFirst(){
      this.queryPageParams.mstBill.pageNum = 1;
      this.handleSearchMstBill();
    },
    //查询主榜单数据
    handleSearchMstBill(){
      this.mstBillLoading = true
      this.queryPageParams.mstBill.poundid = this.truepoundData.buspoundid;
      listPoundmst(this.queryPageParams.mstBill).then(response => {
        this.queryPageParams.mstBill.total = response.total;
        this.mstBillList = response.rows;
        this.mstBillLoading = false;
      })
    },
    //新增主榜单，将当前重量上的数据赋值到重量中，并且打开弹窗
    getMstBillData(){
      //如果没有选中磅点
      if (!this.checkPoundSelected())return;
      this.resetMstBillForm();
      getNumber().then(response => {
        this.mstBillFormData.mstBillForm.usrcode = response.data.number;
        this.mstBillFormData.mstBillForm.bsndt = response.data.date;
        //将业务磅点进行赋值
        this.mstBillFormData.mstBillForm.poundid = this.truepoundData.buspoundid;
        this.mstBillFormData.mstBillForm.mstBillTitle = '新增主榜单';
        //打开弹窗界面
        this.mstBillFormData.mstBillOpen = true;

      });
    },
    //修改主榜单
    getMstBillDataById(){
      debugger
      if(this.mstBillFormData.selectedIds.length==0){
        this.$message.error("请选择磅单！");
        return;
      }
      const id = this.mstBillFormData.selectedIds[0];
      this.resetMstBillForm();
      getPoundmst(id).then(response => {
        this.mstBillFormData.mstBillForm = response.data;
        this.mstBillFormData.mstBillTitle = '修改主榜单';
        this.mstBillFormData.mstBillOpen = true;
      })
    },
    //主榜单界面，点击确定。
    mstBillFormSubmit(){
      this.$refs["mstBillFormData.mstBillForm"].validate(valid => {
        if (valid) {
          //设置业务磅点ID
          this.mstBillFormData.mstBillForm.poundid = this.truepoundData.buspoundid;
          if (this.mstBillFormData.mstBillForm.id == null){
            addPoundmst(this.mstBillFormData.mstBillForm).then(response => {
              this.$message.success("新增主榜单成功！");
              this.mstBillFormData.mstBillOpen = false;
              //查询主榜单数据
              this.handleSearchMstBillFirst();
            })
          }else{
            updatePoundmst(this.mstBillFormData.mstBillForm).then(response => {
              this.$message.success("修改主榜单成功！");
              this.mstBillFormData.mstBillOpen = false;
              //查询主榜单数据
              this.handleSearchMstBillFirst();
            })
          }
        }
      })
    },
    //主榜单界面，点击取消。
    mstBillFormCancel(){
      this.mstBillFormData.mstBillOpen = false;
    },
  }
}
</script>
