<script>
import { mapState } from "vuex";
const { getDesktopDevice, getDTFSetting } = require("@/server/utils/store");
const { ipcRenderer } = require("electron");

export default {
  props: {
    user: {
      default: () => ({
        avatar: "",
        factory: {}
      }),
      type: Object
    },
    factoryType: { type: String, default: "CN" }
  },
  data() {
    return {
      dialogVisible: false,
      dtfSetting: getDTFSetting(),
      radio: ""
    };
  },
  computed: {
    ...mapState([
      "defaultProportion",
      "countryList",
      "orderType",
      "grid",
      "imgList"
    ])
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.searchRef?.focus();
      if (this.desktopDevice !== 3) {
        this.selectGridIndex = 0;
        this.setting.gridValue = 0;
      } else {
        this.selectGridIndex = 5;
        this.setting.gridValue = 5;
      }
    });
    this.$store.commit("changeDesktopDevice", getDesktopDevice());
  },
  created() {
    localStorage.setItem("desktoVersion", "print");
    if (this.$dataStore.get("setting")) {
      this.setting = this.$dataStore.get("setting");
    } else {
      this.$dataStore.set("setting", this.setting);
    }
  },

  methods: {
    dropdownCommand(v) {
      switch (v) {
        case "logout":
          this.$confirm("是否退出登录?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.$dataStore.delete("user");
              this.$router.push("/");
            })
            .catch(() => {});
          break;

        case "cache":
          this.checkList = [];
          this.cacheVisible = true;
          break;
      }
    },
    getCountryName(code) {
      if (code) {
        const item = this.countryList?.find(el => el.countryCode == code);
        if (item) {
          return `(${item.nameCn})`;
        }
      }

      return "";
    },
    goBack() {
      this.$router.push({ name: "design-dtf" });
    },
    goSetting() {
      this.$router.push({
        path: "/DTFsetting",
        query: { id: 123 }
      });
    },
    getDataInfo() {},
    productionCompleted() {
      this.$confirm("确认完成生产?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {})
        .catch(() => {});
    },
    changeTypesetting() {
      this.dialogVisible = true;
    },
    submit() {
      console.log(this.radio);
      if (!this.radio) {
        return this.$message.error("请选择排版宽度");
      }

      if (this.dtfSetting.overWidthMode === "biggerSize" && this.radio == 60) {
        this.$confirm(
          "由于排版设置中选择“按更大规格排版”仅适用于40+2，选60时，默认超60的规格不排版",
          "提示",
          {
            confirmButtonText: "确定",
            type: "warning"
          }
        ).then(() => {
          this.dialogVisible = false;
          this.$message(
            "排版进行中，请查看处理状态，排版完成的素材至文件夹排版素材查看"
          );
        });
      } else {
        this.$message(
          "排版进行中，请查看处理状态，排版完成的素材至文件夹排版素材查看"
        );
      }
    },
    async goFliePage(value, type) {
      console.log(value);

      let { data } = await this.$api.post("/createBatchFolder", {
        basePath: this.dtfSetting.downloadDir,
        downloadDir: value.batchNo,
        isArray: false
      });
      console.log("goFliePage", data);
      if (type == "check") {
        ipcRenderer.send("open-folder-only", data);
      }
    }
  }
};
</script>

<template>
  <div>
    <div class="page-header">
      <el-form style="display: flex;flex-wrap: nowrap;" inline>
        <el-form-item>
          <el-button
            icon="el-icon-arrow-left"
            @click="goBack"
            style="margin-right:12px;"
            >返回</el-button
          ></el-form-item
        >
        <el-form-item> <el-input></el-input></el-form-item>
        <el-form-item label="操作单状态"> <el-input></el-input></el-form-item>

        <el-form-item>
          <el-button @click="getDataInfo" type="primary"
            >查询
          </el-button></el-form-item
        >
        <el-form-item>
          <el-button @click="productionCompleted" type="success"
            >生产完成
          </el-button></el-form-item
        >
        <el-form-item>
          <el-button @click="goSetting" type="success"
            >下载素材
          </el-button></el-form-item
        >
        <el-form-item>
          <el-button @click="changeTypesetting" type="success"
            >排版
          </el-button></el-form-item
        >
        <el-form-item>
          <el-button @click="goFliePage" type="success"
            >查看排版文件
          </el-button></el-form-item
        >
      </el-form>

      <div class="right-user">
        <div
          v-if="user && user.factory"
          style="font-weight: 700;margin-left: 8px;"
        >
          {{
            user.factory.countryCode
              ? `${user.factory.countryCode}${getCountryName(
                  user.factory.countryCode
                )}`
              : "CN(中国)"
          }}
        </div>
        <p v-if="user && user.factory">{{ user.factory.title }}</p>
        <el-dropdown @command="dropdownCommand">
          <div style="margin-right: 20px">
            <b style="cursor:pointer;">{{ user.account }}</b>
            <i class="el-icon-arrow-down"></i>
          </div>

          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <el-dialog title="排版宽度" :visible.sync="dialogVisible" width="25%">
      <el-radio-group v-model="radio">
        <el-radio :label="40">40+2cm</el-radio>
        <el-radio :label="60">60cm</el-radio>
      </el-radio-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<style lang="less" scoped>
.right-user {
  display: flex;
  align-items: center;

  b {
    margin: 0 3px;
    display: inline-block;
    color: black;
    font-size: 15px;
    font-weight: bold;
  }

  p {
    margin: 0 10px;
    font-weight: bold;
    display: inline-block;
    font-size: 15px;
    color: #e6a23c;
  }
}

.page-header {
  display: flex;
  justify-content: space-between;
  background-color: #ececec;
  padding: 5px;
  .el-form-item {
    margin-bottom: 0;
  }
}
</style>
