<template>
  <div class="detail-main">
    <h2
      style="
        text-align: center;
        font-weight: 700;
        margin-bottom: 10px;
        font-size: 18px;
        color: #4588db;
      "
    >
      出入库安排
    </h2>
    <el-table :data="tableData" stripe border>
      <el-table-column
        type="index"
        align="center"
        label="序号"
        width="70"
      ></el-table-column>
      <el-table-column
        prop="dwmc"
        align="center"
        label="承储单位"
        min-width="160"
      >
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.dwmc"
            @change="handleCompanyChange($event, scope.$index)"
            :disabled="isView"
          >
            <el-option
              v-for="item in companies"
              :key="item.kddm"
              :value="item.kdmc"
              :label="item.kdmc"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="pzmc" align="center" label="品种">
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.pzmc"
            @change="handleVarietyChange($event, scope.$index)"
            :disabled="isView"
          >
            <el-option
              v-for="item in variety"
              :key="item.dictCode"
              :value="item.dictName"
              :label="item.dictName"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="lssl" align="center" label="数量">
        <template slot-scope="scope">
          <el-input v-model="scope.row.lssl" :disabled="isView"></el-input>
        </template>
      </el-table-column>
      <el-table-column
        prop="scnd"
        align="center"
        label="生产年度"
        width="120px"
      >
        <template slot-scope="scope" class="cell">
          <el-date-picker
            type="year"
            value-format="yyyy"
            v-model="scope.row.scnd"
            style="width: 100%"
            :disabled="isView"
          ></el-date-picker>
        </template>
      </el-table-column>
      <el-table-column prop="sfbg" align="center" label="财务账变更">
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.sfbg"
            @change="handleBillChange($event, scope.$index)"
            :disabled="isView"
          >
            <el-option
              v-for="item in [
                { code: 0, name: '是' },
                { code: 1, name: '否' },
              ]"
              :key="item.name"
              :value="item.code"
              :label="item.name"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column prop="remark" align="center" label="备注">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remark" :disabled="isView"></el-input>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="90px" v-if="!isView">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="light"
            content="删除"
            placement="top"
          >
            <el-button
              @click="handleDelRow(scope.$index)"
              type="primary"
              class="operate_delete_btn iconfont iconshanchu"
              size="small"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-row v-if="!isView">
      <div class="add-table-item">
        <el-tooltip class="item" effect="light" content="新增" placement="top">
          <el-button
            @click="handleAddRow"
            type="text"
            class="iconfont iconzengjia"
          ></el-button>
        </el-tooltip>
      </div>
    </el-row>
    <el-row class="footer">
      <el-button
        type="primary"
        v-if="!isView"
        size="small"
        @click="handleSave"
        class="iconfont iconbaocun"
        >保存</el-button
      >
      <el-button
        type="primary"
        v-if="!isView"
        size="small"
        @click="handleSubmit"
        class="iconfont icontijiao"
        >提交</el-button
      >
      <el-button
        type="primary"
        size="small"
        @click="$router.push('index')"
        class="iconfont iconfanhui"
        >返回</el-button
      >
    </el-row>
  </div>
</template>

<script>
import {
  companies,
  varietys,
  insert,
  update,
  detail,
} from "@/api/transaction/tradeEntrust/outInDepotPlan/index.js";
export default {
  data() {
    return {
      planData: {
        pzbm: "",
        dwbm: "",
        lssl: "",
        scnd: "",
        sfbg: "",
        remark: "",
        sjlx: 0, // 安排0, 货源1
        wtbm: "",
        jylx: "",
      },
      tableData: [],
      rowData: {},
      companies: [],
      variety: [],
      form: {
        apzt: "",
        apztmc: "",
        wtbm: "",
      },
      isAdd: false,
      isView: false,
    };
  },
  created() {
    // state 0 add 1 update 2 detail
    let { state, wtbm, jylx } = this.$route.query;
    this.form.wtbm = wtbm;
    this.planData.wtbm = wtbm;
    this.planData.jylx = jylx;
    if (state == 0) {
      this.isAdd = true;
      this.tableData.push(this.planData);
    } else {
      this.getDetail();
    }
    if (state == 2) {
      this.isView = true;
    } else {
      this.getCompanies();
      this.getTypes();
    }
  },
  methods: {
    getDetail() {
      detail({
        wtbm: this.form.wtbm,
        sjlx: 0,
      }).then((res) => {
        if (res.infoCode === 200) {
          this.tableData = res.data.details;
        }
      });
    },
    handleSave() {
      this.form = {
        ...this.form,
        apzt: "91000001",
        apztmc: "待提交",
      };
      if (this.isAdd) {
        this.insert();
        return;
      }
      this.update();
    },
    handleSubmit() {
      this.form = {
        ...this.form,
        apzt: "91000004",
        apztmc: "已提交",
        hybzt: "91000049",
        hybztmc: "待安排",
      };
      if (this.isAdd) {
        this.insert();
        return;
      }
      this.update();
    },
    insert() {
      insert({
        details: this.tableData,
        entryAndExitOrderVO: this.form,
      }).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.form,
      }).then((res) => {
        if (res.infoCode === 200) {
          this.$message.success(res.msg);
          this.$router.push("index");
          return;
        }
        this.$message.error(res.msg);
      });
    },
    handleBillChange(val, index) {
      this.tableData[index].sf = val == 0 ? "是" : "否";
    },
    handleCompanyChange(val, index) {
      let cur = this.companies.filter((item) => item.kdmc == val)[0];
      this.tableData[index].dwbm = cur.kddm;
    },
    handleVarietyChange(val, index) {
      let cur = this.variety.filter((item) => item.dictName == val)[0];
      this.tableData[index].pzbm = cur.dictCode;
    },
    handleAddRow() {
      this.tableData.push({
        ...this.planData,
      });
    },
    handleDelRow(index) {
      this.tableData.splice(index, 1);
    },
    getCompanies() {
      companies().then((res) => {
        this.companies = res.data;
      });
    },
    getTypes() {
      varietys(11).then((res) => {
        this.variety = res.data;
      });
    },
  },
};
</script>

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

  .add-table-item {
    text-align: right;
    margin-right: 36px;

    .iconfont {
      font-size: 20px;
      font-weight: bold;
      cursor: pointer;
    }
  }

  .footer {
    text-align: center;
    margin: 20px 0;
  }
}
</style>
