<template>
  <div class="detail-content">
    <el-form ref="dform" :model="dform" label-width="15em">
      <el-row>
        <el-col :span="12"
          ><el-form-item
            label="标的号:"
            prop="tradeMarkVO.bdh"
            :rules="rules('标的号')"
          >
            <el-input
              v-model="dform.tradeMarkVO.bdh"
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item
            label="委托方:"
            prop="entryAndExitOrderVO.wtfmc"
            :rules="rules('委托方')"
          >
            <el-input
              v-model="dform.entryAndExitOrderVO.wtfmc"
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="委托收购库点编码:">
            <el-input
              v-model="dform.warehousingArrangementVO.dwbm"
              clearable
              :disabled="isView"
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="委托收购库点编码:">
            <el-input
              v-model="dform.warehousingArrangementVO.dwbm"
              clearable
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="委托收购库点:">
            <el-input
              v-model="dform.warehousingArrangementVO.dwmc"
              clearable
              disabled
              class="w90"
            ></el-input>
            <!-- <el-select
              v-model="dform.tradeMarkVO.sgkdmc"
              clearable
              class="w90"
              :disabled="isView"
              filterable
            >
              <el-option
                v-for="(item, index) in sinkpointList"
                :key="index"
                :label="item.kdmc"
                :value="item.kddm"
              ></el-option>
            </el-select>-->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际存储库点编码:">
            <el-input
              v-model="dform.warehousingArrangementVO.dwbm"
              clearable
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实际存储库点:">
            <el-input
              v-model="dform.warehousingArrangementVO.dwmc"
              clearable
              disabled
              class="w90"
            ></el-input>
            <!-- <el-select v-model="dform.tradeMarkVO.cckdmc" clearable disabled class="w90" filterable>
              <el-option
                v-for="(item, index) in sinkpointList"
                :key="index"
                :label="item.kdmc"
                :value="item.kddm"
              ></el-option>
            </el-select>-->
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item
            label="仓号:"
            prop="warehousingArrangementVO.ccdd"
            :rules="rules('仓号')"
          >
            <el-input
              v-model="dform.warehousingArrangementVO.ccdd"
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item
            label="产地:"
            prop="warehousingArrangementVO.cd"
            :rules="rules('产地')"
          >
            <el-input
              v-model="dform.warehousingArrangementVO.cd"
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item
            label="生产年限:"
            prop="warehousingArrangementVO.scnd"
            :rules="rules('生产年限')"
          >
            <el-input
              v-model="dform.warehousingArrangementVO.scnd"
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item
            label="品种:"
            prop="warehousingArrangementVO.pzmc"
            :rules="rules('品种')"
          >
            <el-input
              v-model="dform.warehousingArrangementVO.pzmc"
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="收购等级:">
            <el-select
              v-model="dform.warehousingArrangementVO.sgdj"
              clearable
              :disabled="isView"
              class="w90"
            >
              <el-option
                v-for="(item, index) in grades"
                :key="index"
                :label="item.dictName"
                :value="item.dictCode"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="所在货位混合扦样等级:">
            <el-select
              v-model="dform.tradeMarkVO.qydj"
              clearable
              :disabled="isView"
              class="w90"
            >
              <el-option
                v-for="(item, index) in grades"
                :key="index"
                :label="item.dictName"
                :value="item.dictCode"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="数量:">
            <el-input
              v-model="dform.tradeMarkVO.sl"
              disabled
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item
            label="起报价:"
            prop="tradeMarkVO.qbj"
            :rules="rules('起报价')"
          >
            <el-input
              v-model="dform.tradeMarkVO.qbj"
              clearable
              :disabled="isView"
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="sub-title">{{ type }}品质情况</el-col>
        <div class="qwrap">
          <el-col :span="12" v-for="(item, index) in qtypes[type]" :key="index"
            ><el-form-item :label="item + ':'">
              <el-input
                v-model="dform.warehousingArrangementVO[qnames(item)]"
                disabled
                class="w90"
              ></el-input>
            </el-form-item>
          </el-col>
        </div>
        <el-col :span="12"
          ><el-form-item label="承储库日正常出库能力:">
            <el-input
              v-model="dform.warehousingArrangementVO.cknl"
              clearable
              :disabled="isView"
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="常用出库方式(铁路/公路/水路):">
            <el-checkbox-group
              v-model="dform.warehousingArrangementVO.cyckfs"
              :disabled="isView"
            >
              <el-checkbox label="0">铁路</el-checkbox>
              <el-checkbox label="1">公路</el-checkbox>
              <el-checkbox label="2">水路</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item
            label="是否具备40吨以上大型运输车辆装车计量能力(是/否):"
            class="label-hide"
          >
            <el-radio-group
              v-model="dform.warehousingArrangementVO.sfjbnl"
              :disabled="isView"
            >
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="有无铁路专用线:">
            <el-radio-group
              v-model="dform.warehousingArrangementVO.ywtl"
              :disabled="isView"
            >
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="是否露天储存:">
            <el-radio-group
              v-model="dform.warehousingArrangementVO.sflt"
              :disabled="isView"
            >
              <el-radio :label="1">是</el-radio>
              <el-radio :label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="距最近车站\码头距离km:">
            <el-input
              v-model="dform.warehousingArrangementVO.jl"
              clearable
              :disabled="isView"
              class="w90"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"
          ><el-form-item label="储粮形态(包装/散装):">
            <el-radio-group
              v-model="dform.warehousingArrangementVO.clxsmc"
              :disabled="isView"
            >
              <el-radio label="包装">包装</el-radio>
              <el-radio label="散装">散装</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24"
          ><el-form-item label="备注:">
            <el-input
              v-model="dform.warehousingArrangementVO.remark"
              type="textarea"
              :disabled="isView"
              :autosize="{ minRows: 2 }"
              class="w95"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="btn-footer">
      <el-button
        type="primary"
        icon="iconfont iconbaocun"
        v-if="!isView"
        @click="handleSubmit('save')"
        >保存</el-button
      >
      <el-button
        type="primary"
        icon="iconfont icontijiao"
        v-if="!isView"
        @click="handleSubmit('submit')"
        >提交</el-button
      >
      <el-button
        type="primary"
        icon="iconfont iconfanhui"
        @click="$router.go(-1)"
        >返回</el-button
      >
    </el-row>
  </div>
