<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
	  draggable
	  width="80%"
      @close="close"
      @keyup.enter.native="doSubmit"
	  v-model="visible">
      <el-form size="large"
               :model="inputForm"
               ref="inputForm"
               :class="method==='view'?'readonly':''"
               :disabled="method==='view'"
               v-loading="loading"
               label-width="150px">
        <el-row  :gutter="15">
          <el-col :span="12">
            <el-form-item label="客户名称" prop="customerName"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.customerName" placeholder="请填写客户名称"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName"
                          :rules="[
                  {required: true, message:'项目名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.projectName" placeholder="请填写项目名称"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年份" prop="year"
                          :rules="[
                  {required: true, message:'年份不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.year" placeholder="请填写年份"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评估目的" prop="evaluationObjective"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.evaluationObjective" placeholder="请填写评估目的"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目类型" prop="projectType"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.projectType" placeholder="请填写项目类型"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评估基准日" prop="evaluationBaseDateUi"
                          :rules="[

                 ]">
<!--              {required: true, message:'评估基准日不能为空', trigger:'blur'}-->
<!--              <el-input v-model="inputForm.evaluationBaseDate" placeholder="请填写评估基准日"     ></el-input>-->
              <el-date-picker
                style="width:100%;"
                v-model="inputForm.evaluationBaseDateUi"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评估报告日" prop="evaluationReportDateUi"
                          :rules="[

                 ]">
<!--              {required: true, message:'评估报告日不能为空', trigger:'blur'}-->
<!--              <el-input v-model="inputForm.evaluationReportDate" placeholder="请填写评估报告日"     ></el-input>-->
              <el-date-picker
                style="width:100%;"
                v-model="inputForm.evaluationReportDateUi"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="协议号" prop="protocolNum"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.protocolNum" placeholder="请填写协议号"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文号" prop="documentNum"
                          :rules="[
                  {required: true, message:'文号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.documentNum" placeholder="请填写文号"     ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item prop="projectHead" :rules=" [{required: true, message: '项目负责人不能为空', trigger: 'blur'}]" label="项目负责人">
              <SelectUserTree
                ref="companyTree"
                :props="{
                    value: 'id',             // ID字段名
                    label: 'name',         // 显示名称
                    children: 'children'    // 子级字段名
                  }"
                :url="`/system-server/sys/user/treeUserDataAllOffice?type=2`"
                :value="inputForm.projectHead"
                :clearable="true"
                :accordion="true"
				size="large"
                :disabled="method==='view'"
                @getValue="(value) => {inputForm.projectHead=value}"/>
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="废旧物资评估(万元)" prop="waystEvaluation"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.waystEvaluation" placeholder="请填写废旧物资评估" @keyup.native="inputForm.waystEvaluation = twoDecimalPlaces(inputForm.waystEvaluation)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="固定资产评估(万元)" prop="fixedAssetsEvaluation"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.fixedAssetsEvaluation" placeholder="请填写固定资产评估" @keyup.native="inputForm.fixedAssetsEvaluation = twoDecimalPlaces(inputForm.fixedAssetsEvaluation)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="净资产评估" prop="netAssetsEvaluation"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.netAssetsEvaluation" placeholder="请填写净资产评估" @keyup.native="inputForm.netAssetsEvaluation = twoDecimalPlaces(inputForm.netAssetsEvaluation)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租金评估(万/年)" prop="rentEvaluation"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.rentEvaluation" placeholder="请填写租金评估" @keyup.native="inputForm.rentEvaluation = twoDecimalPlaces(inputForm.rentEvaluation)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司法鉴定" prop="forensics"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.forensics" placeholder="请填写司法鉴定"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报告收费(元)" prop="reportCharges"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.reportCharges" placeholder="请填写报告收费" @keyup.native="inputForm.reportCharges = twoDecimalPlaces(inputForm.reportCharges)"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item prop="currentDisposePerson" :rules=" [{trigger: 'blur'}]" label="当前处理人">
              <SelectUserTree
                ref="companyTree"
                :props="{
                    value: 'id',             // ID字段名
                    label: 'name',         // 显示名称
                    children: 'children'    // 子级字段名
                  }"
                :url="`/system-server/sys/user/treeUserDataAllOffice?type=2`"
                :value="inputForm.currentDisposePerson"
                :clearable="true"
                :accordion="true"
				size="large"
                :disabled="method==='view'"
                @getValue="(value) => {inputForm.currentDisposePerson=value}"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="是否开票" prop="isInvoice"
                          :rules="[
                 ]">
              <el-radio-group v-model="inputForm.isInvoice">
                <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票日期" prop="invoiceDateUi"
                          :rules="[
                 ]">
