<template>
  <div class="detail-content">
    <div class="sub-title">场次基本情况</div>
    <el-form ref="dform" :model="dform" label-width="10em" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item label="委托编号:" prop="wtbm" id="protectedItems">
            <el-select
              v-model="dform.wtbh"
              clearable
              class="w90"
              @change="handleWtChange"
              :disabled="isDisabled"
            >
              <el-option
                v-for="(item, index) in wtbhList"
                :key="index"
                :label="item.wtbh"
                :value="item.wtbm"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="场次:" prop="apcc">
            <el-input
              v-model="dform.apcc"
              clearable
              class="w90"
              :disabled="isDisabled"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="交易方向:">
            <!-- <el-select v-model="dform.jyfx" class="w90">
              <el-option label="采购" :value="0"></el-option>
              <el-option label="销售" :value="1"></el-option>
              <el-option label="购销双向" :value="'0,1'"></el-option>
            </el-select> -->
            <el-checkbox-group
              :disabled="isDisabled"
              style="width: 100%"
              v-model="dform.jyfx"
            >
              <el-checkbox class="radioSize" label="采购"></el-checkbox>
              <el-checkbox class="radioSize" label="销售"></el-checkbox>
            </el-checkbox-group>
            <!-- <el-radio-group v-model="dform.jyfx" :disabled="isDisabled">
              <el-radio :label="0">采购</el-radio>
              <el-radio :label="1">销售</el-radio>
            </el-radio-group> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易方式:">
            <!-- <el-select v-model="dform.jyfx" class="w90">
              <el-option label="采购" :value="0"></el-option>
              <el-option label="销售" :value="1"></el-option>
              <el-option label="购销双向" :value="'0,1'"></el-option>
            </el-select> -->
            <el-radio-group v-model="dform.jyfs" :disabled="isDisabled">
              <el-radio :label="0">竞价</el-radio>
              <el-radio :label="1">挂牌</el-radio>
              <el-radio :label="2">购销双向</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="交易日期:" prop="jyrq">
            <el-date-picker
              type="date"
              v-model="dform.jyrq"
              clearable
              class="w90"
              :disabled="isDisabled"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易时间:" prop="jysj">
            <el-time-picker
              v-model="dform.jysj"
              clearable
              class="w90"
              format="HH:mm"
              value-format="HH:mm"
              :disabled="isDisabled"
              placeholder="任意时间点"
            >
            </el-time-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="委托单位:" prop="wtfmc">
            <el-input
              v-model="dform.wtfmc"
              clearable
              class="w90"
              :disabled="isDisabled"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手续费率(%):" prop="sxfl">
            <!-- oninput="value=value.replace(/[^\d.]/g,'').replace(/^\./g,'').replace(/\.{2,}/g,'.').replace('.','$#$').replace(/\./g,'').replace('$#$','.')" -->
            <el-input
              v-model="dform.sxfl"
              @change="sxflChange"
              clearable
              class="w90"
              :disabled="isDisabled"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="交易保证金(元/吨):" prop="jybzj">
            <el-input
              :disabled="isDisabled"
              clearable
              class="w90"
              v-model="dform.jybzj"
              v-bind:value="
                String(dform.jybzj).indexOf('.') > -1
                  ? Number(dform.jybzj).toFixed(2)
                  : dform.jybzj
              "
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="履约保证金(元/吨):" prop="lybzj">
            <el-input
              :disabled="isDisabled"
              clearable
              class="w90"
              v-model="dform.lybzj"
              v-bind:value="
                String(dform.lybzj).indexOf('.') > -1
                  ? Number(dform.lybzj).toFixed(2)
                  : dform.lybzj
              "
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row id="tradingFloorPrice"></el-row>
    <div class="sub-title" v-if="subjectMatter.length > 0">
      标的信息-入库安排
    </div>

    <div
      v-for="(item, index) in subjectMatter"
      :key="index"
      v-if="subjectMatter.length > 0"
    >
      <div class="tableheader">承储单位：{{ item.dwmc }}</div>
      <el-table
        :data="item.detailList"
        border
        stripe
        show-header
        tooltip-effect="dark"
      >
        <!-- <el-table-column type="index" label="序号" align="center" width="60"></el-table-column> -->
        <el-table-column
          label="标的"
          prop="bdh"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <!-- <el-table-column label="承储单位" prop="dwmc" align="center" show-overflow-tooltip></el-table-column> -->
        <el-table-column
          label="仓号"
          prop="ccdd"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="品种"
          prop="pzmc"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="生产年度"
          prop="scnd"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="数量(吨)"
          prop="lssl"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{
              String(scope.row.lssl).indexOf(".") > -1
                ? parseFloat(scope.row.lssl).toFixed(3)
                : scope.row.lssl
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="完成时间"
          prop="wcsj"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="入库能力(吨/天)" prop="rknl" align="center">
          <template slot-scope="scope">
            {{
              String(scope.row.rknl).indexOf(".") > -1
                ? parseFloat(scope.row.rknl).toFixed(3)
                : scope.row.rknl
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否空仓"
          prop="sfkc"
          align="center"
        ></el-table-column>
        <el-table-column
          label="交易底价(元/吨)"
          prop="jydj"
          align="center"
          label-class-name="require_star"
        >
          <template slot-scope="scope">
            <el-input
              v-if="isUpdate"
              v-model="scope.row.jydj"
              onkeyup="value=value.match(/\d+\.?\d{0,2}/)"
              class="item"
              placeholder="请输入底价"
            ></el-input>
            <div v-else class="text">
              <span v-if="toHidden && scope.row.jydj">****</span>
              <span v-else>
                {{
                  String(scope.row.jydj).indexOf(".") > -1
                    ? parseFloat(scope.row.jydj).toFixed(2)
                    : scope.row.jydj
                }}
              </span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-row id="tradingFloorPrice1"></el-row>
    <div
      class="sub-title"
      v-if="subjectMatter1.length > 0"
      style="margin-top: 10px"
    >
      标的信息-货源表
    </div>
    <div
      v-for="(item, index) in subjectMatter1"
      :key="index"
      v-if="subjectMatter1.length > 0"
    >
      <div class="tableheader">承储单位：{{ item.dwmc }}</div>
      <el-table
        :data="item.detailList"
        border
        stripe
        show-header
        tooltip-effect="dark"
      >
        <!-- <el-table-column type="index" label="序号" align="center" width="60"></el-table-column> -->
        <el-table-column
          align="center"
          prop="bdh"
          label="标的"
          show-overflow-tooltip
        >
        </el-table-column>
        <!-- <el-table-column
          label="承储单位"
          prop="dwmc"
          align="center"
          show-overflow-tooltip
        ></el-table-column> -->
        <el-table-column
          label="仓号"
          prop="ccdd"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="品种"
          prop="pzmc"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="生产年限"
          prop="scnd"
          align="center"
        ></el-table-column>
        <el-table-column
          label="数量(吨)"
          prop="lssl"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{
              String(scope.row.lssl).indexOf(".") > -1
                ? parseFloat(scope.row.lssl).toFixed(3)
                : scope.row.lssl
            }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="wcsj"
          label="质量指标"
          v-if="zlzbPz.indexOf('小麦粉') > -1"
        >
          <el-table-column align="center" prop="jgjd" label="加工精度">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].jgjd }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="sf" label="水分%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].sf }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="hf" label="灰分%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].hf }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="szqw" label="气味口味">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].szqw }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          prop="wcsj"
          label="质量指标"
          v-if="zlzbPz.indexOf('小麦') > -1 && zlzbPz.indexOf('小麦粉') == -1"
        >
          <el-table-column align="center" prop="sf" label="水分%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].sf }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="zhaz" label="杂质%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].zhaz }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="bwsl" label="不完善粒%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].bwsl }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="rz" label="容重g/L">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].rz }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          prop="wcsj"
          label="质量指标"
          v-if="zlzbPz.indexOf('米') > -1"
        >
          <el-table-column align="center" prop="sm" label="碎米%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].sm }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="sf" label="水分%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].sf }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="zhaz" label="杂质%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].zhaz }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="szqw"
            label="色泽气味"
            width="130"
          >
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].szqw }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          align="center"
          prop="wcsj"
          label="质量指标"
          v-if="zlzbPz.indexOf('稻谷') > -1"
        >
          <el-table-column align="center" prop="ccl" label="出糙率%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].ccl }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="sf" label="水分%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].sf }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="zhaz" label="杂质%">
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].zhaz }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="szqw"
            label="色泽气味"
            width="130"
          >
            <template slot-scope="scope">
              <span>{{ item.detailList[scope.$index].szqw }}</span>
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          label="完成时间"
          prop="wcsj"
          align="center"
        ></el-table-column>
        <el-table-column label="出库能力(吨/天)" prop="rknl" align="center">
          <template slot-scope="scope">
            {{
              String(scope.row.rknl).indexOf(".") > -1
                ? parseFloat(scope.row.rknl).toFixed(3)
                : scope.row.rknl
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="交易底价(元/吨)"
          prop="jydj"
          align="center"
          label-class-name="require_star"
        >
          <template slot-scope="scope">
            <el-input
              v-if="isUpdate"
              v-model="scope.row.jydj"
              onkeyup="value=value.match(/\d+\.?\d{0,2}/)"
              class="item"
              placeholder="请输入底价"
            ></el-input>
            <div v-else class="text">
              <span v-if="toHidden && scope.row.jydj">****</span>
              <span v-else>
                {{
                  String(scope.row.jydj).indexOf(".") > -1
                    ? parseFloat(scope.row.jydj).toFixed(2)
                    : scope.row.jydj
                }}
              </span>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-row class="btn-footer">
      <el-button
        v-if="isUpdate"
        type="primary"
        icon="iconfont iconbaocun"
        @click="handleUpdate('待提交')"
        >保存</el-button
      >
      <el-button
        v-if="isUpdate"
        type="primary"
        icon="iconfont icontijiao"
        @click="handleUpdate('待盖章')"
        >提交</el-button
      >
      <el-button
        v-if="$route.query.type == 3 && isSeal"
        type="primary"
        icon="iconfont icongaizhang"
        @click="signature"
        >盖章</el-button
      >
      <el-button
        v-if="$route.query.type == 3 && isSeal"
        type="primary"
        icon="iconfont icongaizhang"
        @click="doreject"
        >驳回</el-button
      >
      <el-button
        v-if="isExport"
        type="primary"
        class="iconfont icondaochu11"
        @click="exportFile"
        >导出</el-button
      >
      <el-button
        type="primary"
        icon="iconfont iconfanhui"
        @click="$router.push('/transaction/tradingFloorPrice/index')"
        >返回</el-button
      >
    </el-row>
    <sealDialog
      ref="sealDialog"
      sealId="tradingFloorPrice"
      @sealTodo="sealTodo"
      @sealError="sealError"
    ></sealDialog>
  </div>
</template>
<script>
import sealDialog from "@/components/sealDialog/index.vue";
import {
  selectwtList,
  selectEntryAndExitOrder,
  selectEntryAndExitOrderBySite,
  selectCode,
  insertCC,
  updateCC,
  selectCC,
  valiCode,
  selectList,
  getBdInfo,
  updatePrice, //更新底价
  updatePrice2NextNode, //底价状态更新
} from "@/api/transaction/trading/fieldInfo/index";
import { NumberToChinese, ChineseToNumber } from "@/utils/num2cn";
import { dateFormat } from "@/utils/index";
import htmlToPdf from "@/utils/htmlToPdf";
const log = console.log.bind(console);
const status = {
  // ['91000048','91000008','91000001','91000004']
  91000001: "待提交",
  91000300: "待盖章",
  91000600: "已确认",
  91000103: "签章驳回",
};
export default {
  components: { sealDialog },
  data() {
    var checkmoney = (rule, value, callback) => {
      if (!value) {
        if (this.saveClose) {
          callback(new Error("请输入"));
        } else {
          callback();
        }
      } else {
        if (!/^[0-9]\d{0,10}(\.\d{1,2})?$/.test(value)) {
          // 保留两位小数
          callback(new Error("请输入数字,保留两位小数"));
        } else {
          callback();
        }
      }
    };
    var checkinput = (rule, value, callback) => {
      if (!value) {
        if (this.saveClose) {
          callback(new Error("请输入"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    return {
      toHidden: true,
      zlzbPz: "小麦",
      dform: {
        wtbm: "",
        apcc: "",
        jyfx: "",
        jyfs: "",
        jyrq: "",
        jysj: "",
        wtfmc: "",
        sxfl: "",
        jybzj: "",
        lybzj: "",
      },
      jyfxArr: { 采购: "0", 销售: "1", 0: "采购", 1: "销售" },
      tableData: [],
      isView: false,
      wtOpts: [],
      isAdd: false,
      msg: "",
      isUpdate: false,
      ccbm: null,
      saveClose: true,
      wtbhList: [],
      // 查看编辑时禁止输入
      isDisabled: false,
      // 标的数据
      subjectMatter: [],
      //货源表
      subjectMatter1: [],
      //出入库委托书
      entryAndExitOrderVO: {},
      // 刷新失去委托编码时
      wtbm: "",
      isSeal: true, // 盖章标识
      rules: {
        wtbh: [
          {
            required: true,
            message: "请选择委托编号",
            trigger: ["change", "blur"],
          },
        ],
        apcc: [
          { required: true, message: "请填写", trigger: ["change", "blur"] },
          // { validator: checkCC, trigger: ['change', 'blur'] }
        ],
        jyfx: [
          { required: true, message: "请选择", trigger: ["change", "blur"] },
        ],
        jyrq: [
          {
            required: true,
            message: "请选择交易时间",
            trigger: ["change", "blur"],
          },
          { validator: checkinput, trigger: ["change", "blur"] },
        ],
        jyfs: [{ required: true, message: "请选择", trigger: "change" }],
        jysj: [
          { required: true, message: "请选择", trigger: "change" },
          { validator: checkinput, trigger: ["change", "blur"] },
        ],
        sxfl: [
          { required: true, message: "请填写", trigger: ["change", "blur"] },
          { validator: checkmoney, trigger: ["change", "blur"] },
        ],
        jybzj: [
          { required: true, message: "请填写", trigger: ["change", "blur"] },
          { validator: checkmoney, trigger: ["change", "blur"] },
        ],
        lybzj: [
          { required: true, message: "请填写", trigger: ["change", "blur"] },
          { validator: checkmoney, trigger: ["change", "blur"] },
        ],
      },
    };
  },
  created() {
    // type 0查看 1编辑  2盖章
    // console.log(this.$route.query, 'this.$route.query')
    let { state, data, ccbm, type } = this.$route.query;
    this.ccbm = ccbm;
    this.isAdd = state == 0; //新增
    this.isView = type == "show"; //查看
    this.isUpdate = type == "update"; //编辑
    this.isExport = type == "export"; //导出

    if (this.isAdd) {
      this.selectCode();
      this.isDisabled = false;
    }
    this.selectwtList1();
    // 合并日期和时间
    let dateTimeString = `${data.jyrq} ${data.jysj}`;
    let timestamp = new Date(dateTimeString).getTime()-1800000;
    let nowtime = new Date().getTime();
    if (nowtime > timestamp) {
      this.toHidden = false
    }
    if (!this.isAdd) {
      this.isDisabled = true;
      this.dform.wtfmc = data.wtfmc;
      this.dform.jysj = data.jysj;

      this.getDetail(ccbm);
      // 根据委托编码查询标的信息-入库安排
    }
    // 获取当前日期并赋值给交易日期，
    // this.dform.jyrq = dateFormat(new Date())
    // console.log(dateFormat(new Date()),'dateFormat(new Date())',this.dform.jyrq,'this.dform.jyrq')
    // 获取委托编码
    // this.selectwtList1()
  },
  methods: {
    // 导出
    exportFile() {
      // 获取要复制的元素
      // let src_ul = document.getElementById("transactionHandling");
      // console.log(src_ul);
      // // 复制 true深度克隆，可以克隆结构和内容
      // let copy_ul = src_ul.cloneNode(true);
      // console.log(copy_ul);
      // copy_ul.style.margin = "40";
      // htmlToPdf.downloadPDF(src_ul, "退还保证金通知单.pdf");
    },
    // 底价金额校验

    checkFloorPrice(value) {
      console.log(value, "value");
      if (!/^\d{1,10}(\.\d{1,2})?$/.test(value)) {
        // 保留两位小数
        new Error("请输入数字,保留两位小数");
      }
    },
    sxflChange(val) {
      if (val && this.dform.sl) {
        // console.log(this.dform.sl, val);
        this.dform.sxf = val * Number(this.dform.sl);
        // console.log(this.dform.sxf);
      }
    },
    async handleCheckCC(val) {
      let reg = /第(.*)场/;
      let cnCode = val.replace(reg, "$1");
      cnCode = cnCode == "十" ? "一十" : cnCode;
      cnCode = cnCode == "十万" ? "一十万" : cnCode;
      // console.log("cnCode", cnCode);
      let code = ChineseToNumber(cnCode);
      // console.log("code", code);
      let res = await valiCode({ ccbm: this.ccbm, cccode: code });
      return res.data;
    },
    checkmoney(rule, value, callback) {
      if (!value) {
        if (this.saveClose) {
          callback(new Error("请输入"));
        } else {
          callback();
        }
      } else {
        if (!/^\d{1,10}(\.\d{1,2})?$/.test(value)) {
          // 保留两位小数
          callback(new Error("限数字,限 10 字符"));
        } else {
          callback();
        }
      }
    },
    checkmoneyzxf(rule, value, callback) {
      if (!value) {
        if (this.saveClose) {
          callback(new Error("请输入"));
        } else {
          callback();
        }
      } else {
        if (!/^\d{1,2}(\.\d{1,2})?$/.test(value)) {
          callback(new Error("限数字,最多保留2位小数"));
        } else {
          callback();
        }
      }
    },
    /* checkinput (rule, value, callback) {
      if (!value) {
        if (this.saveClose) {
          callback(new Error("请输入"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    }, */
    async getDetail(ccbm) {
      let res = await selectCC(ccbm);
      if (res.infoCode == 200) {
        // console.log(res, '详情')
        this.wtbm = res.data.deatils[0].wtbm;
        this.entryAndExitOrderVO = res.data.entryAndExitOrderVO;
        // 获取标的信息
        getBdInfo(res.data.deatils[0].wtbm)
          .then((res) => {
            this.subjectMatter = res.data.apList ? res.data.apList : []; // 标的-入库安排数据
            this.subjectMatter1 = res.data.hyList ? res.data.hyList : []; //货源表
            if (res.data.hyList && res.data.hyList.length > 0) {
              this.zlzbPz = res.data.hyList[0].detailList[0].pzmc;
            }
            // 章的显示
            if (this.entryAndExitOrderVO.jydjqz) {
              if (this.entryAndExitOrderVO.jydjqz.indexOf("newSeal") == -1) {
                if (this.subjectMatter.length > 0) {
                  this.$nextTick(() => {
                    this.$ShowSeal("tradingFloorPrice", 1);
                    this.$SetSealData(
                      this.entryAndExitOrderVO.jydjqz,
                      0,
                      "lingdao",
                      1
                    );
                  });
                }
                if (this.subjectMatter1.length > 0) {
                  this.$nextTick(() => {
                    this.$ShowSeal("tradingFloorPrice1", 1);
                    this.$SetSealData(
                      this.entryAndExitOrderVO.jydjqz,
                      0,
                      "lingdao",
                      1
                    );
                  });
                }
              } else {
                if (this.subjectMatter.length > 0) {
                  this.$nextTick(() => {
                    this.$signatureOnload(
                      "tradingFloorPrice",
                      this.entryAndExitOrderVO.jydjqz.substring(7)
                    );
                  });
                }
                if (this.subjectMatter1.length > 0) {
                  this.$nextTick(() => {
                    this.$signatureOnload(
                      "tradingFloorPrice1",
                      this.entryAndExitOrderVO.jydjqz.substring(7)
                    );
                  });
                }
              }
            }
          })
          .catch((err) => {
            console.log(err);
          });

        // // 章的显示
        // if (this.entryAndExitOrderVO.jydjqz) {
        //   if (this.entryAndExitOrderVO.jydjqz.indexOf("newSeal") == -1) {
        //     this.$nextTick(() => {
        //       this.$ShowSeal("tradingFloorPrice", 1);
        //       this.$SetSealData(
        //         this.entryAndExitOrderVO.jydjqz,
        //         0,
        //         "lingdao",
        //         1
        //       );
        //     });
        //     this.$nextTick(() => {
        //       this.$ShowSeal("tradingFloorPrice1", 1);
        //       this.$SetSealData(
        //         this.entryAndExitOrderVO.jydjqz,
        //         0,
        //         "lingdao",
        //         1
        //       );
        //     });
        //   } else {
        //     this.$nextTick(() => {
        //       this.$signatureOnload(
        //         "tradingFloorPrice",
        //         this.entryAndExitOrderVO.jydjqz.substring(7)
        //       );
        //     });
        //     this.$nextTick(() => {
        //       this.$signatureOnload(
        //         "tradingFloorPrice1",
        //         this.entryAndExitOrderVO.jydjqz.substring(7)
        //       );
        //     });
        //   }
        // }

        this.dform = {
          ...this.entryAndExitOrderVO,
          ...res.data.siteVO,
          pz: this.entryAndExitOrderVO.pzmc,
        };
        this.dform.jyfs = Number(res.data.siteVO.jyfs);
        if (res.data.siteVO.jyfx == "0,1") {
          this.dform.jyfx = ["采购", "销售"];
        } else if (res.data.siteVO.jyfx == "0") {
          this.dform.jyfx = ["采购"];
        } else {
          this.dform.jyfx = ["销售"];
        }

        // 对手续费率、交易保证金、履约保证金格式化 jybzj lybzj
        this.dform.sxfl =
          String(this.dform.sxfl).indexOf(".") > -1
            ? parseFloat(this.dform.sxfl).toFixed(2)
            : this.dform.sxfl;
        this.dform.jybzj =
          String(this.dform.jybzj).indexOf(".") > -1
            ? parseFloat(this.dform.jybzj).toFixed(2)
            : this.dform.jybzj;
        this.dform.lybzj =
          String(this.dform.lybzj).indexOf(".") > -1
            ? parseFloat(this.dform.lybzj).toFixed(2)
            : this.dform.lybzj;
        this.getSL();
      }
    },

    // 编辑底价提交保存update
    // 首先判断底价是否全部填写，然后再根据type判断保存提交，差异化data参数
    handleUpdate(type) {
      let isPass = false;
      let isPass1 = false;
      try {
        this.subjectMatter.forEach((item, index) => {
          // console.log(item, 'item')
          if (item.detailList) {
            item.detailList.forEach((ele) => {
              // console.log(ele, 'ele')
              if (!ele.jydj) {
                // console.log(ele.jydj, 'jydj')
                // console.log(ele, 'ele 对应仓号')
                throw new Error(
                  "标的" + ele.bdh + ", 仓号" + ele.ccdd + ",交易底价未填写"
                );
              } else {
                // console.log('交易底价都已填写！')

                isPass = true;
              }
            });
          }
        });
      } catch (e) {
        this.$message.warning(e.message);
        isPass = false;
      }
      try {
        this.subjectMatter1.forEach((item, index) => {
          // console.log(item, 'item')
          if (item.detailList) {
            item.detailList.forEach((ele) => {
              // console.log(ele, 'ele')
              if (!ele.jydj) {
                // console.log(ele.jydj, 'jydj')
                // console.log(ele, 'ele 对应仓号')
                throw new Error(
                  "标的" + ele.bdh + ", 仓号" + ele.ccdd + ",交易底价未填写"
                );
              } else {
                // console.log('交易底价都已填写！')

                isPass1 = true;
              }
            });
          }
        });
      } catch (e) {
        this.$message.warning(e.message);
        isPass1 = false;
      }
      console.log(isPass, isPass1, "isPass");
      if (this.subjectMatter.length < 1) {
        isPass = true;
      }
      if (this.subjectMatter1.length < 1) {
        isPass1 = true;
      }
      if (isPass && isPass1) {
        if (type == "待提交") {
          this.entryAndExitOrderVO.apzt = "91000001";
          this.entryAndExitOrderVO.apztmc = "待提交";
        }
        if (type == "待盖章") {
          this.entryAndExitOrderVO.apzt = "91000300";
          this.entryAndExitOrderVO.apztmc = "待盖章";
        }
        this.dform.apList = this.subjectMatter;
        this.dform.hyList = this.subjectMatter1;
        this.dform.entryAndExitOrderVO = this.entryAndExitOrderVO;
        this.dform.jyfxmc =
          this.dform.jyfx && this.dform.jyfx.length > 1
            ? "采购,销售"
            : this.dform.jyfx.toString();
        this.dform.jyfx =
          this.dform.jyfx && this.dform.jyfx.length > 1
            ? "0,1"
            : this.jyfxArr[this.dform.jyfx.toString()];
        updatePrice(this.dform).then((res) => {
          // console.log(res, 'res-updatePrice')
          if (type == "待提交") {
            this.$message.success("保存成功");
          }
          if (type == "待盖章") {
            this.$message.success("提交成功");
          }

          this.$router.push("/transaction/tradingFloorPrice/index");
        });
      }

      this.saveClose = false;
    },
    handleSubmit(type, zt) {},
    async insert() {
      let res = await insertCC(this.dform);
      if (res.infoCode == 200) {
        this.$message.success(this.msg);
        this.$router.push("index");
      } else {
        this.$message.error(res.msg);
      }
    },
    async update() {
      console.log(this.dform, "this.dform");
      let res = await updateCC(this.dform);
      if (res.infoCode == 200) {
        this.$message.success(this.msg);
        this.$router.push("index");
      } else {
        this.$message.error(res.msg);
      }
    },
    // 生成场次数字并转成中文
    async selectCode() {
      let res = await selectCode();
      console.log(res, "场次");
      let code = res.data;
      let codeCn = NumberToChinese(code);
      this.dform.cccode = code;
      this.dform.apcc = `第${codeCn}场`;
    },
    getSL() {
      this.dform.sl = this.tableData.reduce((a, d) => a + +d.sl, 0) || 0;
      this.dform.sl = Math.floor(this.dform.sl * 100) / 100;
    },
    async handleWtChange(val) {
      if (val == "") {
        this.tableData = [];
        return;
      }
      getBdInfo(val)
        .then((res) => {
          console.log(res, "标的信息");
          // 标的信息-入库安排
          this.subjectMatter = res.data.apList ? res.data.apList : []; // 标的-入库安排数据
          this.subjectMatter1 = res.data.hyList ? res.data.hyList : []; //货源表
          if (res.data.hyList && res.data.hyList.length > 0) {
            this.zlzbPz = res.data.hyList[0].detailList[0].pzmc;
          }
          console.log(this.subjectMatter, "dwmc");
          let cur = res.data.entryAndExitOrderVO;
          this.dform.wtfmc = cur.wtfmc;
          this.dform.jyrq = cur.jyrq;
          this.dform.jyfx = cur.jyfx;

          if (cur.jyfs) {
            this.dform.jyfs = Number(cur.jyfs);
          }
          if (cur.jyfx) {
            this.dform.jyfx = Number(cur.jyfx);
          }
          this.dform = {
            ...this.dform,
            pz: cur.pzmc,
            jyrq: cur.jyrq,
            jylx: cur.jylx,
            // wtbm: cur.wtbm,
            wtbh: cur.wtbh,
            wtfbm: cur.wtfbm,
          };
          console.log(this.dform, "dform----");
        })
        .catch((err) => {
          console.log(err);
        });
      /* let res = await selectEntryAndExitOrderBySite(val);
      this.subjectMatter = res.data.apList // 标的-入库安排数据
      let cur = res.data.entryAndExitOrderVO;
      let details = res.data.deatils;
      this.dform.orderVO = cur;
      this.tableData = details;
      this.dform.wtfmc = cur.wtfmc
      this.dform.jyrq = cur.jyrq
      this.dform.jyfx = cur.jyfx
      this.dform = {
        ...this.dform,
        // pz: cur.pzmc,
        jyrq: cur.jyrq,
        jylx: cur.jylx,
        wtbh: cur.wtbh,
        wtbm: cur.wtbm,
        wtfbm:cur.wtfbm 
      }; */
      this.getSL();
    },
    selectwtList1() {
      selectEntryAndExitOrder().then((res) => {
        // console.log(res, '委托编码')
        if (res.infoCode == 200) {
          // console.log(res,'委托编码列表')
          this.wtbhList = res.data;
        }
      });
    },
    // 驳回
    doreject() {
      this.$confirm("是否确认驳回？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.entryAndExitOrderVO.apzt = "91000103";
          this.entryAndExitOrderVO.apztmc = "签章驳回";
          let data = {
            wtbm: this.$route.query.data.wtbm
              ? this.$route.query.data.wtbm
              : this.entryAndExitOrderVO.wtbm,
            apzt: "91000103",
            apztmc: "签章驳回",
          };
          updatePrice2NextNode(data)
            .then((res) => {
              this.$message({
                type: "info",
                message: "已成功驳回",
              });
              this.$router.push({
                path: "/transaction/tradingFloorPrice/index",
              });
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消驳回",
          });
        });
    },
    signature() {
      this.$confirm("是否确认盖章？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.isSeal = false;
          this.$refs.sealDialog.getData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消盖章",
          });
        });
    },
    sealError() {
      this.isSeal = true;
    },
    sealTodo(sealData) {
      // var sh = this.$GetSealData("websignApply");91000600: "已确认",
      let sh = `newSeal${sealData}`;
      var id = "";
      this.entryAndExitOrderVO.apzt = "91000600";
      this.entryAndExitOrderVO.apztmc = "已确认";
      this.entryAndExitOrderVO.jydjqz = sh;
      console.log(this.entryAndExitOrderVO, "this.entryAndExitOrderVO");
      let data = {
        wtbm: this.$route.query.data.wtbm
          ? this.$route.query.data.wtbm
          : this.entryAndExitOrderVO.wtbm,
        ...this.entryAndExitOrderVO,
      };
      updatePrice2NextNode(data)
        .then((res) => {
          // console.log(res,'res-updatePrice2NextNode')
          this.$message({
            type: "success",
            message: "盖章成功",
          });
          this.$router.push({ path: "/transaction/tradingFloorPrice/index" });
          // if(res)
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 通过查询列表数据，并拿到委托编号
    getWtbh() {
      let pageData = {
        pageNum: 1,
        pageSize: 100,
        total: 0,
      };

      selectList(pageData).then((res) => {
        // console.log(res, 'res')
        res.data.items.forEach((item) => {
          // console.log(item.wtbh, 'wtbh')
          /*  this.wtbhList.push({
             wtbh:item.wtbh,
             wtbm:item.wtbm
           }) */
        });
        // console.log(this.wtbhList,'list')
      });
    },
    showInfo(row) {
      this.$router.push({
        path: "transObj",
        query: {
          apbm: row.apbm,
          wtbm: row.wtbm,
          bdbm: row.bdbm,
          type: "show",
        },
      });
    },
    delInfo(index) {
      if (this.tableData.length == 1) {
        this.$message.warning("至少保留一条标的信息!");
        return;
      }
      this.$confirm("此操作将删除当前信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData.splice(index, 1);
          this.getSL();
        })
        .catch((err) => {});
    },
    /* rules (label, isChange) {
      return {
        required: this.saveClose,
        message: isChange ? "请选择" + label : "请输入" + label,
        trigger: ["change", "blur"],
      };
    }, */
  },
};
</script>
<style lang="scss" scoped>
.el-table /deep/ .cell.require_star:before {
  content: "*";
  color: red;
}
.detail-content {
  padding: 20px;
}
.w90 {
  width: 90% !important;
}
.sub-title {
  width: 98%;
  text-align: center;
  font-size: 1em;
  line-height: 2.5em;
  color: #fff;
  background-color: #49a2fa;
  box-sizing: border-box;
  margin-bottom: 20px;
  position: relative;
}
.btn-footer {
  text-align: center;
  margin: 20px;
}
.tableheader {
  padding: 10px;
  border: 1px solid #ccc;
  font-weight: 500;
  margin-top: 10px;
  span {
    padding-left: 15px;
  }
}
</style>
<style>
#tradingFloorPrice {
  position: relative;
}
#tradingFloorPrice > div {
  top: -19px !important;
  left: 270px !important;
}
#tradingFloorPrice1 {
  position: relative;
}
#tradingFloorPrice1 > div {
  top: -10px !important;
  left: 270px !important;
}
</style>
