<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>
      <trade-card title="基础资料" style="margin-top: 60px" v-loading="loadingBtn">
        <avue-form ref="form" class="trading-form" v-model="form" :option="option">
          <template slot="corpsTypeId">
            <div style="display:flex;">
              <avue-cascader :emit-path="false" check-strictly :show-all-levels="false" v-model="form.corpsTypeId"
                placeholder="请选择客户分类" :dic="corpTypeList" :props="props">
              </avue-cascader>
              <i class="el-icon-circle-plus-outline" style="font-size:18px;line-height: 32px;margin-left:4px"
                @click="corpTypeVisible = true"></i>
            </div>
          </template>
        </avue-form>
      </trade-card>
      <trade-card title="地址管理" v-loading="loadingBtn">
        <el-tabs v-model="activeName">
          <el-tab-pane label="客户地址" name="first">
            <avue-crud ref="crud" :option="option2" :data="data1" :table-loading="loading" @saveColumn="saveColumn"
              @resetColumn="resetColumn" :cell-style="cellStyle" class="address">
              <template slot="headerSerial">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click.stop="addRow"
                  :disabled="detailData.status == 1" circle></el-button>
              </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="物流地址" name="second">
            <avue-crud ref="crud2" :option="option3" :data="data2" :table-loading="loading" @saveColumn="saveColumn2"
              @resetColumn="resetColumn2" :cell-style="cellStyle">
              <template slot="headerSerial">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click.stop="addRow2"
                  :disabled="detailData.status == 1" circle></el-button>
              </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>

      </trade-card>
      <containerTitle title="上传附件"></containerTitle>
      <c-upload v-loading="loadingBtn" typeUpload="CD"
        deleteUrl="/api/trade-purchase/woodHarvestingCloud/removeByFileId" :data="orderFilesList" display
        :enumerationValue="35.1" :disabled="detailData.status == 1"></c-upload>
      <el-dialog title="添加客户分类" v-dialogDrag :visible.sync="corpTypeVisible" class="avue-dialog avue-dialog--top"
        width="30%" append-to-body @closed="corpTypeClosed">
        <span>
          <avue-form :key="reload" ref="corpType" v-model="form4" :option="option4" style="margin-top:20px">
          </avue-form>
        </span>
        <div class="avue-dialog__footer">
          <el-button @click="corpTypeVisible = false" size="mini">取 消</el-button>
          <el-button @click="addCorpType" type="primary" size="mini">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { option2, option3 } from "./js/optionList";
