<template>
  <a-card :bordered="false" v-show="visible" class="card" :title="modalTitle">
    <a-row :gutter="48" slot="extra">
      <a-col :md="48" :sm="48">
        <span class="table-page-search-submitButtons" style="float: right">
          <a-button type="primary" @click="save()">提交</a-button>
          <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
        </span>
      </a-col>
    </a-row>

    <title-divider title="基本信息" width="90px"></title-divider>
    <a-form :form="form">
      <a-form-item v-show="false" >
        <a-input v-decorator="['id']" type="hidden"/>
        <a-input v-decorator="['parentId']" type="hidden"/>
      </a-form-item>

      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="设备（新号）"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['no', {rules: [{required: true, message: '不能为空'}]}]" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="设备（旧号）"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['zbh', {rules: [{required: false, message: '不能为空'}]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="设备名称"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['name', {rules: [{required: true, message: '设备名称不能为空'}]}]" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="型号"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['model', {rules: [{required: true, message: '设备型号不能为空'}]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="设备等级"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['level', {initialValue:1, rules: [{required: true, message: '设备等级不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in levelMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="设备价格"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"

          >
            <a-input-number
              style="width: 100%"
              :min="0"
              :step="0.01"
              :formatter="BaseTool.Amount.formatter"
              :parser="BaseTool.Amount.parser"
              v-decorator="['initialValue', {rules: [{required: true, message: '设备原值不能为空'}]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="设备类型"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-tree-select
              style="width: 100%"
              :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
              :treeData="sbTypeTreeData"
              :treeNodeFilterProp="'title'"
              :showSearch="true"
              v-decorator="['typeId', {rules: [{required: true, message: '设备类型不能为空'}]}]"
              placeholder="请选择"
            >
            </a-tree-select>
          </a-form-item>
        </a-col>

        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="自定义类型"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select @change="handleChange" v-decorator="['useType', {rules: [{required: true, message: '自定义类型不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in useTypeMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="资产"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['financingNo']" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="状态"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['status', {rules: [{required: true, message: '状态不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="(label,value) in statusMap"
                :key="value"
                :label="label"
                :value="parseInt(value)">{{ label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="大小/尺寸"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['zz']" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="系列号(出厂编号)"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['zzh']" />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="房间"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['cph']" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="生产商"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              style="width: 70%"
              disabled
              v-decorator="['producerName', {rules: [{required: true, message: '生产商不能为空'}]}]" />
            <a-button style="width: 30%" type="primary" @click="handleProducerSelect">选择</a-button>
          </a-form-item>
          <a-form-item
            label="生产商"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
            v-show="false"
          >
            <a-input
              v-decorator="['producerId', {rules: [{required: true, message: '生产商不能为空'}]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="父设备"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              disabled
              style="width: 80%"
              v-decorator="['parentSbName']"/>
            <a-button type="primary" style="width: 20%" @click="handleSbSelect">选择</a-button>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="是否子设备"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['isChild']" placeholder="请选择">
              <a-select-option value="1">子设备</a-select-option>
              <a-select-option value="2">父设备</a-select-option>
              <a-select-option value="3">普通设备</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>

      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="存放位置"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              style="width: 100%"
              v-decorator="['positionId']" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="是否显示"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['isShow']" placeholder="请选择">
              <a-select-option value="0">不显示</a-select-option>
              <a-select-option value="1">显示</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="大屏水平位置"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['zjm']"
              placeholder="请输入水平位置，如300"/>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="大屏垂直位置"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['jbdh']"
              placeholder="请输入垂直位置，如200"/>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="备注"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-input
              v-decorator="['remark', {rules: [{required: false, message: '备注不能为空'}]}]" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="使用机台"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['saveUser', {rules: [{required: true, message: '项目部不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="({userId, realName}) in userList"
                :key="userId"
                :label="realName"
                :value="userId">{{ realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <!--      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="所属区域"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select @change="handleAreaChange" v-decorator="['useArea', {rules: [{required: true, message: '所属区域不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="({deptId, name}) in areaList"
                :key="deptId"
                :label="name"
                :value="deptId">{{ name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="所属公司"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select @change="handleCompanyChange" v-decorator="['useCompany', {rules: [{required: true, message: '所属公司不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="({deptId, name}) in companyList"
                :key="deptId"
                :label="name"
                :value="deptId">{{ name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">

        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="所属项目部"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select @change="handleProjectChange" v-decorator="['useProject', {rules: [{required: true, message: '项目部不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="({deptId, name}) in projectList"
                :key="deptId"
                :label="name"
                :value="deptId">{{ name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="所属部门"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select @change="handleDeptChange" v-decorator="['useDept', {rules: [{required: true, message: '所属公司不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="({deptId, name}) in deptList"
                :key="deptId"
                :label="name"
                :value="deptId">{{ name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>-->
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <!--        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="所属班组"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select @change="handleGroupChange" v-decorator="['useGroup', {rules: [{required: true, message: '项目部不能为空'}]}]" placeholder="请选择">
              <a-select-option
                v-for="({deptId, name}) in groupList"
                :key="deptId"
                :label="name"
                :value="deptId">{{ name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>-->
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="维修人"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['repairUser']" placeholder="请选择">
              <a-select-option
                v-for="({userId, realName}) in userList"
                :key="userId"
                :label="realName"
                :value="userId">{{ realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="第二维修人员"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-select v-decorator="['repairUserSecond']" placeholder="请选择">
              <a-select-option
                v-for="({userId, realName}) in userList"
                :key="userId"
                :label="realName"
                :value="userId">{{ realName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="购置日期"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
              v-decorator="['buyDate']" />
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="投用日期"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-date-picker
              style="width: 100%"
              :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
              v-decorator="['startDate']" />
          </a-form-item>
        </a-col>
      </a-row>
      <title-divider v-show="useType == 5" title="特种信息" width="90px"></title-divider>
      <div v-show="useType == 5" >
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="注册代码"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['registerNo']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="介质属性"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['characterType']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="设备批号"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['batchNo']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="使用登记证编号"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['usedNo']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="维护供应商"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['supplier']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="登记日期"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-date-picker
                style="width: 100%"
                :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                v-decorator="['registerDate']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="预计工作年限"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                style="width: 100%"
                suffix="年"
                :formatter="BaseTool.Amount.formatter"
                :parser="BaseTool.Amount.parser"
                v-decorator="['workYear']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="设计到期年限"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-date-picker
                style="width: 100%"
                :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                v-decorator="['retirementDate']" />
            </a-form-item>
          </a-col>
        </a-row>
      </div>
      <title-divider v-show="useType == 4" title="计量信息" width="90px"></title-divider>
      <div v-show="useType == 4" >
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="测量设备编号"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['cardNo']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="准确度等级"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['fdjxh']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="检定单位"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['dph']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="检定人"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['zaiz']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="检定结论"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-select v-decorator="['rlType']" placeholder="请选择">
                <a-select-option
                  v-for="(label,value) in rlTypeMap"
                  :key="value"
                  :label="label"
                  :value="parseInt(value)">{{ label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="是否周检查"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-select v-decorator="['color']" placeholder="请选择">
                <a-select-option
                  v-for="(label,value) in colorMap"
                  :key="value"
                  :label="label"
                  :value="parseInt(value)">{{ label }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="检定证书编号"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                v-decorator="['sbdh']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="预警天数"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input-number
                style="width: 100%"
                :min="0"
                :step="0.01"
                :formatter="BaseTool.Amount.formatter"
                :parser="BaseTool.Amount.parser"
                v-decorator="['seatNumber']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="检定日期"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-date-picker
                style="width: 100%"
                :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                v-decorator="['checkDate']" />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="检定周期"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                style="width: 100%"
                suffix="月"
                :formatter="BaseTool.Amount.formatter"
                :parser="BaseTool.Amount.parser"
                v-decorator="['checkPeriod']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="有效日期至"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-date-picker
                style="width: 100%"
                :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                v-decorator="['nextCheckDate']" />
            </a-form-item>
          </a-col>
        </a-row>
      </div>
      <title-divider title="文件信息" width="90px"></title-divider>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="维保手册"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              :file-list="this.defaultRepairFileList"
              @change="handleRepairFileChange"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 选择上传文件 </a-button>
            </a-upload>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="使用手册"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              :file-list="this.defaultUseFileList"
              @change="handleUseFileChange"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 选择上传文件 </a-button>
            </a-upload>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="验收文件"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              :file-list="this.defaultRecheckFileList"
              @change="handleRecheckFileChange"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 选择上传文件 </a-button>
            </a-upload>
          </a-form-item>
        </a-col>
        <a-col :lg="12" :md="24" :sm="24">
          <a-form-item
            label="设备图片"
            :labelCol="BaseTool.Constant.labelCol"
            :wrapperCol="BaseTool.Constant.wrapperCol"
          >
            <a-upload
              :action="uploadUrl"
              :multiple="true"
              :file-list="this.defaultSbFileList"
              @change="handleSbFileChange"
              accept="image/*"
              :headers="headers"
            >
              <a-button> <a-icon type="upload" /> 选择上传文件 </a-button>
            </a-upload>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <sb-model-select-modal ref="sbModelSelectModal" @selected="handleSbModelSelected"/>
    <sb-info-select-modal ref="sbInfoSelectModal" @selected="handleSbSelectd"/>
    <firm-producer-select-modal ref="producerSelectModal" @selected="handleProducerSelected"/>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { addSbInfo, updateSbInfo, copySbInfo } from '@/api/sb/info'
import { fetchSbTypeTree } from '@/api/sb/type'
import FirmProducerSelectModal from '@/views/firm/producer/modules/FirmProducerSelectModal'
import SbGuigeSelectModal from '@/views/sb/guige/modules/SbGuigeSelectModal'
import SbModelSelectModal from '@/views/sb/model/modules/SbModelSelectModal'
import BaseTool from '../../../../utils/tool'
import { queryUserDeptByUserDeptId } from '@/api/upms/user-dept'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import Vue from 'vue'
import { uploadUrl } from '@/api/upms/file'
import { queryDept, getDeptsAllByParentId } from '@/api/upms/dept'
import { queryUser } from '@/api/upms/user'

import SbInfoSelectModal from '@/views/sb/info/modules/SbInfoSelectModal'

export default {
  name: 'BaseSbInfo',
  components: {
    FirmProducerSelectModal,
    SbGuigeSelectModal,
    SbModelSelectModal,
    SbInfoSelectModal
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      form: this.$form.createForm(this),
      areaList: {},
      companyList: {},
      projectList: {},
      deptList: {},
      groupList: {},
      visible: false,
      sbTypeTreeData: [],
      // sbInfoTreeData: [],
      // sourceSbInfoTreeData: [],
      // sbPositionTreeData: [],
      depreciationTypeMap: {},
      sourceTypeMap: {},
      levelMap: {},
      rlTypeMap: {},
      colorMap: {},
      useTypeMap: {},
      statusMap: {},
      userList: {},
      unitMap: {},
      sbInfoSelectType: null,
      repairFileList: [], // 维修手册
      defaultRepairFileList: [],
      defaultUseFileList: [],
      defaultRecheckFileList: [],
      defaultSbFileList: [],
      useFileList: [], // 使用手册
      recheckFileList: [], // 验收手册
      sbFileList: [], // 设备图片,
      uploadUrl: uploadUrl,
      qrcodesrc: null,
      fileList: [],
      type: null,
      headers: {
        Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)
      }
    }
  },
  props: {
    useType: {
      type: Number,
      default: 3
    }
  },
  created () {
    this.depreciationTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_DEPRECIATIONTYPE)
    this.levelMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_LEVEL)
    this.colorMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_COLOR)
    this.rlTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.RANLIAO_TYPE)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_INFO_STATUS)
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.sourceTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBTYPE_SOURCETYPE)
    this.useTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SB_USE_TYPE)
  },
  methods: {
    base (record, type) {
      this.setTree(record)
      // 清除文件列表
      this.visible = true
      // 选择框查询条件
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.qrcodesrc = null
        this.modalTitle = '添加'
        this.clearFileList()
        return
      }
      this.useType = record.useType
      this.modalTitle = '编辑'
      this.qrcodesrc = record.qrCode
      this.repairFileList = record.repairFileList
      this.useFileList = record.useFileList
      this.recheckFileList = record.recheckFileList
      this.sbFileList = record.sbFileList
      this.defaultRepairFileList = this.BaseTool.UPLOAD.transImg(this.repairFileList)
      this.defaultUseFileList = this.BaseTool.UPLOAD.transImg(this.useFileList)
      this.defaultRecheckFileList = this.BaseTool.UPLOAD.transImg(this.recheckFileList)
      this.defaultSbFileList = this.BaseTool.UPLOAD.transImg(this.sbFileList)

      if (type === 1) {
        this.type = type
        this.modalTitle = '复制'
        this.qrcodesrc = null
        record.no = null
        this.clearFileList()
      }
      if (record.isChild === 1) {
        record.isChild = '1'
      }
      if (record.isChild === 2) {
        record.isChild = '2'
      }
      if (record.isChild === 3) {
        record.isChild = '3'
      }

      if (record.isShow === 0) {
        record.isShow = '0'
      }
      if (record.isShow === 1) {
        record.isShow = '1'
      }

      const { form: { setFieldsValue } } = this
      // 日期处理
      // record.buyDate = this.BaseTool.Moment(record.buyDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      // record.startDate = this.BaseTool.Moment(record.startDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      // record.nextCheckDate = this.BaseTool.Moment(record.nextCheckDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      // record.guaranteeDate = this.BaseTool.Moment(record.guaranteeDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      // record.retirementDate = this.BaseTool.Moment(record.retirementDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)

      // record.lastRepaireTime = this.BaseTool.Moment(record.lastRepaireTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      // record.lastBaoyangTime = this.BaseTool.Moment(record.lastBaoyangTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      if (record.buyDate != null) {
        record.buyDate = this.BaseTool.Moment(record.buyDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'buyDate'
          ])))
        })
      }
      if (record.startDate != null) {
        record.startDate = this.BaseTool.Moment(record.startDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'startDate'
          ])))
        })
      }
      if (record.checkDate != null) {
        record.checkDate = this.BaseTool.Moment(record.checkDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'checkDate'
          ])))
        })
      }
      if (record.nextCheckDate != null) {
        record.nextCheckDate = this.BaseTool.Moment(record.nextCheckDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'nextCheckDate'
          ])))
        })
      }
      if (record.retirementDate != null) {
        record.retirementDate = this.BaseTool.Moment(record.retirementDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'retirementDate'
          ])))
        })
      }
      if (record.registerDate != null) {
        record.registerDate = this.BaseTool.Moment(record.registerDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(record, [
            'registerDate'
          ])))
        })
      }
      record.unitName = this.BaseTool.Object.getField(this.unitMap, record.unit)
      record.useTypeName = this.BaseTool.Object.getField(this.useTypeMap, record.useType)

      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'no',
          'parentId',
          'parentSbName',
          'isChild',
          'isShow',
          'financingNo',
          'name',
          'model',
          'positionId',
          'producerName',
          'useArea',
          'useCompany',
          'useProject',
          'useDept',
          'saveUser',
          'repairUser',
          'useGroup',
          'typeId',
          'level',
          'initialValue',
          // 'cutValue',
          'producerId',
          // 'sourceType',
          'unit',
          'buyDate',
          'startDate',
          'zbh',
          // 'wbFile',
          // 'useFile',
          // 'checkFile',
          // 'sbImage',
          'status',
          'remark',
          'zzh',
          'zz',
          'zjm',
          'jbdh',
          'useType',

          'cardNo',
          'fdjxh',
          'checkDate',
          'checkPeriod',
          'nextCheckDate',
          'dph',
          'zaiz',
          'rlType',
          'color',
          'sbdh',
          'seatNumber',
          'registerNo',
          'characterType',
          'usedNo',
          'supplier',
          'repairUserSecond',
          'batchNo',
          'workYear',
          'retirementDate',
          'registerDate'

        ])))
      })
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期数据的处理
        values.buyDate = BaseTool.Date.formatter(values.buyDate, BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.startDate = BaseTool.Date.formatter(values.startDate, BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.checkDate = BaseTool.Date.formatter(values.checkDate, BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.nextCheckDate = BaseTool.Date.formatter(values.nextCheckDate, BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.guaranteeDate = BaseTool.Date.formatter(values.guaranteeDate, BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.retirementDate = BaseTool.Date.formatter(values.retirementDate, BaseTool.Date.PICKER_NORM_DATE_PATTERN)

        values.lastRepaireTime = this.BaseTool.Date.formatter(values.lastRepaireTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.lastBaoyangTime = this.BaseTool.Date.formatter(values.lastBaoyangTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.spDate = this.BaseTool.Date.formatter(values.spDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.jbdDate = this.BaseTool.Date.formatter(values.jbdDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.sbdDate = this.BaseTool.Date.formatter(values.sbdDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        values.sbDate = this.BaseTool.Date.formatter(values.sbDate, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)

        // 上传文件
        values.repairFileList = this.repairFileList
        values.useFileList = this.useFileList
        values.recheckFileList = this.recheckFileList
        values.sbFileList = this.sbFileList

        values.useType = this.useType
        if (this.BaseTool.String.isBlank(values.id)) {
          addSbInfo(values)
            .then(() => {
              this.handleCancel(values)
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          if (this.type == null) {
            updateSbInfo(values)
              .then(() => {
                this.handleCancel(values)
              }).catch(() => {
                this.confirmLoading = false
              })
          } else {
            copySbInfo(values)
              .then(() => {
                this.handleCancel(values)
              }).catch(() => {
                this.confirmLoading = false
              })
          }
        }
      })
    },
    getQrcodeSrc () {
      console.log(111, 22)
    },
    handleCancel () {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok')
    },
    setTree (record = {}) {
      fetchSbTypeTree().then(res => {
        this.sbTypeTreeData = res.data
      })
      // getSbInfoTree({ id: record.id }).then(res => {
      //  this.sbInfoTreeData = res.data
      //  this.sourceSbInfoTreeData = res.data
      // })
      // getSbPositionTree().then(res => {
      //  this.sbPositionTreeData = res.data
      // })
      queryDept({ nature: this.DictCache.VALUE.SYS_DEPT_NATURE.JITUAN }).then(res => {
        this.areaList = res.data
      })

      if (record.useArea != null) {
        getDeptsAllByParentId({ deptId: record.useArea, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.FEN_GONG_SI }).then(res => {
          this.companyList = res.data
        })
        getDeptsAllByParentId({ deptId: record.useCompany, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.XIANG_MU_BU }).then(res => {
          this.projectList = res.data
        })
        getDeptsAllByParentId({ deptId: record.useProject, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.BU_MEN }).then(res => {
          this.deptList = res.data
        })
        getDeptsAllByParentId({ deptId: record.useDept, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.BAN_ZU }).then(res => {
          this.groupList = res.data
        })
        queryUser({ }).then(res => {
          this.userList = res.data
        })
      }
      queryUser({ }).then(res => {
        this.userList = res.data
      })
    },
    handleSbModelSelect () {
      const modal = this.$refs.sbModelSelectModal
      modal.base()
    },
    handleSbModelSelected (keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this

      this.$nextTick(() => {
        setFieldsValue({
          'modelId': key,
          'name': row.name,
          'model': row.model,
          'nameModel': row.nameModel,
          'typeId': row.typeId,
          'typeName': row.typeName,
          'level': row.level,
          'initialValue': row.value,
          'useType': row.useType,
          'useTypeName': this.BaseTool.Object.getField(this.useTypeMap, row.useType),
          'producerId': row.producerId,
          'producerName': row.producerName,
          'unit': row.unit,
          'unitName': this.BaseTool.Object.getField(this.unitMap, row.unit)
        })
      })
    },

    beforeRecheckFileListUpload (file) {
      const reg = /\.(xls|xlsx)(\?.*)?$/
      return new Promise((resolve, reject) => {
        if (reg.test(file.name)) {
          this.recheckFileList = [file]
          return true
        } else {
          this.$message.error(`请上传正确的xls|xlsx文件`)
          reject(new Error('请上传正确的xls|xlsx文件'))
          return false
        }
      })
    },
    beforeUploadPic (file) {
      const reg = /\.(jpg|jpeg|png)(\?.*)?$/
      return new Promise((resolve, reject) => {
        if (reg.test(file.name)) {
          this.sbFileList = [file]
          return true
        } else {
          this.$message.error(`请上传正确的jpg|jpeg|png文件`)
          reject(new Error('请上传正确的jpg|jpeg|png文件'))
          return false
        }
      })
    },
    clearFileList () {
      this.repairFileList = []
      this.useFileList = []
      this.recheckFileList = []
      this.sbFileList = []
    },
    handleRepairFileChange (info) {
      this.defaultRepairFileList = info.fileList
      this.repairFileList = this.setFileList(info, 1)
    },
    handleUseFileChange (info) {
      this.defaultUseFileList = info.fileList
      this.useFileList = this.setFileList(info, 2)
    },
    handleAreaChange (value) {
      getDeptsAllByParentId({ deptId: value, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.FEN_GONG_SI }).then(res => {
        this.companyList = res.data
      })
    },
    handleCompanyChange (value) {
      getDeptsAllByParentId({ deptId: value, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.XIANG_MU_BU }).then(res => {
        this.projectList = res.data
      })
    },
    handleProjectChange (value) {
      getDeptsAllByParentId({ deptId: value, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.BU_MEN }).then(res => {
        this.deptList = res.data
      })
    },
    handleDeptChange (value) {
      getDeptsAllByParentId({ deptId: value, nature: this.DictCache.VALUE.SYS_DEPT_NATURE.BAN_ZU }).then(res => {
        this.groupList = res.data
      })
    },
    handleGroupChange (value) {
      queryUserDeptByUserDeptId({ deptId: value }).then(res => {
        this.userList = res.data
      })
    },
    handleRecheckFileChange (info) {
      this.defaultRecheckFileList = info.fileList
      this.recheckFileList = this.setFileList(info, 3)
    },
    handleSbFileChange (info) {
      this.defaultSbFileList = info.fileList
      this.sbFileList = this.setFileList(info, 4)
    },
    setFileList1 (info, type) {
      const fileList = [...info.fileList]
      const file = [...info.file]
      if (file.status === 'done') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } else if (file.status === 'removed') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } else if (file.status === 'error') {
        this.$message.error('上传失败')
        return []
      }
    },
    handleSbSelect () {
      this.$refs.sbInfoSelectModal.base({}, { isChild: this.DictCache.VALUE.SB_IS_CHILD.IS_PARENT })
    },
    handleSbSelectd (keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'parentId': key,
          'parentSbName': row.name,
          'isChild': this.DictCache.VALUE.SB_IS_CHILD.IS_CHILD
        }))
      })
    },
    handleChange (value) {
      this.useType = value
    },
    handleProducerSelect () {
      const modal = this.$refs.producerSelectModal
      modal.base({}, { type: 1 })
    },
    handleProducerSelected (record, selectedRowKeys, selectedRow) {
      const { form: { setFieldsValue } } = this
      const [ key ] = selectedRowKeys
      const [ row ] = selectedRow
      setFieldsValue({
        'producerId': key,
        'producerName': row.name
      })
    },
    setFileList (info, type) {
      // { file, fileList }

      const file = info.file
      const fileList = info.fileList
      // fileList = fileList.slice(-2)
      // console.log(111, this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type))

      if (file.status === 'done') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } else if (file.status === 'removed') {
        return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)
      } else if (file.status === 'error') {
        this.$message.error('上传失败')
        return null
      }

      // return this.BaseTool.UPLOAD.getUploadFileDTO(fileList, type)

      // 1. Limit the number of uploaded files
      //    Only to show two recent uploaded files, and old ones will be replaced by the new
      // fileList = fileList.slice(-2)

      // 2. read from response and show file link
      // fileList = fileList.map(file => {
      //   if (file.response) {
      //     // Component will show file.url as link
      //     const { response: { data } } = file
      //     file.url = data.url
      //     file.fileName = data.fileName
      //     file.fileFormat = data.fileFormat
      //     file.name = data.name
      //     file.type = type
      //   }
      //   return file
      // })
      // console.log(6666, fileList)
      // return fileList
    }
  }
}
</script>
