<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item width="100" prop="shelfLife" :label="$t('maintenance.bzq')">
        <el-date-picker
          v-model="form.shelfLife"
          size="small"
          type="date"
          placeholder="选择质保期"
          @change="standardToTime"
        ></el-date-picker>
      </el-form-item>
      <el-form-item prop="deviceCode" label="站点">
        <el-select
          v-model="form.deviceCode"
          size="small"
          filterable
          placeholder="站点"
        >
          <el-option
            v-for="item in deviceData"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="name" :label="$t('maintenance.sbmc')">
        <el-select
          v-model="form.name"
          size="small"
          filterable
          placeholder="设备名称"
        >
          <el-option
            v-for="item in nameData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="brand" :label="$t('maintenance.pp')">
        <el-select
          v-model="form.brand"
          size="small"
          filterable
          placeholder="品牌"
        >
          <el-option
            v-for="item in brandData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="supplier" :label="$t('maintenance.gys')">
        <el-select
          v-model="form.supplier"
          size="small"
          filterable
          placeholder="供应商"
        >
          <el-option
            v-for="item in supplierData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="status" :label="$t('dataEnquiry.zt')">
        <el-select
          v-model="form.status"
          size="small"
          filterable
          placeholder="状态"
        >
          <el-option
            v-for="item in statusData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="unit" :label="$t('sensor.dw')">
        <el-select
          v-model="form.unit"
          size="small"
          filterable
          placeholder="单位"
        >
          <el-option
            v-for="item in unitData"
            :key="item.dataCode"
            :label="item.dataValue"
            :value="item.dataCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('maintenance.xh')" prop="model">
        <el-input
          v-model.trim="form.model"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('maintenance.bh')" prop="code">
        <el-input
          v-model.trim="form.code"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('maintenance.gl')" prop="power">
        <el-input
          v-model.trim="form.power"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button size="small" @click="close">取 消</el-button>
      <el-button type="primary" size="small" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  doAdd,
  doEdit,
  getDevices,
  alarmCategoryDict,
} from '@/api/maintenance';
import { getDate } from '@/common/times';
export default {
  data() {
    return {
      form: {
        deviceCode: '',
        name: '',
        brand: '',
        supplier: '',
        status: '',
        unit: '',
        model: '',
        code: '',
        power: '',
        shelfLife: '',
      },
      rules: {
        shelfLife: [
          { required: true, trigger: 'change', message: '请选择质保期' },
        ],
        deviceCode: [
          { required: true, trigger: 'change', message: '请选择站点' },
        ],
        name: [{ required: true, trigger: 'change', message: '请选择设备' }],
        brand: [{ required: true, trigger: 'change', message: '请选择品牌' }],
        supplier: [
          { required: true, trigger: 'change', message: '请选择供应商' },
        ],
        status: [{ required: true, trigger: 'change', message: '请选择状态' }],
        unit: [{ required: true, trigger: 'change', message: '请选择单位' }],
        model: [{ required: true, trigger: 'blur', message: '请输入型号' }],
        code: [{ required: true, trigger: 'blur', message: '请输入状态' }],
        power: [{ required: true, trigger: 'blur', message: '请输入功率' }],
      },
      projectId: '',
      title: '',
      deviceData: [],
      nameData: [],
      brandData: [],
      supplierData: [],
      statusData: [],
      unitData: [],
      dialogFormVisible: false,
    };
  },
  created() {},
  methods: {
    //标准时间转yyyy-mm-dd
    standardToTime() {
      this.form.shelfLife = getDate(this.form.shelfLife);
    },
    //站点
    async getDevicedData() {
      const { data } = await getDevices({ projectId: this.projectId });
      this.deviceData = data;
    },
    async getAlarmCategoryDict() {
      const { data } = await alarmCategoryDict();
      this.nameData = data.equipmentName;
      this.brandData = data.equipmentBrand;
      this.supplierData = data.equipmentSupplier;
      this.statusData = data.equipmentStatus;
      this.unitData = data.equipmentUnit;
    },
    showEdit(row) {
      if (!row) {
        this.title = '添加设备';
      } else {
        this.title = '编辑编辑';
        const data = Object.assign({}, row);
        console.log(123, data);
        this.form.id = data.id;
        if (data.device !== null) {
          this.form.deviceCode = data.device.code;
        }
        if (data.name !== null) {
          this.form.name = data.name.dataCode;
        }
        if (data.brand !== null) {
          this.form.brand = data.brand.dataCode;
        }
        if (data.supplier !== null) {
          this.form.supplier = data.supplier.dataCode;
        }
        if (data.status !== null) {
          this.form.status = data.status.dataCode;
        }
        if (data.unit !== null) {
          this.form.unit = data.unit.dataCode;
        }
        this.form.model = data.model;
        this.form.code = data.code;
        this.form.power = data.power;
        this.form.shelfLife = data.shelfLife;
      }
      this.projectId = sessionStorage.getItem('projectId');
      this.getDevicedData();
      this.getAlarmCategoryDict();
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          if (this.form.id) {
            const { msg } = await doEdit(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          } else {
            delete this.form.id;
            const { msg } = await doAdd(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.$parent.fetchData();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
  },
};
</script>
<style>
.select {
  width: 100%;
}
</style>