import { getDetails, addCorpType, getCorpType, submit, customerList } from "@/api/basicData/client";
export default {
  name: "index",
  data() {
    return {
      props: {
        label: 'title',
        value: 'value'
      },
      corpTypeList: [],
      corpTypeVisible: false,
      activeName: 'first',
      loadingBtn: false,
      addressTitle: null,
      addressVisible: false,
      form: {},
      form2: {},
      form3: {},
      form4: {},
      data1: [],
      data2: [],
      option: {
        menuBtn: false,
        labelWidth: 130,
        column: [
          {
            label: "客户名称",
            prop: "cname",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 8,
          },
          {
            label: "客户分类",
            prop: "corpsTypeId",
            span: 8,
          },
          {
            label: "期初欠款",
            prop: "arrears",
            span: 8,
          },
          {
            label: "电话",
            prop: "tel",
            span: 8,
          },
          {
            label: "备用电话",
            prop: "telephone",
            span: 8,
          },
          {
            label: "传真",
            prop: "fax",
            span: 8,
          },
          {
            label: "邮箱",
            prop: "mailbox",
            span: 8,
          },
          {
            label: "备注",
            prop: "remarks",
            type: "textarea",
            minRows: 2,
            span: 24,
          }
        ]
      },
      option2: {},
      option3: {},
      option4: {
        menuBtn: false,
        labelWidth: 80,
        column: [
          {
            label: "分类名称",
            prop: "cname",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 24,
          },
          {
            label: "上级类型",
            prop: "parentId",
            dicData: [],
            type: "tree",
            props: {
              label: "cname",
              value: "id"
            },
            span: 24,
          }
        ]
      },
    };
  },
  props: {
    detailData: {
      type: Object
    }
  },
  async created() {
    this.option2 = await this.getColumnData(
      this.getColumnName(208),
      option2
    );
    this.option3 = await this.getColumnData(
      this.getColumnName(208.4),
      option3
    );
    if (this.detailData.id) {
      this.getDetail(this.detailData.id);
    }
    if (this.detailData.status == 1) {
      this.option.disabled = true;
    }
    this.getCorpType()
    this.getAllWorkDicts()
  },
  methods: {
    getAllWorkDicts() {
      customerList({ corpType: "KH" }).then(res => {
        this.findObject(this.option4.column, "parentId").dicData = res.data.data.records
      });
      this.getWorkDicts("abbreviation").then(res => {
        this.findObject(this.option2.column, "abbreviation").dicData = res.data.data;
      });
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    selectValue(value, row) {
      if (row.feesId) {
        row.feesName = value.cname
      } else {
        row.feesName = ""
      }
    },
    getDetail(id) {
      this.loadingBtn = true
      getDetails({ id: id })
        .then(res => {
          this.form = res.data.data;
          this.data = res.data.data.corpsAddrList;
        })
        .finally(() => {
          this.loadingBtn = false;
        });
    },
    getCorpData(row) {
      this.form.corpName = row.cname
    },
    addRow() {
      this.data1.push({ $cellEdit: true, type: 0 })
    },
    rowCell(row, index) {
      if (row.$cellEdit == true) {
        this.$set(row, "$cellEdit", false);
      } else {
        this.$set(row, "$cellEdit", true);
      }
    },
    rowDel(row, index) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (row.id) {
          // itemDel(row.id).then(res => {
          //   this.$message({
          //     type: "success",
          //     message: "删除成功!"
          //   });
          //   this.data1.splice(index, 1);
          // });
        } else {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.data1.splice(index, 1);
        }
      });
    },
    addRow2() {
      this.data2.push({ $cellEdit: true, type: 1 })
    },
    rowCell2(row, index) {
      if (row.$cellEdit == true) {
        this.$set(row, "$cellEdit", false);
      } else {
        this.$set(row, "$cellEdit", true);
      }
    },
    rowDe2(row, index) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (row.id) {
          // itemDel(row.id).then(res => {
          //   this.$message({
          //     type: "success",
          //     message: "删除成功!"
          //   });
          //   this.data2.splice(index, 1);
          // });
        } else {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.data2.splice(index, 1);
        }
      });
    },
    corpTypeClosed() {
      this.reload = Math.random();
      this.form4 = this.$options.data().form4
    },
    getCorpType() {
      getCorpType({ corpType: 'KH' }).then(res => {
        this.corpTypeList = res.data.data
      })
    },
    addCorpType() {
      this.$refs["corpType"].validate((valid, done) => {
        done();
        if (valid) {
          addCorpType({ ...this.form4, corpType: 'KH', status: 0 })
            .then(res => {
              this.$message.success("保存成功");
              this.getCorpType()
              this.corpTypeVisible = false
            })
        } else {
          return false;
        }
      });
    },
    //修改提交触发
    editCustomer() {
      this.$refs["form"].validate((valid, done) => {
        done();
        if (valid) {
          this.loadingBtn = true;
          submit({ ...this.form, code: this.form.cname, corpType: "KH", corpsAddrList: this.data })
            .then(res => {
              this.$message.success("保存成功");
              this.form = res.data.data;
              this.data = res.data.data.corpsAddrList;
            })
            .finally(() => {
              this.loadingBtn = false;
            });
        } else {
          return false;
        }
      });
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(208),
        this.option2
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.option2 = option2;
      const inSave = await this.delColumnData(
        this.getColumnName(208),
        option2
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.getAllWorkDicts()
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async saveColumn2() {
      const inSave = await this.saveColumnData(
        this.getColumnName(208.4),
        this.option3
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud2.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn2() {
      this.option3 = option3;
      const inSave = await this.delColumnData(
        this.getColumnName(208.4),
        option3
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.getAllWorkDicts()
        this.$message.success("重置成功");
        this.$refs.crud2.$refs.dialogColumn.columnBox = false;
      }
    },
    //返回列表
    backToList() {
      this.$emit("goBack");
      console.log(11111)
    }
  }
};
</script>

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

::v-deep .el-dialog__body {
  padding: 0px 20px 15px 20px;
}

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

.el-dialog ::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;
}

.addressTabs {
  display: flex;
  justify-content: center;

  span {
    width: 100px;
    font-size: 18px;
    font-weight: 600;
    text-align: center;
  }
}
</style>
