// 添加/修改授权
<template>
  <el-dialog
    :title="isEdit ? '编辑设备权限' : '添加设备权限'"
    width="900px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
    :center="true"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="100px"
    >
      <el-form-item prop="id" style="margin-bottom: 0">&nbsp;</el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备sn" prop="sn" required>
            <el-input
              :disabled="isEdit"
              v-model="form.sn"
              placeholder="请输入设备sn"
              maxlength="100"
              show-word-limit
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="购买方公司" prop="purchaserId" required>
            <el-select
              v-model="form.purchaserId"
              placeholder="请选择购买方公司"
              clearable
              filterable
            >
              <el-option
                v-for="(item, index) in buyList"
                :key="index"
                :label="item.companyName"
                :value="item.purchaserId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="应用类型" prop="applicationTypeId" required>
            <el-select
              v-model="form.applicationTypeId"
              placeholder="请选择应用类型"
              filterable
            >
              <el-option
                v-for="(item, index) in applicationList"
                :key="index"
                :label="item.applicationType"
                :value="item.applicationTypeId"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备型号" prop="deviceModelId" required>
            <el-select
              v-model="form.deviceModelId"
              placeholder="请选择设备型号"
              filterable
            >
              <el-option
                v-for="(item, index) in devicelList"
                :key="index"
                :label="item.deviceModel"
                :value="item.deviceModelId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取消 </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { deviceAdd } from "@/api/tripartiteApi"
import {
  applicationTypeList,
  deviceModelList,
  purchaserList,
} from "@/api/configuration";

export default {
  data() {
    return {
      form: {
        sn: null,
        applicationTypeId: null,
        purchaserId: null,
        deviceModelId: null,
      },
      rules: {
        purchaserId: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
        sn: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
        applicationTypeId: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
        deviceModelId: [
          {
            required: true,
            message: "不能为空!",
            trigger: ["change", "blur"],
          },
        ],
      },
      visible: false,
      submitLoading: false,
      isEdit: false,
      // applicationList: [],
      // devicelList: [],
      // buyList: [],
    };
  },
  props:{
    devicelList: {
      type: Array,
      default: () => [],
    },
    applicationList: {
      type: Array,
      default: () => [],
    },
    buyList: {
      type: Array,
      default: () => [],
    },
  },
  created() {
    // this.applicationTypeList();
    // this.deviceModelList();
    // this.purchaserList();
  },
  methods: {
    open(obj) {
      this.visible = true;
      this.submitLoading = false;
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        if (obj && Object.keys(obj).length > 0) {
          this.form = obj;
          this.isEdit = true;
        } else {
          this.isEdit = false;
        }
      });
    },
    submit(e) {
      e.preventDefault();
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        if (this.submitLoading) {
          return;
        }
        this.submitLoading = true;
        try {
          await deviceAdd(this.form);
          this.visible = false;
          this.$emit("success", this.isEdit);
        } catch (e) {
          console.info("add", e);
        } finally {
          this.submitLoading = false;
        }
      });
    },
    // // 获取应用类型列表
    // applicationTypeList() {
    //   applicationTypeList().then((res) => {
    //     this.applicationList = res.data;
    //   });
    // },
    // // 设备型号列表
    // deviceModelList() {
    //   deviceModelList().then((res) => {
    //     this.devicelList = res.data;
    //   });
    // },
    // // 购买方列表
    // purchaserList() {
    //   purchaserList().then((res) => {
    //     this.buyList = res.data;
    //   });
    // },
  },
};
</script>

<style scoped>
::v-deep .el-select {
  width: 100%;
}
</style>
