<template>
  <div class="edit-add-detail">
    <div class="crumbs">
      <Breadcrumb
        :breadcrumbOpt="breadcrumbOpt"
        svg-name="#icon-wodeqingqiu"
        @breadcrumb-click="handleClose"
      />
    </div>
    <div class="form-content">
      <el-form
        ref="formInfo"
        :model="parameter"
        label-width="auto"
        label-position="right"
        :rules="rules"
        v-loading="loading"
      >
        <template>
          <div class="rows">
            <div class="rows-left">
              <div class="rows-hr"></div>
            </div>
            <div class="rows-title">
              <div class="text-overflow">基本信息</div>
            </div>
          </div>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="供应商名称" prop="supplierName">
                <el-input
                  clearable
                  prefix-icon="el-icon-search"
                  v-model="parameter.supplierName"
                  @focus="open('poolUnits')"
                  placeholder="请选择供应商"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="供应商税号" prop="companyOrgcode">
                <el-input
                  readonly
                  v-model="parameter.companyOrgcode"
                  placeholder="选择供应商自动获取"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="采购分类" prop="categoryName">
                <el-input v-model="parameter.categoryName" placeholder="请选择供应商分类">
                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="handleInput"
                  ></el-button>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row> -->
          <!-- <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="附件上传" prop="accessoryList">
                <el-upload
                  class="upload-demo"
                  ref="upload"
                  :headers="headers"
                  :action="action"
                  :file-list="parameter.accessoryList"
                  :multiple="true"
                  accept=".png,.jpg,.jpeg,.xlsx,.xls,.pdf,.docx,.doc,.zip,.rar"
                  :show-file-list="false"
                  :on-success="handleSuccess"
                >
                  <el-button slot="trigger" size="small" type="primary"
                    >上传文件</el-button
                  >
                </el-upload>
              </el-form-item>
            </el-col>
            <el-table
              class="down"
              :data="parameter.accessoryList"
              border
              stripe
              style="width: 100%; margin-top: 20px"
            >
              <el-table-column prop="name" label="文件名称"></el-table-column>
              <el-table-column prop="size" label="文件大小">
                <template slot-scope="scope">
                  <span v-if="scope.row.fileSize">{{
                    scope.row.fileSize
                  }}</span>
                  <template v-else>
                    <span v-if="scope.row.size / 1024 / 1024 < 1">{{
                      (scope.row.size / 1024).toFixed(2) + 'KB'
                    }}</span>
                    <span v-else>{{
                      (scope.row.size / 1024 / 1024).toFixed(2) + 'MB'
                    }}</span>
                  </template>
                </template>
              </el-table-column>
              <el-table-column prop="createTime" label="上传时间">
                <template slot-scope="scope">
                  <div>
                    {{ scope.row.response.value.createTime }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column width="150px" label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="text">
                    <a
                      target="_blank"
                      @click="
                        downloadFile(
                          scope.row.name,
                          scope.row.response.value.url
                        )
                      "
                      >下载</a
                    >
                  </el-button>
                  <el-button
                    size="small"
                    type="text"
                    @click="deleteHandle(scope.$index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table> -->
          <!-- </el-row> -->
        </template>
      </el-form>
    </div>
    <div slot="footer" class="footer">
      <el-button @click="handleClose">{{ $t('domestic.cancel') }}</el-button>
      <el-button @click="handleSave" :loading="saveing" type="primary"
        >保存</el-button
      >
    </div>
    <classificationDialog
      @load="getClass"
      ref="class"
      :useOrgList="classList"
    ></classificationDialog>
    <expertPoolUnits
      @addDetail="(list) => addDetail(list, 'supplierList')"
      :useOrgList="supplierList"
      :needReturnKey="['code']"
      title="关联供应商"
      ref="poolUnits"
    />
  </div>
</template>
  
  <script>
import supplierManger from '@/api/supplierManger.js'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import expertPoolUnits from './expertPoolUnits.vue'
import classificationDialog from '../../onboardingSupplier/component/classificationDialog.vue'
import {mapState} from 'vuex'
import utils from '@/utils.js'
export default {
  name: 'domesticDialog',
  components: {
    Breadcrumb,
    classificationDialog,
    expertPoolUnits,
  },
  props: {
    editData: Object,
  },
  data() {
    return {
      saveing: false,
      breadcrumbOpt: [{breadcrumbName: '临时供应商'}, {breadcrumbName: '新增'}],
      rules: {
        supplierName: [
          {
            required: true,
            message: '请输入供应商名称',
            trigger: ['blur', 'change'],
          },
        ],
      },
      parameter: {
        type: 2,
        categoryCode: 1,
        accessoryList: [],
        reviewStatus: 2,
      },
      classList: [],
      enterpriseList: [],
      supplierList: [],
      loading: false,
    }
  },
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.enterpriseId = this.currentUser.org[0].orgId
    this.parameter.enterpriseName = this.currentUser.org[0].orgName
    this.parameter.enterpriseCode = this.currentUser.org[0].orgCode
    this.addModifyBtn()
  },
  computed: {
    ...mapState({
      headers: (state) => {
        return {
          Authorization: `Bearer ${state.login.currentUser.token}`,
        }
      },
      action: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
  },
  methods: {
    handleInput() {
      this.$refs['class'].add()
    },
    handleSuccess(res, file, fileList) {
      this.parameter.accessoryList = fileList
    },
    //下载按钮回调
    downloadFile(name, url) {
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.download = name
      a.target = '_blank'
      a.href = url
      a.dispatchEvent(event)
    },
    // 删除
    deleteHandle(index) {
      this.$confirm('是否确认删除该附件', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.parameter.accessoryList.splice(index, 1)
      })
    },
    addDetail(list, key) {
      console.log(list, key)
      this[key] = list
      let arr = []
      list.forEach((item) => {
        arr.push(item.name)
      })
      this.$set(this.parameter, 'supplierName', arr.join(','))
      console.log(this.parameter)
      this.parameter.companyOrgcode = list[0].companyOrgcode
    },
    getClass(list) {
      this.classList = list
      this.parameter.categoryCode = list[0].code
      this.parameter.categoryName = list[0].name
      this.parameter.supplierTypeId = list[0].id
    },
    getByIdData(id) {
      supplierManger.getByIdManage(id).then((res) => {
        let data = res.data.bizSupplierEnterprise
        this.enterpriseList = [
          {
            id: data.enterpriseId,
            code: data.enterpriseCode,
            name: data.enterpriseName,
          },
        ]
        this.supplierList = [
          {
            id: data.enterpriseId,
            code: data.enterpriseCode,
            name: data.enterpriseName,
          },
        ]
        this.$set(data, 'supplierCode', this.supplierList)
        this.parameter = data
      })
    },

    //编辑
    addModifyBtn() {
      let {id} = this.$route.params
      if (id) {
        this.getByIdData(id)
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'temporarySupplier',
        })
      } else {
        this.$router.push({
          name: 'temporarySupplier',
        })
      }
      this.parameter = {}
    },
    open(type) {
      this.$refs[type].add()
    },
    // 保存
    handleSave() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          let supplierCode = []
          let supplierId = []
          this.supplierList.forEach((item) => {
            supplierCode.push(item.code)
            supplierId.push(item.id)
          })
          let params = {
            ...this.parameter,
            supplierCode: supplierCode.join(','),
            supplierId: supplierId.join(','),
          }
          this.$http
            .post(
              '${portal}/supplier/bizSupplierEnterprise/v1/saveOrUpdateTempSupplier',
              params
            )
            .then((res) => {
              this.saveing = false
              if (res.data.state) {
                this.$confirm('保存成功，是否返回列表页?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'success',
                }).then(() => {
                  this.handleClose()
                })
              } else {
                this.loading = false
                this.$message.error('保存失败！')
              }
            })
        }
      })
    },
  },
}
</script>
  
  <style lang="scss"
    scoped>
/deep/ .el-row {
  padding: 0 240px;
}

// 步骤条
.theSteps {
  background-color: white;
  padding: 10px;
  border-radius: 10px;
  display: flex;
  justify-content: center;
  margin-top: 10px;

  /deep/.el-steps {
    width: 50%;
  }

  /deep/.el-steps--simple {
    background-color: rgba($color: #000000, $alpha: 0);
  }
}

.rows {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ebebeb;
  margin-bottom: 16px;
  padding: 10px 0;

  .rows-left {
    margin-right: 10px;

    .rows-hr {
      width: 3px;
      height: 18px;
      background-color: #0e6eff;
      border-radius: 2px;
    }
  }

  .rows-title {
    .text-overflow {
      width: 100%;
      font-size: 16px;
    }
  }
}
</style>
  