<template>
  <div>
    <div class="borderless">
      <div class="customer-head">
        <div class="customer-back">
          <el-button type="danger" style="border: none;background: none;color: red" icon="el-icon-arrow-left"
            @click="backToList">返回列表
          </el-button>
        </div>
        <div class="add-customer-btn">
          <el-button type="primary" size="small" @click="editCustomer">
            保存数据
          </el-button>
        </div>
      </div>
      <containerTitle title="基础资料" style="margin-top: 60px"></containerTitle>
      <basic-container v-loading="loadingBtn">
        <avue-form ref="form" class="trading-form" v-model="form" :option="option">
          <template slot="outHarborName">
            <port-info v-model="form.outHarborName" />
          </template>
          <template slot="objectiveName">
            <port-info v-model="form.objectiveName" />
          </template>
        </avue-form>
      </basic-container>
      <containerTitle title="基础明细"></containerTitle>
      <basic-container v-loading="loadingBtn">
        <el-tabs>
          <el-tab-pane label="运费">
            <avue-crud ref="crud" :option="optionList" :data="dataList" :table-loading="loading"
              @saveColumn="saveColumn" @resetColumn="resetColumn" :cell-style="cellStyle" @row-save="rowSave"
              @row-update="addUpdate">
              <template slot="menuLeft">
                <el-button type="primary" @click="addRow" size="small">新增
                </el-button>
              </template>
              <template slot="feesId" slot-scope="{ row,index }">
                <breakdown-select v-if="row.$cellEdit" v-model="row.feesId"
                  @selectValue="value => selectValue(value,row)" :configuration="breakConfiguration">
                </breakdown-select>
                <span v-else>{{ row.feesName }}</span>
              </template>
              <template slot="currency" slot-scope="{ row,index }">
                <el-select size="small" v-if="row.$cellEdit" v-model="row.currency" placeholder="请选择" clearable>
                  <el-option v-for="item in currencyList" :key="item.id" :label="item" :value="item">
                  </el-option>
                </el-select>
                <span v-else>{{ row.currency}}</span>
              </template>
              <template slot="menu" slot-scope="{ row, index }">
                <el-button size="small" type="text" @click="rowCell(row, index)">{{
                row.$cellEdit ? "保存" : "修改" }}</el-button>
                <el-button size="small" type="text" @click="rowDel(row, index)">删除
                </el-button>
              </template>
            </avue-crud>
          </el-tab-pane>
          <el-tab-pane label="杂费">
            <avue-crud ref="crud2" :option="optionList2" :data="dataList2" :table-loading="loading"
              @saveColumn="saveColumn2" @resetColumn="resetColumn2" :cell-style="cellStyle" @row-save="rowSave2"
              @row-update="addUpdate2">
              <template slot="menuLeft">
                <el-button type="primary" @click="addRow2" size="small" :disabled="detailData.status == 1">新增
                </el-button>
              </template>
              <template slot="feesId" slot-scope="{ row,index }">
                <breakdown-select v-if="row.$cellEdit" v-model="row.feesId"
                  @selectValue="value => selectValue(value,row)" :configuration="breakConfiguration">
                </breakdown-select>
                <span v-else>{{ row.feesName }}</span>
              </template>
              <template slot="currency" slot-scope="{ row,index }">
                <el-select size="small" v-if="row.$cellEdit" v-model="row.currency" placeholder="请选择" clearable>
                  <el-option v-for="item in currencyList" :key="item.id" :label="item" :value="item">
                  </el-option>
                </el-select>
                <span v-else>{{ row.currency}}</span>
              </template>
              <template slot="menu" slot-scope="{ row, index }">
                <el-button size="small" type="text" @click="rowCell2(row, index)">{{
                row.$cellEdit ? "保存" : "修改" }}</el-button>
                <el-button size="small" type="text" @click="rowDel2(row, index)">删除
                </el-button>
              </template>
            </avue-crud>
          </el-tab-pane>
        </el-tabs>
      </basic-container>
    </div>
  </div>
</template>