<!--              {required: true, message:'开票日期不能为空', trigger:'blur'}-->
              <!--              <el-input v-model="inputForm.invoiceDate" placeholder="请填写开票日期"     ></el-input>-->
              <el-date-picker
                style="width:100%;"
                v-model="inputForm.invoiceDateUi"
                type="datetime"
				value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预估/实际收费(万元）" prop="actualCharges"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.actualCharges" placeholder="请填写预估/实际收费" @keyup.native="inputForm.actualCharges = twoDecimalPlaces(inputForm.actualCharges)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同是否存档" prop="isContractArchive"
                          :rules="[
                 ]">
<!--              <el-input v-model="inputForm.isContractArchive" placeholder="请填写合同是否存档"     ></el-input>-->
              <el-radio-group v-model="inputForm.isContractArchive">
                <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="底稿是否完好" prop="isPapersIntact"
                          :rules="[
                 ]">
<!--              <el-input v-model="inputForm.isPapersIntact" placeholder="请填写底稿是否完好"     ></el-input>-->
              <el-radio-group v-model="inputForm.isPapersIntact">
                <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="底稿是否归档" prop="isPapersArchive"
                          :rules="[
                 ]">
<!--              <el-input v-model="inputForm.isPapersArchive" placeholder="请填写底稿是否归档"     ></el-input>-->
              <el-radio-group v-model="inputForm.isPapersArchive">
                <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报销外勤天数" prop="opsAmount"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.opsAmount" placeholder="请填写报销外勤天数" @keyup.native="inputForm.opsAmount = positiveInteger(inputForm.opsAmount)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="外勤是否已经报销" prop="isOpsReimbursement"
                          :rules="[
                 ]">
<!--              <el-input v-model="inputForm.isOpsReimbursement" placeholder="请填写外勤是否已经报销"     ></el-input>-->
              <el-radio-group v-model="inputForm.isOpsReimbursement">
                <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否已经报销提成" prop="isCommissionReimbursement"
                          :rules="[
                 ]">
<!--              <el-input v-model="inputForm.isCommissionReimbursement" placeholder="请填写是否已经报销提成"     ></el-input>-->
              <el-radio-group v-model="inputForm.isCommissionReimbursement">
                <el-radio v-for="item in $dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="已报销金额" prop="reimbursementAmount"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.reimbursementAmount" placeholder="请填写已报销金额" @keyup.native="inputForm.reimbursementAmount = twoDecimalPlaces(inputForm.reimbursementAmount)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="未报销金额" prop="unreimbursedAmount"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.unreimbursedAmount" placeholder="请填写未报销金额" @keyup.native="inputForm.unreimbursedAmount = twoDecimalPlaces(inputForm.unreimbursedAmount)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报销单号" prop="reimbursementNum"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.reimbursementNum" placeholder="请填写报销单号"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报销日期" prop="reimbursementDateUi"
                          :rules="[
                 ]">
<!--              <el-input v-model="inputForm.reimbursementDate" placeholder="请填写报销日期"     ></el-input>-->
              <el-date-picker
                style="width:100%;"
                v-model="inputForm.reimbursementDateUi"
                type="datetime"
				value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="签字评估师1" prop="evaluationPersonOne"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.evaluationPersonOne" placeholder="请填写签字评估师1"     ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="签字评估师2" prop="evaluationPersonTwo"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.evaluationPersonTwo" placeholder="请填写签字评估师2"     ></el-input>
            </el-form-item>
          </el-col>

          <!--<el-col :span="12">
            <el-form-item label="项目状态" prop="status"
                          :rules="[
                 ]">
&lt;!&ndash;              <el-input v-model="inputForm.status" placeholder="请填写项目状态"     ></el-input>&ndash;&gt;
              <el-select v-model="inputForm.status" placeholder="请选择" style="width:100%;">
                <el-option
                  v-for="item in $dictUtils.getDictList('project_status')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>-->

        </el-row>
        <!--        附件-->
		  <div>
			  <UpLoadComponent ref="uploadComponent"></UpLoadComponent>
		  </div>

      </el-form>
