<template>
  <a-page-header-wrapper :title="title">
    <a-card :body-style="{ padding: '24px 32px' }" :bordered="false">
      <a-form>
        <a-form-item
          label="设备名"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-input v-model="post.name" placeholder="给设备起个名字" />
        </a-form-item>
        <a-form-item
          label="IMEI"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-input v-model="post.imei" placeholder="请描述设备IMEI" />
        </a-form-item>
        <a-form-item
          label="设备描述"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="false"
        >
          <a-textarea v-model="post.desc" rows="4" placeholder="请输入设备描述信息" />
        </a-form-item>
        <a-form-item
          label="设备类型"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-select
            v-model="post.type"
            mode="default"
            style="width: 100%"
            placeholder="请选择设备类型"
            :allowClear="true"
          >
            <a-select-option v-for="item in categoryList" :key="item.value">
              {{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          style="margin-top: 24px"
          :wrapperCol="{ span: 10, offset: 7 }"
        >
          <a-button type="primary" v-if="title == '添加'" @click="postDevice">提交</a-button>
          <a-button style="margin-left: 8px" v-else @click="updateDevice">保存</a-button>
        </a-form-item>
      </a-form>
    </a-card>
  </a-page-header-wrapper>
</template>

<script>
import {
  Avatar,
  Row,
  Col,
  Card,
  List,
  Button,
  Form,
  Icon,
  Table,
  Divider,
  Dropdown,
  Input,
  Select,
  Radio,
  Upload,
  PageHeader,
  DatePicker,
  InputNumber,
  message,
} from "ant-design-vue";
import PageHeaderWrapper from "@/components/PageHeaderWrapper";
import DescriptionItem from "@/components/DescriptionItem";

import { mapTrim } from "@/utils/index";
import { postDevice, updateDevice } from "@/api/openapi";

export default {
  name: "DeviceForm",
  components: {
    Icon,
    AUpload: Upload,
    APageHeader: PageHeader,
    ATextarea: Input.TextArea,
    ARadio: Radio,
    ARadioGroup: Radio.Group,
    ARadioButton: Radio.Button,
    AInputNumber: InputNumber,
    AAvatar: Avatar,
    ARow: Row,
    ACol: Col,
    ACard: Card,
    ACardGrid: Card.Grid,
    ACardMeta: Card.Meta,
    AList: List,
    AButton: Button,
    AForm: Form,
    AFormItem: Form.Item,
    AIcon: Icon,
    ATable: Table,
    ADivider: Divider,
    ADropdown: Dropdown,
    AInput: Input,
    ASelect: Select,
    AUploadDragger: Upload.Dragger,
    ASelectOption: Select.Option,
    ADescriptionItem: DescriptionItem,
    APageHeaderWrapper: PageHeaderWrapper,
    ARangePicker: DatePicker.RangePicker,
  },
  data() {
    return {
      uuid: null,
      title: "添加",
      categoryList: [
        { label: "手表", value: "watch" }
      ],
      post: {
        name: null,
        imei: null,
        desc: null,
        type: null,
      }
    };
  },
  created() {
    const params = this.$route.params
    console.log(params)
    if (params.title) {
      this.title = params.title
    }
    if(params.record) {
      this.uuid = params.record.uuid
      this.post = params.record
    }
  },
  methods: {
    postDevice() {
      postDevice(this.post).then(res => {
        message.success(res.msg)
      }).catch(err => {
        message.error(err.msg)
      })
    },
    updateDevice() {
      updateDevice(this.uuid, mapTrim(this.post)).then(res => {
        message.success(res.msg)
      }).catch(err => {
        message.error(err.msg)
      })
    }
  }
};
</script>

<style scoped>
</style>