<template>
  <div class="app-container">
    <div v-show="viewState == 1">
      <div class="tab-header webtop">
        <div>{{ viewTitle }}</div>
        <div>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveOrUpdate()">保存</el-button>
          <el-button type="info" size="small" @click="handleReturn()">关闭</el-button>
        </div>
      </div>
      <div class="listconadd">
        <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
          <el-row style="border-top: 1px solid #e0e3eb">
            <el-col :span="8">
              <div class="span-sty"><span class="icon">*</span>项目名称</div>
              <el-form-item prop="sitemName"><el-input class="addinputInfo addinputw" v-model="formobj.sitemName" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">拼音缩写</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.sitemCode" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty"><span class="icon">*</span>工种</div>
              <el-form-item prop="serviceType">
                <el-select class="addinputInfo" v-model="formobj.serviceType" placeholder="请选择" @change="serviceTypeChange" clearable filterable>
                  <el-option v-for="item in serviceType_list" :key="item.serviceTypeSid" :label="item.serviceType" :value="item.serviceType"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">车型</div>
              <el-form-item>
                <el-select class="addinputInfo" v-model="formobj.goodsModel" placeholder="请选择" @change="goodsModelChange" clearable filterable>
                  <el-option v-for="item in goodsModel_list" :key="item.goodsModelSid" :label="item.goodsModel" :value="item.goodsModel"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">厂家</div>
              <el-form-item>
                <el-select class="addinputInfo" v-model="formobj.manufacturer" placeholder="请选择" @change="manufacturerChange" clearable filterable>
                  <el-option v-for="item in manufacturer_list" :key="item.manufacturerSid" :label="item.manufacturer" :value="item.manufacturer"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">备注</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.remarks" clearable placeholder="" /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">排序</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.sortNo" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">工时单价</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="computeXSJ" v-model="formobj.hourPrice" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">工时数</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="computeXSJ" v-model="formobj.hours" clearable placeholder="" /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">销售价</div>
              <el-form-item><el-input class="addinputInfo addinputw" @input="computeGSS" v-model="formobj.price" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">考核工时</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.examineHourPrice" clearable placeholder="" /></el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">成本</div>
              <el-form-item><el-input class="addinputInfo addinputw" v-model="formobj.cost" clearable placeholder="" /></el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="span-sty">预约项目</div>
              <el-form-item>
                <el-radio-group class="addinputInfo" style="font-size: 1px" v-model="formobj.isReserve">
                  <el-radio label="1">是</el-radio>
                  <el-radio label="0">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <div class="span-sty">洗车项目</div>
              <el-form-item>
                <el-radio-group class="addinputInfo" style="font-size: 1px" v-model="formobj.isClean">
                  <el-radio label="1">是</el-radio>
                  <el-radio label="0">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="8">
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import req from '@/api/basicinformation/maintenanceitem'
import { getServiceType } from '@/api/Common/dictcommons'

export default {
  name: 'MaintenanceItemAdd',
  data() {
    return {
      viewTitle: '',
      viewState: 1,
      submitdisabled: false,
      tableKey: 0,
      index: 0,
      serviceType_list: [],
      goodsModel_list: [],
      manufacturer_list: [],
      formobj: {
        sid: '',
        createBySid: '',
        sitemName: '',
        sitemCode: '',
        serviceTypeSid: '',
        serviceType: '',
        goodsModelSid: '',
        goodsModel: '',
        manufacturerSid: '',
        manufacturer: '',
        hourPrice: '',
        hours: '',
        price: '',
        examineHourPrice: '',
        cost: '',
        isReserve: '',
        isClean: '',
        sortNo: '',
        remarks: '',
        useOrgSid: '',
        createOrgSid: ''
      },
      rules: {
        sitemName: [{ required: true, message: '项目名称不能为空', trigger: 'blur' }],
        serviceType: [{ required: true, message: '工种不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    init() {
      getServiceType({ orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((res) => {
        if (res.success) {
          this.serviceType_list = res.data
        }
      })
      req.getVehModel({ orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((res) => {
        if (res.success) {
          this.goodsModel_list = res.data
        }
      })
      req.getManufacturers({ orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((res) => {
        if (res.success) {
          this.manufacturer_list = res.data
        }
      })
    },
    showAdd() {
      this.viewTitle = '【新增】维修项目'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      this.init()
    },
    showEdit(row) {
      this.viewTitle = '【新增】维修项目'
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      this.init()
      req.fetchBySid(row.sid).then((res) => {
        if (res.success) {
          this.formobj = res.data
        }
      })
    },
    serviceTypeChange(value) {
      const choose = this.serviceType_list.filter((item) => item.serviceType === value)
      if (choose !== null && choose.length > 0) {
        this.formobj.serviceTypeSid = choose[0].serviceTypeSid
      } else {
        this.formobj.serviceTypeSid = ''
      }
    },
    goodsModelChange(value) {
      const choose = this.goodsModel_list.filter((item) => item.goodsModel === value)
      if (choose !== null && choose.length > 0) {
        this.formobj.goodsModelSid = choose[0].goodsModelSid
      } else {
        this.formobj.goodsModelSid = ''
      }
    },
    manufacturerChange(value) {
      const choose = this.manufacturer_list.filter((item) => item.manufacturer === value)
      if (choose !== null && choose.length > 0) {
        this.formobj.manufacturerSid = choose[0].manufacturerSid
      } else {
        this.formobj.manufacturerSid = ''
      }
    },
    // 计算销售价=工时数*工时单价
    computeXSJ() {
      this.formobj.price = Math.round((parseFloat(this.formobj.hourPrice === '' ? '0' : this.formobj.hourPrice) * parseFloat(this.formobj.hours === '' ? '0' : this.formobj.hours)) * 100) / 100
    },
    // 计算工时数=销售价除以工时单价
    computeGSS() {
      this.formobj.hours = Math.round((parseFloat(this.formobj.price === '' ? '0' : this.formobj.price) / parseFloat(this.formobj.hourPrice === '' ? '0' : this.formobj.hourPrice)) * 100) / 100
    },
    saveOrUpdate() {
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          this.formobj.createBySid = window.sessionStorage.getItem('userSid')
          this.formobj.orgPath = window.sessionStorage.getItem('defaultOrgPath')
          req.saveOrUpdate(this.formobj).then((res) => {
            if (res.success) {
              this.$message({ showClose: true, type: 'success', message: '保存成功' })
              this.handleReturn('true')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    },
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      this.formobj = {
        sid: '',
        createBySid: '',
        sitemName: '',
        sitemCode: '',
        serviceTypeSid: '',
        serviceType: '',
        goodsModelSid: '',
        goodsModel: '',
        manufacturerSid: '',
        manufacturer: '',
        hourPrice: '',
        hours: '',
        price: '',
        examineHourPrice: '',
        cost: '',
        isReserve: '',
        isClean: '',
        sortNo: '',
        remarks: '',
        useOrgSid: '',
        createOrgSid: ''
      }
      this.submitdisabled = false
      this.$emit('doback')
    }
  }
}
</script>

<style scoped>
.span-sty {
  width: 130px !important;
}
.addinputInfo {
  margin-left: 120px !important;
}
/deep/ .el-form-item__error {
  margin-left: 120px !important;
}
.formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
  margin-left: 120px !important;
  width: calc(100% - 115px);
}
</style>