<!--      <el-image-viewer-->
<!--        v-if="showViewer"-->
<!--        :on-close="closeViewer"-->
<!--        :url-list="[url]"-->
<!--        zIndex="9999"/>-->
		<template #footer>
			<span class="dialog-footer">
				<el-button  @click="close()" icon="el-icon-circle-close">关闭</el-button>
				<el-button  type="primary" v-if="method != 'view'"  @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
			</span>
		</template>
    </el-dialog>
  </div>
</template>

<script>
  import ProjectService from '@/api/sys/ProjectService'
  import SelectUserTree from '@/views/utils/treeUserSelect'
  import UpLoadComponent from '@/views/common/UpLoadComponent'
  import OSSSerive from '@/api/sys/OSSService'
  // import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
  export default {
    data () {
      return {
        dataList: [],
        dataListNew: [],
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          permissionFlag: '', // 判断是否为当前登录人参数
          customerName: '',
          projectName: '',
          year: '',
          evaluationObjective: '',
          projectType: '',
          evaluationBaseDate: '',
          evaluationBaseDateUi: '',
          evaluationReportDate: '',
          evaluationReportDateUi: '',
          protocolNum: '',
          documentNum: '',
          projectHead: '',
          waystEvaluation: '',
          fixedAssetsEvaluation: '',
          netAssetsEvaluation: '',
          rentEvaluation: '',
          forensics: '',
          reportCharges: '',
          currentDisposePerson: '',
          isInvoice: '',
          invoiceDate: '',
          invoiceDateUi: '',
          actualCharges: '',
          isContractArchive: '',
          isPapersIntact: '',
          isPapersArchive: '',
          opsAmount: '',
          isOpsReimbursement: '',
          reimbursementAmount: '',
          unreimbursedAmount: '',
          reimbursementNum: '',
          reimbursementDate: '',
          reimbursementDateUi: '',
          evaluationPersonOne: '',
          evaluationPersonTwo: '',
          status: '',
          assessReportMessage: '',
          assessBaseMessage: '',
          invoiceMessage: '',
          isCommissionReimbursement: '',
          itemType: '',
          workAttachments: []
        },
        filesArra2: [],
        fileList: [],
        isFlag: true,
        showViewer: false, // 显示查看器
        url: '',
        rowurl: '',
        src: '',
        showVi: true,
        loadProgress: 0, // 动态显示进度条
        progressFlag: false // 关闭进度条
      }
    },
    components: {
      // ElImageViewer,
      SelectUserTree,
      UpLoadComponent
    },
    projectService: null,
    ossService: null,
    created () {
      this.ossService = new OSSSerive()
      this.projectService = new ProjectService()
    },
    mounted () {
    },
    methods: {
      // 关闭查看器
      closeViewer () {
        this.url = ''
        this.showViewer = false
      },
      async init (method, id) {
        this.dataList = []
        this.dataListNew = []
        await this.ossService.findFileList(id).then((data) => {
          data.forEach((item) => {
            item.name = item.attachmentName
            // item.createBy = item.createBy
            // item.createDate = item.createDate
            // item.description = item.description
            this.dataList.push(item)
            this.dataListNew.push(item)
          })
        })
        this.method = method
        this.inputForm = {
          customerName: '',
          projectName: '',
          year: '',
          evaluationObjective: '',
          projectType: '',
          evaluationBaseDate: '',
          evaluationBaseDateUi: '',
          evaluationReportDate: '',
          evaluationReportDateUi: '',
          protocolNum: '',
          documentNum: '',
          projectHead: '',
          waystEvaluation: '',
          fixedAssetsEvaluation: '',
          netAssetsEvaluation: '',
          rentEvaluation: '',
          forensics: '',
          reportCharges: '',
          currentDisposePerson: '',
          isInvoice: '',
          invoiceDate: '',
          invoiceDateUi: '',
          actualCharges: '',
          isContractArchive: '',
          isPapersIntact: '',
          isPapersArchive: '',
          opsAmount: '',
          isOpsReimbursement: '',
          reimbursementAmount: '',
          unreimbursedAmount: '',
          reimbursementNum: '',
          reimbursementDate: '',
          reimbursementDateUi: '',
          evaluationPersonOne: '',
          evaluationPersonTwo: '',
          status: '',
          assessReportMessage: '',
          assessBaseMessage: '',
          invoiceMessage: '',
          isCommissionReimbursement: '',
          itemType: ''
        }
        this.inputForm.id = id
        if (method === 'add') {
          this.inputForm.permissionFlag = true
          this.title = `新建项目`
        } else if (method === 'edit') {
          this.title = '修改项目信息'
        } else if (method === 'view') {
          this.showVi = false
          this.title = '查看项目详情'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
			  this.$refs.uploadComponent.clearUpload()
              this.projectService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.inputForm.evaluationBaseDateUi = data.evaluationBaseDate
              this.inputForm.evaluationReportDateUi = data.evaluationReportDate
              this.inputForm.invoiceDateUi = data.invoiceDate
              this.inputForm.reimbursementDateUi = data.reimbursementDate
              this.inputForm.permissionFlag = data.permissionFlag
              this.inputForm = JSON.parse(JSON.stringify(this.inputForm))
              this.$refs.uploadComponent.newUpload(method, this.dataListNew, 'projectRecords', 1024)
              this.loading = false
            })
          }
          if (method !== 'edit' && method !== 'view') {
            this.$refs.uploadComponent.newUpload(method, [], 'projectRecords', 1024)
          }
        })
      },
      // 表单提交
      doSubmit () {
        if (this.$refs.uploadComponent.checkProgress()) {
          this.loading = false
          return
        }
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.inputForm.itemType = '2'
            this.inputForm.workAttachments = []
            this.dataListNew = this.$refs.uploadComponent.getDataList()
            this.dataListNew.forEach((item) => {
              if (item.id === null || item.id === undefined || item.id === '') {
                item.url = item.raw.url
              }
              item.attachmentFlag = 'projectRecords'
              item.fileSize = item.size
              item.attachmentName = item.name
              item.createBy = null
              this.inputForm.workAttachments.push(item)
            })
            if (this.inputForm.evaluationReportDateUi === null || this.inputForm.evaluationReportDateUi === undefined || this.inputForm.evaluationReportDateUi === '') {
              this.inputForm.status = '1'
            } else {
              this.inputForm.status = '5'
            }
            this.projectService.save(this.inputForm).then((data) => {
              this.close()
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      close () {
        this.$refs.uploadComponent.clearUpload()
        this.$refs.inputForm.resetFields()
        this.visible = false
        this.showVi = true
        this.$emit('refreshDataList')
      },
      // 排序
      sortChangeHandle (column) {
        this.orders = []
        if (column.order != null) {
          this.orders.push({column: this.$utils.toLine(column.prop), asc: column.order === 'ascending'})
        }
        this.refreshList()
      },
      deleteMsgById (row, index) {
        this.dataListNew.splice(index, 1)
        if (row.id !== null && row.id !== '' && row.id !== undefined) {
          this.ossService.deleteMsgById(row.id)
        }
      },
      twoDecimalPlaces (num) {
        let str = num.toString()
        var len1 = str.substr(0, 1)
        var len2 = str.substr(1, 1)
        // eslint-disable-next-line eqeqeq
        if (str.length > 1 && len1 == 0 && len2 != '.') {
          str = str.substr(1, 1)
        }
        // eslint-disable-next-line eqeqeq
        if (len1 == '.') {
          str = ''
        }
        // eslint-disable-next-line eqeqeq
        if (str.indexOf('.') != -1) {
          var str_ = str.substr(str.indexOf('.') + 1)
          // eslint-disable-next-line eqeqeq
          if (str_.indexOf('.') != -1) {
            str = str.substr(0, str.indexOf('.') + str_.indexOf('.') + 1)
          }
          if (str_.length > 4) {
            this.$message.warning(`金额小数点后只能输入四位，请正确输入！`)
            return (str = '')
          }
        }
        // eslint-disable-next-line no-useless-escape
        str = str.replace(/[^\d^\.]+/g, '') // 保留数字和小数点
        return str
      },
      positiveInteger (num) {
        let str = num.toString()
        var len1 = str.substr(0, 1)
        var len2 = str.substr(1, 1)
        // eslint-disable-next-line eqeqeq
        if (str.length > 1 && len1 == 0 && len2 != '.') {
          str = str.substr(1, 1)
        }
        // eslint-disable-next-line eqeqeq
        if (len1 == '.') {
          str = ''
        }
        // eslint-disable-next-line no-useless-escape
        str = str.replace(/[^\d^]+/g, '') // 保留数字
        return str
      }
    }
  }
</script>
<style scoped>
  .avatar{
    height: 100px;
  }
  .el-divider__text {
    font-weight: bold;
    font-size: 16px;
  }
</style>
