<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="wtbh">
            <el-select
              v-model="dform.wtbh"
              clearable
              class="w90"
              @change="handleWtChange"
              :disabled="isView"
            >
              <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="isView"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="场次编号:" prop="ccbh">
            <el-input
              v-model="dform.ccbh"
              clearable
              class="w90"
              :disabled="isView"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易方向:" prop="jyfx">
            <!-- <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-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="交易方式:" prop="jyfs">
            <!-- <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-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="isTimeDisabled"
              placeholder="任意时间点"
            >
            </el-time-picker>
          </el-form-item>
        </el-col>
        <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="isView"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="履约保证金(元/吨):" prop="lybzj">
            <el-input
              clearable
              class="w90"
              :disabled="isView"
              v-model="dform.lybzj"
              v-bind:value="
                String(dform.lybzj).indexOf('.') > -1
                  ? Number(dform.lybzj).toFixed(2)
                  : dform.lybzj
              "
              oninput="this.value= this.value.replace(/[^\d.]/g, '').replace(/(\d+\.\d{1,3})\.?\d*/, '$1')"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易保证金(元/吨):" prop="jybzj">
            <!-- <el-input v-model="dform.jybzj" clearable class="w90" :disabled="isView"></el-input> -->
            <el-input
              clearable
              class="w90"
              :disabled="isView"
              v-bind:value="
                String(dform.jybzj).indexOf('.') > -1
                  ? Number(dform.jybzj).toFixed(2)
                  : dform.jybzj
              "
              v-model="dform.jybzj"
              oninput="this.value= this.value.replace(/[^\d.]/g, '').replace(/(\d+\.\d{1,3})\.?\d*/, '$1')"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <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
          v-show="$route.query.type != 'show'"
          align="center"
          type="index"
          label="标的"
          width="70"
        >
        </el-table-column>
        <el-table-column
          v-show="$route.query.type == 'show'"
          align="center"
          prop="bdh"
          label="标的"
          width="120"
        >
        </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
          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="sfkc"
          align="center"
        ></el-table-column>
      </el-table>
    </div>
    <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
          v-show="$route.query.type != 'show'"
          align="center"
          type="index"
          label="标的"
          width="70"
        >
        </el-table-column>
        <el-table-column
          v-show="$route.query.type != 'show'"
          align="center"
          prop="bdh"
          label="标的"
          width="100"
        >
        </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>
    </div>
    <el-row class="btn-footer">
      <el-button
        type="primary"
        icon="iconfont iconbaocun"
        v-if="isAdd"
        @click="handleSubmit('已发布')"
        >提交</el-button
      >
      <!-- <el-button
        type="primary"
        icon="iconfont iconbaocun"
        v-if="isUpdate"
        @click="handleSubmit('编辑')"
        >保存</el-button> -->
      <!-- <el-button
        type="primary"
        icon="iconfont iconfabu"
        v-if="!isView"
        @click="handleSubmit('已发布')"
        >发布</el-button
      > -->
      <el-button
        type="primary"
        icon="iconfont iconfanhui"
        @click="$router.push('index')"
        >返回</el-button
      >
    </el-row>
  </div>
