<template>
  <div class="detail-main" style="padding: 20px; width: 100%; margin: 0 auto">
    <h2
      style="
        font-weight: 600;
        font-size: 20px;
        text-align: center;
        margin-bottom: 10px;
      "
    >
      成交数据维护
    </h2>
    <el-form
      :inline="true"
      ref="detailForm"
      :model="detailForm"
      label-width="110px"
    >
      <el-row>
        <el-form-item label="省份">
          <!-- sfbm -->
          <!-- <el-input clearable v-model="detailForm.sfbm" style="width:170px"></el-input> -->
          <el-select
            :disabled="isView || isFrozen"
            clearable
            filterable
            v-model="detailForm.sfmc"
            style="width: 170px"
            @change="
              detailForm.sfbm = provinces.filter(
                (p) => p.countyName == $event
              )[0]['countyCode']
            "
          >
            <el-option
              v-for="(item, index) in provinces"
              :key="index"
              :value="item.countyName"
              :label="item.countyName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同号" :rules="rules('合同号')" prop="hth">
          <!-- htbm -->
          <el-input
            @input="hthchan"
            :disabled="isView"
            clearable
            v-model="detailForm.hth"
            style="width: 170px"
          ></el-input>
          <!-- <el-select
						:disabled="isView || isFrozen"
						filterable
						v-model="detailForm.hth"
						style="width:170px;"
						@change="detailForm.htbm = contractInformations.filter(p => p.htmc == $event)[0]['htbh']"
					>
						<el-option
							v-for="(item, index) in contractInformations"
							:key="index"
							:value="item.htmc"
							:label="item.htmc"
						></el-option>
          </el-select>-->
        </el-form-item>
        <el-form-item label="标的号">
          <!-- bdbm -->
          <el-input
            clearable
            :disabled="isView"
            v-model="detailForm.bdh"
            style="width: 170px"
          ></el-input>
          <!-- <el-select
						:disabled="isView || isFrozen"
						filterable
						v-model="detailForm.bdh"
						style="width:170px;"
						@change="detailForm.bdbm = transactions.filter(p => p.bdh == $event)[0]['bdbm']"
					>
						<el-option
							v-for="(item, index) in transactions"
							:key="index"
							:value="item.bdh"
							:label="item.bdh"
						></el-option>
          </el-select>-->
        </el-form-item>
        <el-form-item label="卖方承储库">
          <!-- <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.mfcckmc"
            style="width: 170px"
            @change="
              detailForm.mfcckbm = sinkPoints.filter(
                (p) => p.kdmc == $event
              )[0]['kddm']
            "
          >
            <el-option
              v-for="(item, index) in sinkPoints"
              :key="index"
              :value="item.kdmc"
              :label="item.kdmc"
            ></el-option>
          </el-select> -->
          <el-input
            clearable
            v-model="detailForm.mfcckmc"
            style="width: 170px"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="交货仓库">
          <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.jhckmc"
            style="width: 170px"
            @change="
              detailForm.jhckbm = sinkPoints.filter((p) => p.kdmc == $event)[0][
                'kddm'
              ];
              warehouseList(detailForm.jhckbm);
            "
          >
            <el-option
              v-for="(item, index) in sinkPoints"
              :key="index"
              :value="item.kdmc"
              :label="item.kdmc"
            ></el-option>
          </el-select>
          <!-- <el-input clearable v-model="detailForm.jhckmc" style="width:170px"></el-input> -->
        </el-form-item>
        <el-form-item label="仓号">
          <!-- <el-input clearable v-model="detailForm.chmc" style="width:170px"></el-input> -->
          <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.chmc"
            style="width: 170px"
            @change="
              detailForm.chbm = wareHouses.filter((p) => p.kdmc == $event)[0][
                'kddm'
              ];
              warehouseList(detailForm.jhckbm);
            "
          >
            <el-option
              v-for="(item, index) in wareHouses"
              :key="index"
              :value="item.cfmc"
              :label="item.cfmc"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产年份">
          <el-date-picker
            :disabled="isView || isFrozen"
            clearalbe
            v-model="detailForm.scnf"
            style="width: 170px"
            type="year"
            value-format="yyyy"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="是否露天">
          <!-- 0否 1是 -->
          <el-radio-group
            :disabled="isView || isFrozen"
            v-model="detailForm.sflt"
            style="width: 170px"
          >
            <el-radio label="0">否</el-radio>
            <el-radio label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="收购等级">
          <!-- sgdjbm -->
          <!-- <el-input clearable v-model="detailForm.sgdjmc" style="width:170px"></el-input> -->
          <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.sgdjbm"
            style="width: 170px"
            @change="
              detailForm.sgdjbm = lsdjs.filter((p) => p.dictName == $event)[0][
                'dictCode'
              ]
            "
          >
            <el-option
              v-for="(item, index) in lsdjs"
              :key="index"
              :value="item.dictCode"
              :label="item.dictName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="混合扦样等级">
          <!-- hhqydjbm -->
          <!-- <el-input clearable v-model="detailForm.hhqydjmc" style="width:170px"></el-input> -->
          <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.hhqydjbm"
            style="width: 170px"
            @change="
              detailForm.hhqydjbm = lsdjs.filter(
                (p) => p.dictName == $event
              )[0]['dictCode']
            "
          >
            <el-option
              v-for="(item, index) in lsdjs"
              :key="index"
              :value="item.dictCode"
              :label="item.dictName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成交数量" prop="cjsl">
          <el-input
            :disabled="isView || isFrozen"
            clearable
            v-model="detailForm.cjsl"
            style="width: 170px"
            oninput="this.value= this.value.replace(/[^\d.]/g, '').replace(/(\d+\.\d{1,3})\.?\d*/, '$1')"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="总成交数量"
          prop="zcjsl"
          :rules="rules('总成交数量')"
        >
          <el-input
            :disabled="isView || isFrozen"
            clearable
            v-model="detailForm.zcjsl"
            style="width: 170px"
            oninput="this.value= this.value.replace(/[^\d.]/g, '').replace(/(\d+\.\d{1,3})\.?\d*/, '$1')"
            @change="handleSum"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="粮食性质">
          <!-- lsxzbm -->
          <!-- <el-input clearable v-model="detailForm.lsxzmc" style="width:170px"></el-input> -->
          <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.lsxzbm"
            style="width: 170px"
            @change="
              detailForm.lsxzmc = lsxzs.filter((p) => p.dictName == $event)[0][
                'dictName'
              ]
            "
          >
            <el-option
              v-for="(item, index) in lsxzs"
              :key="index"
              :value="item.dictCode"
              :label="item.dictName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="粮食品种">
          <!-- lspzbm -->
          <!-- <el-input clearable v-model="detailForm.lspzmc" style="width:170px"></el-input> -->
          <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.lspzmc"
            style="width: 170px"
            @change="
              detailForm.lspzbm = lspzs.filter((p) => p.dictName == $event)[0][
                'dictCode'
              ]
            "
          >
            <el-option
              v-for="(item, index) in lspzs"
              :key="index"
              :value="item.dictName"
              :label="item.dictName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起拍价格">
          <el-input
            :disabled="isView || isFrozen"
            clearable
            v-model="detailForm.qpjg"
            style="width: 170px"
            oninput="this.value= this.value.replace(/[^\d.]/g, '').replace(/(\d+\.\d{1,3})\.?\d*/, '$1')"
          ></el-input>
        </el-form-item>
        <el-form-item label="成交单价" :rules="rules('成交单价')" prop="cjdj">
          <el-input
            :disabled="isView || isFrozen"
            v-model="detailForm.cjdj"
            oninput="this.value= this.value.replace(/[^\d.]/g, '').replace(/(\d+\.\d{1,3})\.?\d*/, '$1')"
            @change="handleSum"
            style="width: 170px"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="成交金额" :rules="rules('成交金额')" prop="cjje">
          <el-input
            disabled
            clearable
            v-model="detailForm.cjje"
            style="width: 170px"
            placeholder="总成交数 x 单价"
          ></el-input>
        </el-form-item>
        <el-form-item label="成交日期">
          <el-date-picker
            :disabled="isView || isFrozen"
            clearalbe
            v-model="detailForm.cjrq"
            style="width: 170px"
            type="date"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="付货款截止日期">
          <el-date-picker
            :disabled="isView"
            clearalbe
            v-model="detailForm.fhkjzrq"
            style="width: 170px"
            type="date"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="出库截止日期">
          <el-date-picker
            :disabled="isView"
            clearalbe
            v-model="detailForm.ckjzrq"
            style="width: 170px"
            type="date"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="成交单位" :rules="rules('成交单位')" prop="cjdwmc">
          <!-- cjdwbm -->
          <!-- <el-input clearable v-model="detailForm.cjdwmc" style="width:170px"></el-input> -->
          <el-select
            :disabled="isView || isFrozen"
            filterable
            v-model="detailForm.cjdwmc"
            style="width: 170px"
            @change="
              detailForm.cjdwbm = sinkPoints.filter((p) => p.kdmc == $event)[0][
                'kddm'
              ]
            "
          >
            <el-option
              v-for="(item, index) in sinkPoints"
              :key="index"
              :value="item.kdmc"
              :label="item.kdmc"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            :disabled="isView"
            clearable
            v-model="detailForm.remark"
            style="width: 170px"
          ></el-input>
        </el-form-item>
        <el-form-item label="IP地址及类型">
          <el-input
            :disabled="isView"
            clearable
            v-model="detailForm.dzjlx"
            style="width: 170px"
          ></el-input>
        </el-form-item>
        <el-form-item label="交易节编码">
          <el-input
            :disabled="isView"
            clearable
            v-model="detailForm.jyjbh"
            style="width: 170px"
          ></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="状态" :rules="rules('状态')" prop="ztmc">
          <el-select
            :disabled="isView || fronzenZt"
            filterable
            v-model="detailForm.ztmc"
            style="width: 170px"
            @change="
              detailForm.ztbm = zts.filter((p) => p.name == $event)[0]['code'];
              handleChangeZT($event);
            "
          >
            <el-option
              v-for="(item, index) in zts"
              :key="index"
              :value="item.name"
              :label="item.name"
              :disabled="item.name == '违规清除'"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="横向得分">
          <el-input
            disabled
            v-model="detailForm.hxdf"
            style="width: 170px"
          ></el-input>
        </el-form-item>
        <el-form-item label="场次">
          <el-input
            :disabled="isView || isFrozen"
            clearable
            v-model="detailForm.sscc"
            style="width: 170px"
          ></el-input>
        </el-form-item>
        <el-form-item label="委托方">
          <el-input
            :disabled="isView || isFrozen"
            clearable
            v-model="detailForm.wtf"
            style="width: 170px"
          ></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row style="text-align: center">
      <el-button
        class="iconfont iconbaocun"
        type="primary"
        v-if="!isView"
        @click="handleSave"
        size="small"
        >保存</el-button
      >
      <el-button
        class="iconfont iconfanhui"
        type="primary"
        @click="$router.push('index')"
        size="small"
        >返回</el-button
      >
    </el-row>
  </div>
