<template>
  <div class="publish-main">
    <h2 style="font-weight:600;font-size:20px;text-align:center;margin-bottom:10px;">交易明细</h2>
    <el-form :model="detailForm">
      <el-form-item label="公告标题" required prop="mxbt">
        <el-input
          v-model="detailForm.mxbt"
          :rules="[{ required: true, message: '请输入公告标题', trigger: 'blur' }]"
          :disabled="isView"
        ></el-input>
      </el-form-item>
      <el-form-item label="发布人" required prop="mxfbr">
        <el-input
          v-model="detailForm.mxfbr"
          :rules="[{ required: true, message: '请输入发布人', trigger: 'blur' }]"
          :disabled="isView"
        ></el-input>
      </el-form-item>
      <el-form-item label="公告内容" required prop="mxzw">
        <el-input
          v-html="detailForm.mxzw"
          :rules="[{ required: true, message: '请输入公告内容', trigger: 'blur' }]"
          :disabled="isView"
          type="textarea"
          :autosize="{minRows:4}"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-row style="text-align:center">
      <el-button
        v-if="!isView"
        @click="handleSave"
        class="iconfont iconbaocun"
        type="primary"
        size="small"
      >保存</el-button>
      <el-button
        v-if="!isView"
        @click="handlePublish"
        class="iconfont iconfabu"
        type="primary"
        size="small"
      >发布</el-button>
      <el-button @click="handleBack" class="iconfont iconfanhui" type="primary" size="small">返回</el-button>
    </el-row>
  </div>
</template>

<script>
import {
  update,
  detail
} from "@/api/transaction/tradeEntrust/outInDepotPlan/index.js";
export default {
  data() {
    return {
      detailForm: {
        mxfbr: sessionStorage.getItem("mxfbr") || "中国天津粮油批发交易市场",
        mxzw: sessionStorage.getItem("mxzw") || "",
        mxbt: sessionStorage.getItem("mxbt") || ""
      },
      wtbm: "",
      isView: false,
      tableData: []
    };
  },
  created() {
    // state 0 发布 1 详情
    let { state, wtbm, jylx } = this.$route.query;
    this.detailForm.wtbm = wtbm;
    this.detailForm.jylx = jylx;
    this.getDetail();
    if (state == 1) {
      this.isView = true;
    }
  },
  beforeDestroy() {
    sessionStorage.removeItem("mxbt");
    sessionStorage.removeItem("mxzw");
    sessionStorage.removeItem("mxfbr");
  },
  methods: {
    handleBack() {
      sessionStorage.removeItem("mxbt");
      sessionStorage.removeItem("mxzw");
      sessionStorage.removeItem("mxfbr");
      this.$router.push("index");
    },
    getDetail() {
      detail({
        wtbm: this.detailForm.wtbm,
        jylx: this.detailForm.jylx
      }).then(res => {
        if (res.infoCode === 200) {
          var list = res.data;
          // list.forEach(item => {
          //   item.zt = "91000056";
          //   item.ztmc = "待分解";
          // });
          this.tableData = list;
        }
      });
    },
    // async getDetail() {

    //     var res = await detail(this.wtbm)
    //     if (res && res.infoCode === 200) {
    //         this.detailForm = res.data
    //         if (!res.data.fbr) {
    //             this.detailForm.fbr = sessionStorage.getItem('deptName')
    //         }
    //     }
    // },
    // update() {
    //     update({
    //         ...this.detailForm
    //     }).then(res => {
    //         if (res.infoCode === 200) {
    //             this.$message.success(res.msg)
    //             this.$router.push('index')
    //             return
    //         }
    //         this.$message.error(res.msg)
    //     })
    // },
    update() {
      update({
        details: this.tableData,
        entryAndExitOrderVO: this.detailForm
      }).then(res => {
        if (res.infoCode === 200) {
          this.$message.success(res.msg);
          this.$router.push("index");
          return;
        }
        this.$message.error(res.msg);
      });
    },
    handleSave() {
      // this.detailForm.hybzt = '0'
      // this.detailForm.hybztmc = '暂存'
      this.update();
    },
    handlePublish() {
      // this.detailForm.zt = '91000047'
      // this.detailForm.ztmc = '已发布'
      this.detailForm.hybzt = "91000047";
      this.detailForm.hybztmc = "已发布";
      this.update();
    }
  }
};
</script>

<style lang="scss" scoped>
.publish-main {
  width: 80%;
  margin: 0 auto;
  padding: 20px;
}
</style>