</template>
<script>
import { sinkpointList, foodStocksList, findByGroupId } from "@/api/base";
import { qtypes, getType, qnames } from "@/utils/quality";
import {
  getScalePlanDetails,
  getTrademark,
  updateTrademark,
} from "@/api/transaction/trading/transactionSignupFood/index";
export default {
  data() {
    return {
      type: "小麦", // 当前品种类型, 归属qtypes里的
      qtypes: qtypes,
      dform: {
        wtsgkd: "",
        tradeMarkVO: {},
        warehousingArrangementVO: { cd: "北京", cyckfs: [] },
        entryAndExitOrderVO: {},
      },
      sinkpointList: [],
      isView: false,
      grades: [], //等级
    };
  },
  created() {
    let { type, apbm, wtbm, bdbm } = this.$route.query;
    this.isView = type == "show";
    this.isUpdate = type == "update";
    // this.scalePlanDetails(apbm, wtbm)
    this.getTrademark(bdbm);
    this.init();
  },
  methods: {
    async handleSubmit(type) {
      if (type != "save") {
        var isValid = true;
        await this.$refs.dform.validate((valid) => {
          isValid = valid;
        });
        if (!isValid) {
          this.$message.warning("请先完成必填项");
          return;
        }
      }
      let msg = (type == "save" ? "保存" : "提交") + "成功!";
      let status =
        type == "save"
          ? { ztmc: "待提交", zt: "91000001" }
          : { ztmc: "已提交", zt: "91000004" };
      this.dform.warehousingArrangementVO = {
        ...this.dform.warehousingArrangementVO,
        ...status,
      };
      let data = {
        warehousingArrangementVO: {
          ...this.dform.warehousingArrangementVO,
          cyckfs: this.dform.warehousingArrangementVO.cyckfs[0],
        },
        entryAndExitOrderVO: this.dform.entryAndExitOrderVO,
        tradeMarkVO: this.dform.tradeMarkVO,
      };
      let res = await updateTrademark(data);
      if (res.infoCode == 200) {
        this.$message.success(msg);
      } else {
        this.$message.error(res.msg);
      }
    },
    async foodStocksList(data) {
      let res = await foodStocksList(data);
      if (res.infoCode == 200 && res.data.length) {
        this.dform.warehousingArrangementVO.cd = res.data[0].lscd || "国产";
        this.$refs.dform.clearValidate();
      }
    },
    async getTrademark(bdbm) {
      let res = await getTrademark(bdbm);
      if (res.infoCode != 200) return;
      this.dform = {
        ...this.dform,
        ...res.data,
      };
      let cyckfs = res.data.warehousingArrangementVO.cyckfs;
      cyckfs = cyckfs
        ? String(res.data.warehousingArrangementVO.cyckfs).split(",")
        : [];
      this.dform.warehousingArrangementVO.cyckfs = cyckfs;

      let warehousVo = res.data.warehousingArrangementVO;
      // if (!warehousVo.cd) {
      // 	this.dform.warehousingArrangementVO.cd = '国产'
      // }
      // let _data = {
      // 	lspzmc: '玉米',
      // 	sscfmc: '仓号2',
      // }
      this.foodStocksList({
        lspzmc: warehousVo.pzmc,
        sscfmc: warehousVo.ccdd,
      });
      this.$refs.dform.clearValidate();
    },
    scalePlanDetails(apbm, wtbm) {
      getScalePlanDetails(apbm).then((res) => {
        if (res.infoCode == "200") {
          let resp = res.data;
          this.dform = resp;
          this.dform.cyckfs = String(resp.cyckfs).split(",");
          this.$refs.dform.clearValidate();
        }
      });
      // if (this.$route.query.type != 'updateSave') {
      // 	this.getDetailTable(apbm)
      // }
    },
    qnames(val) {
      return qnames(val);
    },
    async init() {
      let res = await sinkpointList();
      this.sinkpointList = res.data;
      let resp = await findByGroupId("LSDJ");
      this.grades = resp.data;
    },
    rules(label, isChange) {
      return {
        required: true,
        message: isChange ? "请选择" + label : "请输入" + label,
        trigger: ["change", "blur"],
      };
    },
  },
};
</script>
<style lang="scss" scoped>
.detail-content {
  padding: 20px;
}
.w90 {
  width: 90%;
}
.w95 {
  width: 95%;
}
.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;
}
.qwrap {
  width: 100%;
  overflow: hidden;
}
.btn-footer {
  text-align: center;
  margin: 20px;
}
</style>
