<script>
const { 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: "",
      searchForm: {}
    };
  },
  computed: {},
  mounted() {},
  created() {},

  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() {},
    getDataInfo() {
      this.$emit("search", this.searchForm);
    },
    productionCompleted() {
      this.$confirm("确认完成生产?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {})
        .catch(() => {});
    },
    changeTypesetting() {
      this.dialogVisible = true;
    },
    async submit() {
      if (!this.radio) {
        return this.$message.error("请选择排版宽度");
      }

      if (this.dtfSetting.widthStrategy == 3 && this.radio == 60) {
        this.$confirm(
          "由于排版设置中选择“按更大规格排版”仅适用于40+2，选60时，默认超60的规格不排版",
          "提示",
          {
            confirmButtonText: "确定",
            type: "warning"
          }
        );
        await this.$api.post("/psReComposingDesignImages", {
          batchArrangeNum: this.templateBatchArrangeNum,
          templateWidth: this.radio
        });
        this.$message(
          "排版进行中，请查看处理状态，排版完成的素材至文件夹排版素材查看"
        );
      } else {
        await this.$api.post("/psReComposingDesignImages", {
          batchArrangeNum: this.templateBatchArrangeNum,
          templateWidth: this.radio
        });
        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
            placeholder="请扫描操作单"
            v-model="searchForm.operationNo"
          ></el-input
        ></el-form-item>
        <el-form-item label="操作单状态">
          <el-select v-model="searchForm.status" placeholder="请选择" clearable>
            <el-option
              v-for="(value, label) in statusMap"
              :key="value"
              :label="label"
              :value="value"
            ></el-option> </el-select
        ></el-form-item>
        <el-form-item label="操作单排版状态">
          <el-select v-model="searchForm.layout" placeholder="请选择">
            <el-option label="是" :value="true"></el-option>
            <el-option label="否" :value="false"> </el-option> </el-select
        ></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>
