<template>
  <div>
    <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(0)">返回列表
        </el-button>
      </div>
      <div class="add-customer-btn">
        <!--        <el-button class="el-button&#45;&#45;small-yh" style="margin-right: 10px" type="primary" size="small" v-if="!editButton"-->
        <!--                   @click="confirmEditing">编辑-->
        <!--        </el-button>-->
        <el-button
          class="el-button--small-yh"
          type="primary"
          size="small"
          @click="editCustomer">保存数据
        </el-button>
        <el-button
          class="el-button--small-yh"
          type="primary"
          size="small"
          @click="enableNot"
          v-if="form.id">
          {{ form.enableOrNot == 0 ? '启用' : '禁用' }}
        </el-button>
      </div>
    </div>
    <div style="margin-top: 50px">
      <trade-card title="基础信息">
        <avue-form :option="optionForm" v-model="form" ref="form"></avue-form>
      </trade-card>
      <trade-card title="附件信息  (图片文件像素推荐700×400)">
        <c-upload
          basic
          :data="form.brandFilesList"
          :disabled="disabled"
          deleteUrl="/api/blade-sales-part/brandfiles/remove"
          :enumerationValue="272.1"
          display
        />
        <!-- <span style="font-size: 12px;">(图片文件像素推荐700X750，有且只允许有一张主图，其余均为副图)</span> -->
      </trade-card>
    </div>
  </div>
</template>

<script>
// import {submit} from "@/api/tirePartsMall/basicData/customerInformation";
import {submit, getDetails, updateEnableOrNot} from "@/api/tirePartsMall/basicData/brandPage";
import it from "element-ui/src/locale/lang/it";

export default {
  name: "detailsPage",
  data() {
    return {
      disabled: false,
      form: {
        brandFilesList: []
      },
      optionForm: {
        menuBtn: false,
        span: 8,
        column: [{
          label: '名称',
          prop: "cname",
          rules: [{
            required: true,
            message: " ",
            trigger: "blur"
          }]
        }, {
          label: '制单人',
          prop: "createUserName",
          disabled: true
        }, {
          label: '制单日期',
          prop: "createTime",
          disabled: true
        }, {
          label: '排序',
          prop: "sort",
          type:'number'
        }, {
          label: '备注',
          prop: "remarks",
          type: 'textarea',
          span: 24,
          minRows: 2
        }]
      },
      formContacts: {},
    }
  },
  props: {
    onLoad: Object,
    detailData: Object
  },
  async created() {
    if (this.onLoad.id && this.detailData.id) {
      this.refresh(this.onLoad.id, true)
    } else if (this.onLoad.id) {
      this.refresh(this.onLoad.id, true)
    }
  },
  methods: {
    refresh(id, type) {
      const loading = this.$loading({
        lock: true,
        text: '加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(255,255,255,0.7)'
      })
      getDetails({id: id}).then(res => {
        this.form = res.data.data
        loading.close();
      }).catch(() => {
        loading.close();
      })
    },
    //是否启用
    enableNot() {
      const {brandFilesList, ...formWithoutBrandFilesList} = this.form;
      const loading = this.$loading({
        lock: true,
        text: '加载中',
        spinner: 'el-icon-loading',
        background: 'rgba(255,255,255,0.7)'
      })
      updateEnableOrNot({
        ...formWithoutBrandFilesList,
        enableOrNot: this.form.enableOrNot == 0 ? 1 : 0
      }).then(res => {
        loading.close()
        this.$message.success("操作成功")
        this.refresh(this.form.id)
      }).catch(() => {
        loading.close()
      })
    },
    //修改提交触发
    editCustomer() {
      if (this.form.brandFilesList.length === 0) {
        return this.$message.error("请上传主图")
      }
      let num = 0
      for (let item of this.form.brandFilesList) {
        if (item.version == 0) {
          num++
        }
      }
      if (num > 1) {
        return this.$message.error("主图类型重复")
      }
      this.$refs["form"].validate((valid, done) => {
        done();
        if (valid) {
          this.loadingBtn = true;
          this.form.type = 'LBT'
          submit({
            ...this.form
          }).then(res => {
            this.$message.success("保存成功");
            this.refresh(res.data.data.id)
          }).finally(() => {
            this.loadingBtn = false;
          });
        } else {
          return false;
        }
      });
    },
    //自定义列保存
    async saveColumnTwo(ref, option, optionBack, code) {
      /**
       * 已定义全局方法，直接使用，saveColumnData保存列数据方法,参数传值(表格名称，当前表格的option数据)
       * 已定义全局方法，直接使用，getColumnName方法用来获取枚举值，参数根据自己定义的code值获取中文名
       * 一定要执行异步操作，要等接口成功返回，才能执行下一行代码
       */
      const inSave = await this.saveColumnData(this.getColumnName(code), this[option]);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    },
    //自定义列重置
    async resetColumnTwo(ref, option, optionBack, code) {
      this[option] = this[optionBack];
      const inSave = await this.delColumnData(this.getColumnName(code), this[optionBack]);
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs[ref].$refs.dialogColumn.columnBox = false;
      }
    },
    backToList(type) {
      this.$emit("backToList", type);
    }
  }
}
</script>

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