<template>
  <div class="app-container">
    <el-form :inline="true" ref="form" :model="form" size="mini">
      <el-form-item label="应用名称" prop="uuid">
        <el-select v-model="form.uuid" filterable placeholder="请输入标题">
          <el-option
            v-for="(item, index) in roles"
            :key="index"
            :label="item.name"
            :value="item.uuid"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        ><el-button type="primary" @click="onSubmit"
          >查询</el-button
        ></el-form-item
      >
      <el-form-item><el-button @click="onReset">重置</el-button></el-form-item>
      <el-form-item
        ><el-button type="warning" @click="onAdd">添加</el-button></el-form-item
      >
    </el-form>
    <el-table
      v-loading="isLoading"
      element-loading-text="Loading"
      :data="list"
      size="mini"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column
        prop="app_id.app_name"
        label="应用名称"
        width="180"
      ></el-table-column>
      <el-table-column
        prop="build_text"
        label="打包信息"
        min-width="250"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column
        prop="create_at"
        label="创建时间"
        width="150"
      ></el-table-column>
      <el-table-column
        prop="create_by.username"
        label="创建者"
        width="150"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="240" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="handleDownload(scope.$index, scope.row)"
            >下载</el-button
          >
          <el-button
            size="mini"
            type="success"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="page-wrapper">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="form.pagenum"
        background
        small
        :page-size="form.pagesize"
        :pager-count="5"
        layout="pager, prev, next, total"
        :total="total"
      ></el-pagination>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="50%">
      <el-form
        :model="post"
        status-icon
        :rules="rules"
        :inline="true"
        ref="post"
        size="mini"
        label-width="200px"
      >
        <el-form-item label="标题" prop="name">
          <el-input
            type="text"
            v-model="post.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="年度调派现场总天数" prop="year_dispatch_site_days">
          <el-input
            type="number"
            v-model.number="post.year_dispatch_site_days"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="应用文件" prop="app_files">
          <el-upload
            drag
            action="https://jsonplaceholder.typicode.com/posts/"
            multiple
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" plain @click="submitForm('post')"
          >提交</el-button
        >
        <el-button type="success" size="mini" plain @click="onReset('form')"
          >重置</el-button
        >
        <el-button size="mini" @click="dialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getBuildLogsList } from "@/api/app-store";
import { mapTrim } from "@/utils/index";

export default {
  name: "AppBuild",
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      total: 0,
      list: [],
      isLoading: false,
      roles: [],
      form: {
        uuid: null,
        name: null,
        pagesize: 15,
        pagenum: 1,
      },
      currentIndex: 0,
      currentValue: null,
      dialogTitle: "",
      post: {
        name: null,
        year_dispatch_site_days: null,
      },
      rules: {
        year_dispatch_site_days: [
          {
            type: "number",
            required: true,
            message: "字段不能为空",
            trigger: "blur",
          },
        ],
        name: [
          {
            type: "string",
            required: true,
            message: "用户名不能为空",
            trigger: "blur",
          },
          {
            min: 1,
            max: 20,
            message: "字符串长度在 1 到 20 之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    fetchData(params) {
      this.isLoading = true;
      getBuildLogsList(params)
        .then((res) => {
          this.total = res.count;
          this.list = res.data.map(item => {
              item.build_text = JSON.stringify(item.app_info)
              return item
          });
        })
        .catch((err) => {
          // this.$message.error(err.message)
          console.log(err.message);
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    fetchSelectData() {
      getBuildLogsList({ scope_type: "list" })
        .then((res) => {
          if (res.code == 200) this.roles = res.data;
        })
        .catch((err) => {
          // this.$message.error(err.message)
          console.log(err.message);
        });
    },
    handleRemove(file) {
      console.log(file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleDownload(index, row) {
      console.log(index, row);
    },
    handleSizeChange(e) {
      this.form.pagesize = e;
      this.fetchData(mapTrim(this.form));
    },
    handleCurrentChange(e) {
      this.form.pagenum = e;
      this.fetchData(mapTrim(this.form));
    },
    handleEdit(index, row) {
      this.post = Object.assign(row);
      this.currentIndex = index;
      this.currentValue = row;
      this.dialogTitle = "编辑";
      this.dialogVisible = true;
    },
    handleDelete(index, row) {
      this.$alert(
        "您确定要删除么？删除操作将不可恢复。如需取消操作，请点击右上角关闭按钮。",
        "删除提醒",
        {
          confirmButtonText: "确定",
          callback: (action) => {
            if (action == "confirm") console.log(action, index, row);
          },
        }
      );
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        let result = true;
        if (valid) {
          console.log(valid);
        } else {
          result = false;
        }
        this.dialogVisible = false;
        return result;
      });
    },
    onAdd() {
      this.dialogTitle = "添加";
      this.dialogVisible = true;
    },
    onSubmit() {
      this.form.pagenum = 1;
      this.form.pagesize = 15;
      this.fetchData(mapTrim(this.form));
    },
    onReset(formName) {
      this.$refs[formName].resetFields();
      this.form.pagesize = 15;
      this.form.pagenum = 1;
      this.fetchData(mapTrim(this.form));
    },
  },
  mounted() {},
  created() {
    this.fetchData(mapTrim(this.form));
    this.fetchSelectData();
  },
};
</script>
<style lang="less" scoped>
.app-container {
  & > div.page-wrapper {
    margin: 10px 0px;
  }
}
</style>
