<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 placeholder="给应用起个名字" />
        </a-form-item>
        <a-form-item
          label="打包算法"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
        >
          <a-radio-group v-model="value" @change="onChange">
            <a-radio-button value="zlib"> zlib </a-radio-button>
            <a-radio-button value="eheatshrink"> eheatshrink </a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label="应用版本号"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="false"
        >
          <a-input v-model="post.app_version" placeholder="请输入版本号" />
        </a-form-item>
        <a-form-item
          label="应用类别"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-select
            mode="tags"
            style="width: 100%"
            placeholder="应用所属类别"
            @change="handleChange"
          >
            <a-select-option v-for="i in 25" :key="(i + 9).toString(36) + i">
              {{ (i + 9).toString(36) + i }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="应用ICON"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-upload
            name="avatar"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
            :before-upload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="imageUrl" :src="imageUrl" alt="avatar" />
            <div v-else>
              <a-icon :type="loading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
        </a-form-item>
        <a-form-item
          label="适配尺寸"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-select :default-value="sizeList[0].label" style="width: 120px">
            <a-select-option v-for="item in sizeList" :key="(item.value + 9).toString(36) + item.value" :value="item.label">
              {{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="适配平台"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-select :default-value="portList[0].label" style="width: 120px">
            <a-select-option v-for="item in portList" :key="(item.value + 9).toString(36) + item.value" :value="item.label">
              {{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="应用介绍"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
        >
          <a-textarea rows="4" placeholder="请输入应用介绍" />
        </a-form-item>
        <a-form-item
          label="应用源文件"
          :labelCol="{ span: 7 }"
          :wrapperCol="{ span: 10 }"
          :required="true"
        >
          <a-upload-dragger
            name="file"
            :multiple="true"
            action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
            @change="handleSourceFileChange"
          >
            <p class="ant-upload-drag-icon">
              <a-icon type="inbox" />
            </p>
            <p class="ant-upload-text">
              点击或拖拽文件到此
            </p>
            <p class="ant-upload-hint">
              支持单次或批量上传
            </p>
          </a-upload-dragger>
        </a-form-item>
        <a-form-item
          style="margin-top: 24px"
          :wrapperCol="{ span: 10, offset: 7 }"
        >
          <a-button type="primary">提交</a-button>
          <a-button style="margin-left: 8px">保存</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,
} from "ant-design-vue";
import PageHeaderWrapper from "@/components/PageHeaderWrapper";
import DescriptionItem from "@/components/DescriptionItem";
export default {
  name: "BasicForm",
  data() {
    return {
      loading: false,
      imageUrl: "",
      value: 1,
      title: "编辑",
      post: {
        app_version: '',
      },
      sizeList: [
        { label: '240 * 240', value: 1 },
        { label: '390 * 390', value: 2 },
        { label: '450 * 450', value: 3 }
      ],
      portList: [
        { label: 'ASR3601', value: 1 },
        { label: 'ASR3603', value: 2 },
      ],
    };
  },
  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,
  },
  methods: {
    handleUploadChange(info) {
      if (info.file.status === "uploading") {
        this.loading = true;
        return;
      }
      if (info.file.status === "done") {
        // Get this url from response in real world.
        getBase64(info.file.originFileObj, (imageUrl) => {
          this.imageUrl = imageUrl;
          this.loading = false;
        });
      }
    },
    handleSourceFileChange(info) {
      const status = info.file.status;
      if (status !== 'uploading') {
        console.log(info.file, info.fileList);
      }
      if (status === 'done') {
        this.$message.success(`${info.file.name} file uploaded successfully.`);
      } else if (status === 'error') {
        this.$message.error(`${info.file.name} file upload failed.`);
      }
    },
    beforeUpload(file) {
      const isJpgOrPng =
        file.type === "image/jpeg" || file.type === "image/png";
      if (!isJpgOrPng) {
        this.$message.error("You can only upload JPG file!");
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error("Image must smaller than 2MB!");
      }
      return isJpgOrPng && isLt2M;
    },
    onChange(e) {
      console.log(`checked = ${e.target.value}`);
    },
    handleChange(value) {
      console.log(`selected ${value}`);
    },
  },
  computed: {
    desc() {
      return this.$t("pageDesc");
    },
  },
  created() {
    console.log(this.$route.query);
  },
};
</script>

<style scoped>
.avatar-uploader > .ant-upload {
  width: 128px;
  height: 128px;
}
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>