<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="12">
              <el-form-item label="企业名称" prop="enterpriseName">
                <el-input
                  v-model="parameter.enterpriseName"
                  placeholder=""
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="选择项目" prop="noticeName">
                <el-input  prefix-icon="el-icon-search" v-model="parameter.noticeName" @focus="handleInput" placeholder="请选择项目">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="供应商名称" prop="supplierName">
                <el-select v-model="parameter.supplierName" style="width: 100%;" clearable filterable placeholder="请选择供应商">
                    <el-option
                        v-for="item in supplierList"
                        :key="item.companyId"
                        @click.native="seleteSupplier(item)"
                        :label="item.companyName"
                        :value="item.companyId">
                    </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="记录时间" prop="recordTime">
                <el-date-picker
                  style="width: 100%"
                  v-model="parameter.recordTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="请选择记录时间"
                ></el-date-picker>
              </el-form-item>
            </el-col>
            
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="企业联系人" prop="enterpriseContact">
                <el-input
                  v-model="parameter.enterpriseContact"
                  placeholder="请输入企业联系人"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="企业联系电话" prop="enterpriseContactPhone">
                <el-input
                  v-model="parameter.enterpriseContactPhone"
                  placeholder="请输入企业联系电话"
                ></el-input>
              </el-form-item>
            </el-col>
            
          </el-row>

          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item
                label="企业联系地址"
                prop="enterpriseContactAddress"
              >
                <el-input
                  v-model="parameter.enterpriseContactAddress"
                  placeholder="请输入企业联系地址"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="问题类别" prop="problemCategory">
                <dict-select
                  dictCode="gyswtlb"
                  :hasDefaultOption="true"
                  v-model="parameter.problemCategory"
                  style="width: 100%"
                  clearable
                  placeholder="请选择问题类别"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="问题描述" prop="problemDescription">
                <el-input
                  clearable
                  type="textarea"
                  :autosize="{minRows: 2, maxRows: 4}"
                  v-model="parameter.problemDescription"
                  placeholder="请输入问题描述"
                ></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"
                  :before-upload="beforeUpload"
                  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>
    <!-- 选择立项项目 -->
    <noticeDialog ref="notice" :useOrgList="noticeList" @addDetail="load"></noticeDialog>
  </div>
</template>
  
  <script>
import supplierManger from '@/api/supplierManger.js'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import {mapState} from 'vuex'
import Breadcrumb from '@/components/Breadcrumb/index.vue'
import noticeDialog from './noticeDialog.vue'
import utils from '@/utils.js'
import moment from 'moment'
export default {
  name: 'domesticDialog',
  components: {
    Breadcrumb,
    dictUtils,
    noticeDialog
  },
  props: {
    editData: Object,
  },
  data() {
    return {
      saveing: false,
      breadcrumbOpt: [{breadcrumbName: '问题清单'}, {breadcrumbName: '新增'}],
      rules: {
        enterpriseName: [
          {
            required: true,
            message: '请选择企业',
            trigger: ['blur', 'change'],
          },
        ],
        supplierName: [
          {
            required: true,
            message: '请选择供应商',
            trigger: ['blur', 'change'],
          },
        ],
        problemDescription: [
          {
            required: true,
            message: '请输入问题描述',
            trigger: ['blur', 'change'],
          },
        ],
        recordTime: [
          {
            required: true,
            message: '请选择记录时间',
            trigger: ['blur', 'change'],
          },
        ],
        problemCategory: [
          {
            required: true,
            message: '请选择问题类别',
            trigger: ['blur', 'change'],
          },
        ],
        noticeName: [
          {
            required: true,
            message: '请选择项目',
            trigger: ['blur', 'change'],
          },
        ],
      },
      parameter: {
        recordTime: '',
        supplierName: '',
        enterpriseName: '',
        problemCategory: 1,
        accessoryList: [],
        noticeName:'',
        noticeId:'',
        noticeCode:'',
        noticeType:''
      },
      options: [],
      suppliersQualificationList: [],
      enterpriseList: [],
      supplierList: [],
      loading: false,
      currentUser:{},
      noticeList:[]
    }
  },
  created() {
    let moment = require('moment')
    this.parameter.recordTime = moment().format('YYYY-MM-DD HH:mm:ss')
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.parameter.enterpriseName =this.currentUser.org[0].orgName
    this.parameter.enterpriseId  = this.currentUser.org[0].orgId
    this.parameter.enterpriseCode = this.currentUser.org[0].orgCode
    console.log(this.parameter)
    this.addModifyBtn()
  },
  computed: {
    ...mapState({
      headers: (state) => {
        return {
          Authorization: `Bearer ${state.login.currentUser.token}`,
        }
      },
      action: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
  },
  methods: {
    handleSuccess(res, file, fileList) {
      this.parameter.accessoryList = fileList
    },
    beforeUpload(file) {
      let acceptList = ['xlsx', 'xls','png','jpg','jpeg','xlsx','xls','pdf','docx','doc','zip','rar']
      // 根据文件名获取文件的后缀名
      let fileType = file.name.split('.').pop().toLowerCase()
      // 判断文件格式是否符合要求
      if (acceptList.indexOf(fileType) === -1) {
        this.$message.error('请上传 .png,.jpg,.jpeg,.xlsx,.xls,pdf,.docx,.doc,.zip,.rar 格式的文件 !')
        return false
      }
    },
    getSupplierList() {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [],
      }
      params.querys.push({
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'noticeId',
        relation: 'AND',
        value: this.parameter.noticeId,
      })
      this.$http
        .post(
          '${portal}/userParticipation/v1/pageAll',
          params
        )
        .then((res) => {
          this.supplierList = res.data.value.rows
        })
    },
    //下载按钮回调
    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)
      })
    },
    load(data){
      this.parameter.noticeName = data[0].noticeTitle
      this.parameter.noticeId = data[0].id
      this.parameter.noticeCode = data[0].projectNumber
      this.parameter.noticeType = data[0].noticeType
      this.noticeList = data
      this.getSupplierList()
    },
    seleteSupplier(val){
      this.parameter.supplierName = val.companyName
      this.parameter.supplierId = val.companyId
      this.parameter.supplierCode = val.companyCode
    },
    getByIdData(id) {
      supplierManger.getByIdManage(id).then((res) => {
        console.log(res, 'ress')
        this.loading = false
        let data = res.data
        this.parameter = data
      })
    },
    handleInput(){
      this.$refs.notice.add()
    },
    //编辑
    addModifyBtn() {
      let {id} = this.$route.params
      if (id) {
        this.loading = true
        this.getByIdData(id)
        this.breadcrumbOpt[1].breadcrumbName = '编辑'
      } else {
        this.breadcrumbOpt[1].breadcrumbName = '新增'
      }
    },
    // 关闭//面包屑点击
    handleClose() {
      if (this.breadcrumbOpt[1].breadcrumbName === '编辑') {
        this.$router.push({
          name: 'questionsList',
        })
      } else {
        this.$router.push({
          name: 'questionsList',
        })
      }
    },
    // 保存
    handleSave() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.saveing = true
          let params = this.parameter
          this.$http
            .post(
              '${portal}/supplierIssueManagement/v1/saveOrUpdateEntity',
              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>
  