</template>

<script>
import {
  insert,
  province,
  contractInformationList,
  getTransactions,
  sinkpointList,
  warehouseList,
  // varietys,
  lsdj,
  lsxz,
  detail,
  update,
  getconfig,
  warehouseListByKddm,
} from "@/api/transaction/creditAssess/transactionDataMaintain/index";
import statusList from "./status.js";
import { varietys } from "@/api/dict";
export default {
  data() {
    return {
      detailForm: {
        sfmc: "天津",
        sflt: "0", // 是否露天
        cjje: 0,
        hxdf: 0,
      },
      provinces: [], // 省份
      contractInformations: [], // 合同列表
      transactions: [], // 标的号
      sinkPoints: [], // 库点列表
      wareHouses: [], // 仓房列表
      lsdjs: [], // 粮食等级
      lsxzs: [], // 粮食性质
      lspzs: [], // 粮食品种
      zts: statusList,
      cjbm: "",
      isView: false,
      isUpdate: false,
      isFrozen: false,
      config: {},
      fronzenZt: false,
      ztmcBak: "",
      // rules: {
      // 	cjsl: [
      // 		{
      // 			pattern: /^\d{1,10}?$/,
      // 			message: '长度最多10位，且为正整数',
      // 			trigger: 'blur',
      // 		},
      // 	],
      // 	zcjsl: [
      // 		{
      // 			pattern: /^\d{1,10}?$/,
      // 			message: '长度最多10位，且为正整数',
      // 			trigger: 'blur',
      // 		},
      // 	],
      // },
    };
  },
  created() {
    // state 0 add 1 change, 2 detail

    let { state, cjbm } = this.$route.query;
    this.cjbm = cjbm;
    if (state == 0) {
      this.detailForm.sjlx = 1; // 合同来源 新增, 0为系统合同
    }
    if (state != 0) {
      this.detail();
    }
    if (state == 1) {
      this.isUpdate = true;
    }
    if (state == 2) {
      this.isView = true;
    }
    this.getTransactions();
    this.province();
    this.contractInformationList();
    this.sinkpointList();
    this.warehouseList();
    this.varietys();
    this.lsdj();
    this.lsxz();
    this.getconfig();
  },
  compueted: {
    cjje() {
      let { cjsl, cjdj } = this.detailForm;
      let cjje;
      if (cjsl && cjdj) {
        cjje = cjsl * cjdj;
        this.detailForm.cjje = cjje;
      }
    },
  },
  methods: {
    rules(msg) {
      return [{ required: true, message: "请填写" + msg, trigger: "change" }];
    },
    cjsl(rule, value, callback) {
      if (!/^[1-9]\d*$/.test(value)) {
        callback(new Error("请输入正确的成交数量格式"));
      } else {
        callback();
      }
    },
    zcjsl(rule, value, callback) {
      if (!/^[1-9]\d*$/.test(value)) {
        callback(new Error("请输入正确的总成交数量格式"));
      } else {
        callback();
      }
    },
    hthchan() {
      this.detailForm.hth = this.detailForm.hth.replace(/\s+/g, "");
    },
    detail() {
      detail({
        cjbm: this.cjbm,
      }).then((res) => {
        if (res.infoCode == 200) {
          this.detailForm = res.data;
          this.fronzenZt = res.data.ztmc == "违规";
          if (this.detailForm.sjlx == 0) this.isFrozen = true;
          if (!this.detailForm.sfmc) this.detailForm.sfmc = "天津";
          if (!this.detailForm.ztmc) this.this.detailForm.ztmc = "履约";
          this.ztmcBak = this.detailForm.ztmc;
        }
      });
    },
    handleChange(fname, val) {
      this.detailForm[fname] = val;
    },
    handleChangeZT(val) {
      if (val == "违规") {
        this.$confirm("违规的客户，将影响下次交易的报名,是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then((_) => {
            this.ztmcBak = val;
            this.handleSum();
          })
          .catch((_) => {
            this.detailForm.ztmc = this.ztmcBak;
          });
      } else {
        this.handleSum();
      }
    },
    handleSum() {
      let { ztmc, zcjsl, cjdj } = this.detailForm;
      let cjje;
      this.detailForm.a = this.config.a;
      this.detailForm.b = this.config.b;
      this.detailForm.c = this.config.c;
      if (zcjsl && cjdj) {
        cjje = zcjsl * cjdj;
        this.detailForm.cjje = cjje;
      }

      if (ztmc && cjje) {
        let { a, b, c, formula1, formula2 } = this.config;
        let 交易额 = cjje;
        var sum = "";
        if (ztmc == "履约") sum = eval(formula1).toFixed(2);
        if (ztmc == "部分履约") sum = eval(formula2).toFixed(2);
        if (ztmc == "违约") sum = "0";
        if (ztmc == "违规") sum = "0";
        if (ztmc == "终止") sum = "-0.5";
        this.detailForm.hxdf = sum;
      }
    },
    getconfig() {
      getconfig(1045).then((res) => {
        if (res.infoCode == 200) {
          if (res.data.length) {
            this.config = JSON.parse(res.data[0].configLastValue);
          }
        }
      });
    },
    // 粮食性质列表
    lsxz() {
      lsxz().then((res) => {
        if (res.infoCode === 200) {
          this.lsxzs = res.data;
        }
        // dictName
        // dictCode
      });
    },
    // 粮食等级列表
    lsdj() {
      lsdj().then((res) => {
        if (res.infoCode === 200) {
          this.lsdjs = res.data;
        }
        // dictName,dictCode
      });
    },
    // 品种列表
    varietys() {
      varietys().then((res) => {
        if (res.infoCode === 200) {
          this.lspzs = res.data;
        }
        // dictCode,dictName
      });
    },
    //库点列表
    sinkpointList() {
      sinkpointList().then((res) => {
        if (res.infoCode === 200) {
          this.sinkPoints = res.data;
          // kddm
          // kdmc
        }
      });
    },
    // 仓房列表 根据库点 kddm
    warehouseList(kddm) {
      warehouseListByKddm(kddm).then((res) => {
        if (res.infoCode === 200) {
          this.wareHouses = res.data;
          // kddm
          // kdmc
        }
      });
    },
    // 合同列表
    contractInformationList() {
      contractInformationList().then((res) => {
        if (res.infoCode === 200) {
          this.contractInformations = res.data;
          // htbh
          // htmc
        }
      });
    },
    // 交易标的,
    getTransactions() {
      getTransactions().then((res) => {
        if (res.infoCode === 200) {
          this.transactions = res.data;
          // bdh
          // bdbm
          // apcc 场次
        }
      });
    },
    // 省份
    province() {
      province().then((res) => {
        if (res.infoCode === 200) {
          this.provinces = res.data;
          // countyCode
          // countyName
        }
      });
    },
    handleSave() {
      this.$refs["detailForm"].validate((valid) => {
        if (valid) {
          console.log(this.detailForm);
          if (this.isUpdate) {
            this.update();
            return;
          }
          this.insert();
        }
      });
      // l(JSON.stringify({...this.detailForm},4,null))
    },
    insert() {
      let data = {
        ...this.detailForm,
      };
      insert(data).then((res) => {
        if (res.infoCode === 200) {
          this.$message.success(res.msg);
          this.$router.push("index");
          return;
        }
        this.$message.error(res.msg);
      });
    },
    update() {
      let data = {
        ...this.detailForm,
        cjbm: this.cjbm,
      };
      update(data).then((res) => {
        if (res.infoCode === 200) {
          this.$message.success(res.msg);
          this.$router.push("index");
          return;
        }
        this.$message.error(res.msg);
      });
    },
  },
};
</script>
