<template>
  <div>
    <Drawer
      :title="formTitle"
      v-model="modalVisible"
      width="720"
      :mask-closable="false"
      :styles="styles"
      @on-close="close"
    >
      <Form
        ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="150"
      >
        <FormItem label="版本号" prop="version">
           <Input
            v-model="formValidate.version"
            placeholder="Enter something"
          ></Input>
        </FormItem>
        <FormItem label="更新说明" prop="summary">
          <Input
          type="textarea"
            v-model="formValidate.summary"
            placeholder="Enter something"
          ></Input>
        </FormItem>
         <FormItem label="强制更新" >
             <RadioGroup v-model="formValidate.is_force">
        <Radio :label="1">
            <span>是</span>
        </Radio>
        <Radio :label="0">
            <span>否</span>
        </Radio>
    </RadioGroup>
        </FormItem>
        <FormItem label="更新设备">
            <Select clearable  v-model="formValidate.platform" style="width:180px">
              <Option
                v-for="item in platform"
                :value="item.name"
                :key="item.id"
                >{{ item.name }}</Option
              >
            </Select>
        </FormItem>
         <FormItem label="安卓App包上传" prop="apk_url">
          <FileUpload ref="apk_url" v-model="formValidate.apk_url" />
        </FormItem>
         <FormItem label="安卓App包下载地址" prop="res_uri">
          <a href="">{{formValidate.apk_url.url}}</a> <a style="margin-left: 10px;" v-clipboard:copy="formValidate.apk_url.url" v-clipboard:success="onCopy">复制</a>
        </FormItem>
      </Form>
      <div class="demo-drawer-footer">
        <Button style="margin-right: 8px" @click="close">取消</Button>
        <Button type="primary" @click="submit">保存</Button>
      </div>
    </Drawer>
  </div>
</template>
<script>
import FileUpload from '@/view/common/FileUpload.vue'
import {
  addVersion,
  editVersion
} from '@/api/version'
const _requireBlur = { required: true, message: ' ', trigger: 'blur' }
const validatorPic = (rule, value, callback) => {
  if (!value) {
    callback(new Error(' '))
  } else {
    callback()
  }
}
const _requirePic = { required: true, message: ' ', trigger: 'blur', validator: validatorPic }
export default {
  props: {
    search: {
      type: Function,
      default: null
    },
    row: {
      type: Object,
      default: () => {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  components: {
    FileUpload
  },
  data () {
    return {
      modalVisible: false,
      editMode: false,
      formTitle: '',
      styles: {
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static'
      },
      platform: [{ id: '1', name: '平台' }, { id: '2', name: 'ios' }, { id: '3', name: 'android' }],
      formValidate: {},
      ruleValidate: {
        'details.name': [_requireBlur],
        'details.summary': [_requireBlur],
        'details.res_home_background_pic': [_requirePic],
        'details.res_discover_list_pic': [_requirePic]
      }
    }
  },
  watch: {
    visible: {
      handler: function (val) {
        this.modalVisible = val
      },
      immediate: true
    },
    row: {
      handler: function (val) {
        console.log(val)
        if (val.id === 'add') {
          this.editMode = false
          this.formTitle = '新增更新'
          this.formValidate = {
            version: '',
            summary: '',
            is_force: 1,
            platform: '',
            apk_url: ''
          }
        } else {
          this.editMode = true
          this.formTitle = '编辑更新'
          this.formValidate = val
          this.formValidate.apk_url = {
            url: val.apk_url,
            name: val.apk_url
          }
        }
      },
      immediate: true
    }
  },
  methods: {
    submit () {
      let apk_url = this.$refs['apk_url'].getData()
      if (apk_url.url) {
        this.formValidate.apk_url = apk_url.url
      }
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          delete this.formValidate.created_at
          delete this.formValidate.deleted_at
          delete this.formValidate.updated_at
          let params = [this.formValidate]
          if (this.editMode) {
            editVersion(params).then((res) => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('修改成功')
                this.close()
                this.search()
              }
            })
          } else {
            addVersion(params).then((res) => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('新增成功')
                this.close()
                this.search()
              }
            })
          }
        } else {
          this.$Message.error('您还有必填项未填写')
        }
      })
    },
    change (val, type) {
      if (type === 1) {
        this.formValidate.is_in_home_lis = val ? 1 : 0
      }
      if (type === 2) {
        this.formValidate.is_in_discover_list = val ? 1 : 0
      }
    },
    // 复制成功时的回调函数
    onCopy (e) {
      this.$Message.success('复制成功')
    },
    close () {
      this.$emit('update:visible', false)
    }
  }
}
</script>
<style>
.demo-drawer-footer {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  background: #fff;
}
</style>