<script>
import { optionList, optionList2 } from "./js/optionList";
import { getDetail, submit, itemDel } from "@/api/maintenance/landFreight";
import { areaTypeTree } from "@/api/basicData/customerInformation";
import { getCode } from "@/api/basicData/customerInquiry";
export default {
  name: "index",
  data() {
    return {
      loadingBtn: false,
      form: {},
      dataList: [],
      dataList2: [],
      option: {
        menuBtn: false,
        labelWidth: 130,
        column: [
          {
            label: "货物属性",
            prop: "goodsAttribute",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=cargo_type",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 8,
          },
          {
            label: "起运港",
            prop: "outHarborName",
            span: 8,
          },
          {
            label: "目的港",
            prop: "objectiveName",
            span: 8,
          },
          {
            label: "备注",
            prop: "remarks",
            type: "textarea",
            minRows: 2,
            span: 24,
          }
        ]
      },
      optionList: {},
      optionList2: {},
      breakConfiguration: {
        multipleChoices: false,
        multiple: false,
        disabled: false,
        searchShow: true,
        collapseTags: false,
        clearable: true,
        placeholder: "请点击右边按钮选择",
        dicData: []
      },
      currencyList: []
    };
  },
  props: {
    detailData: {
      type: Object
    }
  },
  async created() {
    this.optionList = await this.getColumnData(
      this.getColumnName(206),
      optionList
    );
    this.optionList2 = await this.getColumnData(
      this.getColumnName(206.1),
      optionList2
    );
    if (this.detailData.id) {
      this.getDetail(this.detailData.id);
    }
    if (this.detailData.status == 1) {
      this.option.disabled = true;
    }
    getCode().then(res => {
      this.currencyList = res.data.data;
    });
  },
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    selectValue(value, row) {
      console.log(value, row)
      if (row.feesId) {
        row.feesName = value.cname
      } else {
        row.feesName = ""
      }
    },
    getDetail(id) {
      this.loadingBtn = true
      getDetail(id)
        .then(res => {
          this.form = res.data.data;
          this.dataList = res.data.data.orderCostItemList.filter(e => e.type == 2);
          this.dataList2 = res.data.data.orderCostItemList.filter(e => e.type == 3);
        })
        .finally(() => {
          this.loadingBtn = false;
        });
    },
    getCorpData(row) {
      this.form.corpName = row.cname
    },
    addRow() {
      this.dataList.push({ currency: 'CNY', type: 2, $cellEdit: true });
    },
    rowCell(row, index) {
      this.$refs.crud.rowCell(row, index)
    },
    rowSave(form, done) {
      done()
    },
    addUpdate(form, index, done, loading) {
      done()
    },
    rowDel(row, index) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (row.id) {
          itemDel(row.id).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.dataList.splice(index, 1);
          });
        } else {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.dataList.splice(index, 1);
        }
      });
    },
    addRow2() {
      this.dataList2.push({ currency: 'CNY', type: 3, $cellEdit: true });
    },
    rowCell2(row, index) {
      this.$refs.crud2.rowCell(row, index)
    },
    rowSave2(form, done) {
      done()
    },
    addUpdate2(form, index, done, loading) {
      done()
    },
    rowDel2(row, index) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (row.id) {
          itemDel(row.id).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.dataList2.splice(index, 1);
          });
        } else {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.dataList2.splice(index, 1);
        }
      });
    },
    //修改提交触发
    editCustomer() {
      this.$refs["form"].validate((valid, done) => {
        done();
        if (valid) {
          this.loadingBtn = true;
          submit({ ...this.form, feesType: 2, orderCostItemList: this.dataList.concat(this.dataList2) })
            .then(res => {
              this.$message.success("保存成功");
              this.form = res.data.data;
              this.dataList = res.data.data.orderCostItemList.filter(e => e.type == 2);
              this.dataList2 = res.data.data.orderCostItemList.filter(e => e.type == 3);
            })
            .finally(() => {
              this.loadingBtn = false;
            });
        } else {
          return false;
        }
      });
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(206),
        this.optionList
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.optionList = optionList;
      const inSave = await this.delColumnData(
        this.getColumnName(206),
        optionList
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async saveColumn2() {
      const inSave = await this.saveColumnData(
        this.getColumnName(206.1),
        this.optionList2
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud2.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn2() {
      this.optionList2 = optionList2;
      const inSave = await this.delColumnData(
        this.getColumnName(206.1),
        optionList2
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("重置成功");
        this.$refs.crud2.$refs.dialogColumn.columnBox = false;
      }
    },
    //返回列表
    backToList() {
      this.$emit("goBack");
    }
  }
};
</script>

<style lang="scss" scoped>
.trading-form ::v-deep .el-form-item {
  margin-bottom: 8px !important;
}

::v-deep .el-form-item__error {
  display: none !important;
}

.img-form ::v-deep .el-form-item {
  height: 150px;
  line-height: 150px;
  margin-bottom: 8px !important;
}

.img-form ::v-deep .avue-upload__icon {
  font-size: 20px;
  width: 150px;
  height: 150px;
  line-height: 150px;
}

::v-deep .el-table .cell {
  padding: 0 2px !important;
}

::v-deep .avue-crud .el-table .el-form-item__label {
  left: -1px;
}
</style>
