<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" :disabled="loadingBtn || detailData.status == 1">
            保存数据
          </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="corpId">
            <crop-select v-model="form.corpId" corpType="KH" :refresh="false" @getCorpData="getCorpData"
              :disabled="detailData.status == 1"></crop-select>
          </template>
          <!-- <template slot="warehouseId">
            <el-cascader v-model="form.warehouseId" :options="storagetreeList" :show-all-levels="false"
              :disabled="detailData.status == 1"
              :props="{ checkStrictly: true, emitPath: false, label: 'title',multiple: true }" clearable>
            </el-cascader>
          </template> -->
        </avue-form>
      </basic-container>
      <containerTitle title="基础明细"></containerTitle>
      <basic-container v-loading="loadingBtn">
        <avue-crud ref="crud" :option="optionList" :data="dataList" :table-loading="loading" @saveColumn="saveColumn"
          @resetColumn="resetColumn" :cell-style="cellStyle">
          <template slot="menuLeft">
            <el-button type="primary" @click="addRow" size="small" :disabled="detailData.status == 1">新增
            </el-button>
          </template>
          <template slot="feeId" slot-scope="{ row,index }">
            <breakdown-select v-if="row.$cellEdit" v-model="row.feeId" @selectValue="value => selectValue(value,row)"
              :configuration="breakConfiguration">
            </breakdown-select>
            <span v-else>{{ row.feeName }}</span>
          </template>
          <template slot="menu" slot-scope="{ row, index }">
            <el-button size="small" type="text" @click="rowCell(row, index)" :disabled="detailData.status == 1">{{
            row.$cellEdit ? "保存" : "修改" }}</el-button>
            <el-button size="small" type="text" @click="rowDel(row, index)" :disabled="detailData.status == 1">删除
            </el-button>
          </template>
        </avue-crud>
      </basic-container>
      <upload-file ref="uploadFile" title="合同附件" :disabled="detailData.status == 1" :orderFilesList="orderFilesList"
        delUrl="" />
    </div>
  </div>
</template>

<script>
import { optionList } from "./js/optionList";
import { getDetail, submit, getStoragetree, getAllgoodstype, getLazylist } from "@/api/basicData/agreement";
export default {
  name: "index",
  data() {
    return {
      loadingBtn: false,
      form: {},
      dataList: [],
      option: {
        menuBtn: false,
        labelWidth: 130,
        column: [
          {
            label: "合同编号",
            prop: "contractNo",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 6,
          },
          {
            label: "制单部门",
            prop: "deptId",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            type: "select",
            dataType: "string",
            props: {
              label: "deptName",
              value: "id"
            },
            dicData: [],
            span: 6,
          },
          {
            label: "客户名称",
            prop: "corpId",
            rules: [
              {
                required: true,
                message: "",
                trigger: "blur"
              }
            ],
            span: 6,
          },
          {
            label: "贸易方式",
            prop: "billType",
            type: "select",
            dataType: "string",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dicData: [],
            multiple: true,
            span: 6,
          },
          {
            label: "有效期起",
            prop: "beginDate",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            span: 6,
          },
          {
            label: "有效期至",
            prop: "endDate",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            span: 6,
          },
          {
            label: "商品分类",
            prop: "goodsId",
            type: "select",
            dataType: "string",
            props: {
              label: "cname",
              value: "id"
            },
            dicData: [],
            multiple: true,
            span: 6,
          },
          {
            label: "仓库",
            prop: "warehouseId",
            type: "cascader",
            dataType: "string",
            dicData: [],
            props: {
              label: 'title',
              value: 'id'
            },
            multiple: true,
            span: 6,
          },
          {
            label: "状态",
            prop: "billStatus",
            type: "select",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            dicData: [],
            disabled: true,
            span: 6,
          },
          {
            label: "备注",
            prop: "remark",
            type: "textarea",
            minRows: 2,
            width: 120,
            span: 18,
          },
        ]
      },
      optionList: {},
      resultList: [],
      normTypeList: [],
      jobTitleList: [],
      jobLevelList: [],
      occupationalLevelList: [],
      maritalList: [],
      orderFilesList: [],
      storagetreeList: [],
      breakConfiguration: {
        multipleChoices: false,
        multiple: false,
        disabled: false,
        searchShow: true,
        collapseTags: false,
        clearable: true,
        placeholder: "请点击右边按钮选择",
        dicData: []
      },
    };
  },
  props: {
    detailData: {
      type: Object
    }
  },
  async created() {
    this.optionList = await this.getColumnData(
      this.getColumnName(193),
      optionList
    );
    if (this.detailData.id) {
      this.getDetail(this.detailData.id);
    }
    if (this.detailData.status == 1) {
      this.option.disabled = true;
    }
    this.getWorkDicts("result_type").then(res => {
      this.resultList = res.data.data;
    });
    this.getWorkDicts("trading_type").then(res => {
      this.findObject(this.option.column, "billType").dicData =
        res.data.data;
    });
    this.getWorkDicts("unit").then(res => {
      this.findObject(this.optionList.column, "feeUnitId").dicData =
        res.data.data;
    });
    this.getWorkDicts("bill_status").then(res => {
      this.findObject(this.option.column, "billSdtatus").dicData = res.data.data;
    });
    getStoragetree().then(res => {
      this.findObject(this.option.column, "warehouseId").dicData = res.data.data;
    })
    getAllgoodstype().then(res => {
      this.findObject(this.option.column, "goodsId").dicData =
        res.data.data;
    })
    getLazylist().then(res => {
      this.findObject(this.option.column, "deptId").dicData =
        res.data.data;
    })
  },
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    selectValue(value,row) {
      console.log(value,row)
      if(row.feeId){
        row.feeName=value.cname
      }else{
        row.feeName=""
      }
    },
    getDetail(id) {
      this.loadingBtn=true
      getDetail(id)
        .then(res => {
          // if (res.data.data.warehouseId) {
          //   res.data.data.warehouseId = res.data.data.warehouseId.split(',')
          // }
          this.form = res.data.data;
          this.dataList = res.data.data.agreementitemsList;
          this.orderFilesList = res.data.data.orderFilesList
        })
        .finally(() => {
          // this.loading = false;
          // this.showBut = true;
          this.loadingBtn = false;
        });
    },
    getCorpData(row) {
      this.form.corpName = row.cname
    },
    addRow() {
      this.dataList.push({ $cellEdit: true });
    },
    rowCell(row, index) {
      if (row.$cellEdit == true) {
        this.editCustomer();
        this.$set(row, "$cellEdit", false);
      } else {
        this.$set(row, "$cellEdit", true);
      }
    },
    rowDel(row, index) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        if (row.id) {
          // delItem(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);
        }
      });
    },
    //修改提交触发
    editCustomer() {
      this.$refs["form"].validate((valid, done) => {
        done();
        if (valid) {
          // if (this.form.warehouseId) {
          //   this.form.warehouseId = this.form.warehouseId.join(',')
          // }
          this.loadingBtn = true;
          submit({ ...this.form, agreementitemsList: this.dataList })
            .then(res => {
              this.$message.success("保存成功");
              // if (res.data.data.data.warehouseId) {
              //   res.data.data.data.warehouseId = res.data.data.data.warehouseId.split(',')
              // }
              console.log()
              this.form = res.data.data.data;
              this.dataList = res.data.data.data.agreementitemsList;
              this.orderFilesList = res.data.data.orderFilesList
            })
            .finally(() => {
              this.loadingBtn = false;
            });
        } else {
          return false;
        }
      });
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(193),
        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(193),
        optionList
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("重置成功");
        this.$refs.crud.$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;
}
</style>