</template>
<script>
import {
  selectEntryAndExitOrder,
  selectCode,
  insertCC,
  updateCC,
  selectCC,
  valiCode,
  selectList,
  getBdInfo,
} from "@/api/transaction/trading/fieldInfo/index";
import { NumberToChinese, ChineseToNumber } from "@/utils/num2cn";
const log = console.log.bind(console);
export default {
  data() {
    var checkCC = (rule, val, cb) => {
      this.dform.apcc = val.trim();
      if (!val) {
        cb(new Error("请输入场次"));
      } else {
        cb();
      }
    };
    var checkinput = (rule, value, callback) => {
      if (!value) {
        if (this.saveClose) {
          callback(new Error("请输入"));
        } else {
          callback();
        }
      } else {
        callback();
      }
    };
    var checkNum = (rule, value, callback) => {
      if (value == 0.0) {
        callback(new Error("请填写"));
      } else {
        callback();
      }
    };
    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();
        }
      }
    };
    return {
      dform: {
        wtbh: "",
        wtbm: "",
        apcc: "",
        ccbh: "",
        jyfx: null,
        jyfs: null,
        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,
      isTimeDisabled: false,
      // 标的数据
      subjectMatter: [],
      subjectMatter1: [],
      zlzbPz: "小麦",
      rules: {
        wtbh: [
          {
            required: true,
            message: "请选择委托编号",
            trigger: ["change", "blur"],
          },
        ],
        apcc: [
          { required: true, message: "请填写", trigger: ["change", "blur"] },
          // { validator: checkCC, trigger: ['change', 'blur'] }
        ],
        ccbh: [
          { required: true, message: "请填写", trigger: ["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() {
    // 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";
    // console.log(data.wtfmc,this.$route.query.data.wtfmc)

    if (this.isAdd) {
      // this.selectCode();
      this.isDisabled = true;
    }
    this.selectwtBhList();
    if (!this.isAdd) {
      this.isDisabled = true;
      this.isTimeDisabled = true;
      this.dform.wtfmc = data.wtfmc;
      this.getDetail(ccbm);
      this.handleWtChange(this.$route.query.data.wtbm);
      // getBdInfo(this.$route.query.data.wtbm)
      //   .then((res) => {
      //     console.log(res.data.apList, "apList");
      //     this.subjectMatter = res.data.apList ? res.data.apList : []; // 标的-入库安排数据
      //     this.subjectMatter1 = res.data.hyList ? res.data.hyList : []; //货源表
      //     if (res.data.hyList.length > 0) {
      //       this.zlzbPz = res.data.hyList[0].pzmc;
      //     }
      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });
    }
    // 获取当前日期并赋值给交易日期，
    // this.dform.jyrq = dateFormat(new Date())
    // console.log(dateFormat(new Date()),'dateFormat(new Date())',this.dform.jyrq,'this.dform.jyrq')
    // 获取委托编码
    // this.selectwtBhList()
  },
  methods: {
    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 checkCC(rule, val, cb) {
    //   this.dform.apcc = val.trim();
    //   if (!val) {
    //     cb(new Error("请输入场次"));
    //   } else {
    //     cb();
    //   }
    //   // let reg = /第[一二三四五六七八九]?千?[零一二三四五六七八九]?百?[零一二三四五六七八九]?十?[一二三四五六七八九]?万?[零一二三四五六七八九]?千?[零一二三四五六七八九]?百?[零一二三四五六七八九]?十?[一二三四五六七八九]?场/;
    //   // if (reg.test(val)) {
    //   //   let canUse = await this.handleCheckCC(val);
    //   //   if (canUse) {
    //   //     cb();
    //   //   } else if (canUse == "") {
    //   //     cb(new Error("获取场次是否被使用失败"));
    //   //   } else {
    //   //     cb(new Error("当前输入场次已经被使用"));
    //   //   }
    //   // } else {
    //   //   cb(new Error("请输入正确的场次"));
    //   // }
    // },
    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 (!((/^[0-9]\d{0,10}(\.\d{1,2})?$/).test(value))) {
          // 保留两位小数
          callback(new Error("限数字,最多保留2位小数"));
        } else {
          callback();
        }
      }
    }, */
    checkmoneyzxf(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("限数字,最多保留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) {
        let ee = res.data.entryAndExitOrderVO;
        console.log(res, "获取信息");
        this.dform = {
          ...ee,
          ...res.data.siteVO,
          pz: ee.pzmc,
        };
        this.dform.jyfs = Number(res.data.siteVO.jyfs);
        // if (res.data.siteVO.jyfx) {
        //   this.dform.jyfx = res.data.siteVO.jyfxmc.split(",");
        // }
        // if (res.data.siteVO.jyfx) {
        //   this.dform.jyfx = res.data.siteVO.jyfxmc.split(",");
        // }
        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();
      }
    },
    handleSubmit(zt) {
      this.saveClose = false;
      this.$refs.dform.validate((valid) => {
        if (valid) {
          let isValidSelected = this.wtOpts.find(
            (d) => d.wtbh == this.dform.wtbh
          );
          /* if (!isValidSelected) {
            this.dform.wtbh = "";
            this.$message.warning(
              "当前委托编号已经发布, 请换一个新的委托编号再提交!"
            );
            return;
          } */
          this.dform.cczt = zt;
          console.log(zt, "zt");
          this.dform.marks = JSON.stringify(this.tableData);
          this.msg = zt == "待发布" ? "保存成功!" : "提交成功!";
          this.dform.tradeMarkVOList = this.tableData;
          if (this.isAdd) {
            this.insert();
          }
          if (this.isUpdate) {
            this.update();
          }
        } else {
          this.$message({
            message: "请正确填写信息",
            type: "warning",
          });
        }
      });

      // if (zt == "待发布") {
      //   // console.log(this.dform,'dform')
      //   this.saveClose = false;
      //   this.$refs.dform.validate((valid) => {
      //     if (valid) {
      //       let isValidSelected = this.wtOpts.find(
      //         (d) => d.wtbh == this.dform.wtbh
      //       );
      //       /* if (!isValidSelected) {
      //         this.dform.wtbh = "";
      //         this.$message.warning(
      //           "当前委托编号已经发布, 请换一个新的委托编号再提交!"
      //         );
      //         return;
      //       } */
      //       // this.dform.cczt = zt;
      //       this.dform.marks = JSON.stringify(this.tableData);
      //       this.msg = zt == "待发布" ? "保存成功!" : "发布成功!";
      //       this.dform.tradeMarkVOList = this.tableData;
      //       if (this.isAdd) {
      //         this.insert();
      //       } else {
      //         // this.update();
      //       }
      //     }
      //   });
      // } else {
      //   this.saveClose = true;
      //   this.$refs.dform.validate((valid) => {
      //     if (valid) {
      //       let isValidSelected = this.wtOpts.find(
      //         (d) => d.wtbh == this.dform.wtbh
      //       );
      //       if (!isValidSelected) {
      //         this.dform.wtbh = "";
      //         this.$message.warning(
      //           "当前委托编号可以已经发布, 请换一个新的委托编号再提交!"
      //         );
      //         return;
      //       }
      //       this.dform.cczt = zt;
      //       this.dform.marks = JSON.stringify(this.tableData);
      //       this.msg = zt == "待发布" ? "保存成功!" : "发布成功!";
      //       this.dform.tradeMarkVOList = this.tableData;
      //       if (this.isAdd) {
      //         this.insert();
      //       } else {
      //         this.update();
      //       }
      //     } else {
      //       this.$message.warning("请先完成必填项!");
      //     }
      //   });
      // }
    },
    async insert() {
      console.log(this.dform.jyfx, "this.dform.jyfx");
      // console.log(this.dform, 'this.dform')
      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()];
      this.dform.apcc = this.dform.apcc.trim()
      let res = await insertCC(this.dform);
      if (res.infoCode == 200) {
        console.log(res, "提交");
        this.$message.success(this.msg);
        this.$router.push("index");
      } else {
        this.$message.error(res.msg);
      }
    },
    async update() {
      // console.log(this.dform,'this.dform')
      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()];
      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;
      }
      console.log(val, "val");
      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 = cur.jyfxmc.split(",");
          }
          console.log(this.dform.jyfx, "this.dform.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);
       let cur = res.data.entryAndExitOrderVO;
       let details = res.data.deatils;
       this.dform.orderVO = cur;
       // this.tableData = details;
       this.dform = {
         ...this.dform,
         pz: cur.pzmc,
         jyrq: cur.jyrq,
         wtbm: cur.wtbm,
         jylx: cur.jylx,
         wtbh: cur.wtbh,
       }; */
      this.getSL();
    },
    selectwtBhList() {
      selectEntryAndExitOrder().then((res) => {
        if (res.infoCode == 200) {
          console.log(res, "wtbh");
          this.wtbhList = res.data;
        }
      });
    },
    // 通过查询列表数据，并拿到委托编号
    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>
.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>
