<template>
  <el-dialog v-model="dialogFormVisible" title="水质检测" width="500px">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="选择站点" prop="deviceCode">
        <el-select v-model="form.deviceCode" 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 :label="$t('apply.jcrq')" prop="date">
        <el-date-picker
          v-model="form.date"
          type="date"
          :placeholder="$t('defalult.xzrq')"
          @change="standardToTime"
        ></el-date-picker>
      </el-form-item>
      <h2>进水</h2>
      <el-form-item label="COD" prop="inCod">
        <el-input v-model="form.inCod"></el-input>
      </el-form-item>
      <el-form-item :label="$t('apply.ad')" prop="inAn">
        <el-input v-model="form.inAn"></el-input>
      </el-form-item>
      <el-form-item :label="$t('apply.zl')" prop="inTp">
        <el-input v-model="form.inTp"></el-input>
      </el-form-item>
      <el-form-item :label="$t('apply.zd')" prop="inTn">
        <el-input v-model="form.inTn"></el-input>
      </el-form-item>
      <el-form-item label="PH" prop="inPh">
        <el-input v-model="form.inPh"></el-input>
      </el-form-item>
      <h2>出水</h2>
      <el-form-item label="COD" prop="outCod">
        <el-input v-model="form.outCod"></el-input>
      </el-form-item>
      <el-form-item :label="$t('apply.ad')" prop="outAn">
        <el-input v-model="form.outAn"></el-input>
      </el-form-item>
      <el-form-item :label="$t('apply.zl')" prop="outTp">
        <el-input v-model="form.outTp"></el-input>
      </el-form-item>
      <el-form-item :label="$t('apply.zd')" prop="outTn">
        <el-input v-model="form.outTn"></el-input>
      </el-form-item>
      <el-form-item label="PH" prop="outPh">
        <el-input v-model="form.outPh"></el-input>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button size="medium" type="primary" @click="save">确 定</el-button>
      <el-button size="medium" @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { doAdd, doEdit } from '@/api/testing';
import { getDevices } from '@/api/inspection';
import { getDate } from '@/common/times';

export default {
  data() {
    return {
      form: {
        id: '',
        deviceCode: '',
        inCod: '',
        inAn: '',
        inTp: '',
        inTn: '',
        inPh: '',
        outCod: '',
        outAn: '',
        outTp: '',
        outTn: '',
        outPh: '',
        date: '',
      },
      rules: {
        deviceCode: [
          { required: true, trigger: 'change', message: '请选择站点' },
        ],
        date: [
          { required: true, trigger: 'change', message: '请选择检测时间' },
        ],
        inCod: [{ required: true, trigger: 'blur', message: '请输入COD' }],
        inAn: [{ required: true, trigger: 'blur', message: '请输入氨氮' }],
        inTp: [{ required: true, trigger: 'blur', message: '请输入总磷' }],
        inTn: [{ required: true, trigger: 'blur', message: '请输入总氮' }],
        inPh: [{ required: true, trigger: 'blur', message: '请输入PH' }],
        outCod: [{ required: true, trigger: 'blur', message: '请输入COD' }],
        outAn: [{ required: true, trigger: 'blur', message: '请输入氨氮' }],
        outTp: [{ required: true, trigger: 'blur', message: '请输入总磷' }],
        outTn: [{ required: true, trigger: 'blur', message: '请输入总氮' }],
        outPh: [{ required: true, trigger: 'blur', message: '请输入PH' }],
      },
      title: '',
      dialogFormVisible: false,
      deviceData: [],
    };
  },
  created() {},
  methods: {
    async getDevicesData() {
      var param = {
        projectId: sessionStorage.getItem('projectId'),
      };
      const { data } = await getDevices(param);
      this.deviceData = data;
    },
    //标准时间转yyyy-mm-dd
    standardToTime() {
      this.form.date = getDate(this.form.date);
    },
    setDefaultTime() {
      this.form.date = getDate();
    },
    showEdit(row) {
      this.getDevicesData();
      if (!row) {
        this.title = '添加';
      } else {
        this.title = '编辑';
        this.menuId = row.id;
        this.ruleForm = Object.assign({}, row);
        this.form = Object.assign({}, row);
      }
      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 {
            const { msg } = await doAdd(this.form);
            this.$notify({
              title: msg,
              type: 'success',
            });
          }
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false;
          this.$parent.fetchData();
        } else {
          return false;
        }
      });
    },
  },
};
</script>
