<template>
  <div>
    <div style="font-size:18px;margin-bottom: 15px;">
      <a href="#" @click="prev" style="text-decoration:none;font-size: 14px;">
        <icon-svg
          name="jiantou"
          style="width: 1.2em;height: 1.2em;position: relative;top: 0.3em;"
        ></icon-svg>
        返回
      </a>
      <span style="display: inline-block;margin: 0 15px;color: #D9D9D9;"
        >|</span
      >
      <span>打印配置</span>
    </div>

    <div class="body">
      <!-- 左边预览效果 -->
      <div class="body_left">
        <div class="left_title">预览效果</div>

        <el-card class="left_card" v-if="list1">
          <!-- 基本信息区域 -->
          <div
            v-for="(item, index) in list1.details"
            :key="item.id + 'list1'"
            class="card_box"
            :style="
              `fontSize:${
                list1.types[index].wordSize == 2 ? 20 : 16
              }px;fontWeight:${list1.types[index].isBlod == 1 ? 'bold' : '400'}`
            "
          >
            <div v-if="item.isCheck == 1">
              {{ item.printName }}
              <span v-if="item.printName == '订单号'">:xxxxxxxxx</span>
              <span v-if="item.printName == '订单类型'">:xxxxxxxxx</span>
              <span v-if="item.printName == '送达时间'">:xxxxxxxxx</span>
            </div>
          </div>

          <hr />

          <!-- 商品信息区域 -->
          <div class="card_table">
            <div
              class="table_box"
              v-for="item in list2.details.filter(
                ele => ele.printName != '备注'
              )"
              :key="item.id"
              :style="
                `fontSize:${
                  list2.types.find(ele => ele.printName == item.printName)
                    .wordSize == 2
                    ? 20
                    : 16
                }px;fontWeight:${
                  list2.types.find(ele => ele.printName == item.printName)
                    .isBlod == 1
                    ? 'bold'
                    : '400'
                }`
              "
            >
              <div v-if="item.printName != '备注' && item.isCheck == 1">
                {{ item.printName }}
              </div>
            </div>
          </div>
          <hr />
          <div class="card_table">
            <div
              class="table_box"
              v-for="item in list2.details.filter(
                ele => ele.printName != '备注'
              )"
              :key="item.id"
              :style="
                `fontSize:${
                  list2.types.find(ele => ele.printName == item.printName)
                    .wordSize == 2
                    ? 20
                    : 16
                }px;fontWeight:${
                  list2.types.find(ele => ele.printName == item.printName)
                    .isBlod == 1
                    ? 'bold'
                    : '400'
                }`
              "
            >
              <div v-if="item.printName != '备注' && item.isCheck == 1">
                xx
              </div>
            </div>
          </div>
          <hr />
          <div
            class="card_box2"
            :style="
              `fontSize:${
                list2.types.find(ele => ele.printName == '备注').wordSize == 2
                  ? 20
                  : 16
              }px;fontWeight:${
                list2.types.find(ele => ele.printName == '备注').isBlod == 1
                  ? 'bold'
                  : '400'
              }`
            "
          >
            备注
          </div>

          <hr />

          <!-- 支付信息区域 -->
          <div
            v-for="(item, index) in list3.details"
            :key="item.id"
            class="card_box2"
            :style="
              `fontSize:${
                list3.types[index].wordSize == 2 ? 20 : 16
              }px;fontWeight:${list3.types[index].isBlod == 1 ? 'bold' : '400'}`
            "
          >
            <div v-if="item.isCheck == 1">{{ item.printName }}：xx</div>
          </div>

          <hr />

          <!-- 用户信息区域 -->
          <div
            v-for="(item, index) in list4.details"
            :key="item.id"
            class="card_box2"
            :style="
              `fontSize:${
                list4.types[index].wordSize == 2 ? 20 : 16
              }px;fontWeight:${list4.types[index].isBlod == 1 ? 'bold' : '400'}`
            "
          >
            <div v-if="item.isCheck == 1">{{ item.printName }}：xxx</div>
          </div>
        </el-card>
      </div>

      <!-- 编辑区域 -->
      <div class="body_right">
        <div class="right_title">
          小票编辑
          <el-button size="mini" type="primary" @click="handleAdd"
            >添加打印信息</el-button
          >
        </div>

        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane
            v-for="item in modelDatas"
            :key="item.id"
            :label="item.name"
            :name="item.name"
          >
            <div class="title">
              <div class="dot"></div>
              <div class="title_text">基本信息</div>
              <div class="title_desc">
                （{{ defaultMsg }}为小票必须展示的信息）
              </div>
            </div>

            <div class="check_box">
              <el-checkbox
                v-for="ele in item.details"
                :key="ele.id"
                :value="ele.isCheck == 1"
                :disabled="
                  ele.printName == '平台名称' ||
                    ele.printName == '店铺名' ||
                    ele.printName == '订单号' ||
                    ele.printName == '送达时间' ||
                    ele.printName == '订单类型' ||
                    ele.printName == '商品名称' ||
                    ele.printName == '单价' ||
                    ele.printName == '数量' ||
                    ele.printName == '备注' ||
                    ele.printName == '打包费' ||
                    ele.printName == '商品费' ||
                    ele.printName == '优惠券' ||
                    ele.printName == '跑腿费' ||
                    ele.printName == '优惠活动' ||
                    ele.printName == '合计' ||
                    ele.printName == '姓名' ||
                    ele.printName == '电话号' ||
                    ele.printName == '地址' ||
                    ele.printName == '订餐时间'
                "
                @change="changeType($event, ele)"
              >
                {{ ele.printName }}
              </el-checkbox>
            </div>

            <div class="title">
              <div class="dot"></div>
              <div class="title_text">自定义样式</div>
            </div>

            <div v-for="element in item.types" :key="element.id">
              <div class="radio_box">{{ element.printName }}</div>
              <div class="radio_list">
                打印字号：
                <el-radio-group v-model="element.wordSize">
                  <el-radio :label="1">正常</el-radio>
                  <el-radio :label="2">放大</el-radio>
                </el-radio-group>
              </div>
              <div class="radio_list">
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;字体：
                <el-radio-group v-model="element.isBlod">
                  <el-radio :label="0">正常</el-radio>
                  <el-radio :label="1">加粗</el-radio>
                </el-radio-group>
              </div>
              <hr />
            </div>

            <div class="btn">
              <el-button size="mini" type="primary" @click="handleSave"
                >保存</el-button
              >
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>

    <!-- 添加打印信息弹窗区域 -->
    <el-dialog
      title="添加打印信息"
      :close-on-click-modal="false"
      :visible.sync="centerDialogVisible"
      width="36%"
      center
    >
      <div>
        <el-form :model="form" :rules="rules" ref="ruleForm">
          <el-form-item
            prop="typeId"
            label="打印信息类型："
            :label-width="formLabelWidth"
          >
            <el-select
              v-model="form.typeId"
              placeholder="请选择打印信息类型"
              style="width: 270px;"
            >
              <el-option
                v-for="item in form.typesList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            prop="printName"
            label="打印信息名称："
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.printName"
              autocomplete="off"
              placeholder="请输入打印信息名称"
              style="width: 270px;"
            ></el-input>
          </el-form-item>
          <el-form-item
            prop="printField"
            label="打印信息字段名："
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="form.printField"
              autocomplete="off"
              placeholder="请输入打印信息字段名"
              style="width: 270px;"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    let checkPrintField = (rule, value, callback) => {
      let reg = /^[a-zA-Z_]+$/;
      let flag = reg.test(value);
      if (flag) {
        callback();
      } else {
        callback(new Error("只能输入英文字母和下划线"));
      }
    };
    return {
      // 店铺id
      shopId: "",
      // 打印数据id
      printId: "",
      // 当前激活选项卡
      activeName: "",
      // 小票模板数据
      modelDatas: [],
      defaultMsg: "平台名称、店铺名、订单号",
      // 弹窗显示隐藏控制
      centerDialogVisible: false,
      // 弹窗数据
      form: {
        typeId: "",
        typesList: [],
        printField: "",
        printName: ""
      },
      formLabelWidth: "150px",
      rules: {
        typeId: [
          { required: true, message: "请选择打印信息类型", trigger: "change" }
        ],
        printName: [
          { required: true, message: "请输入打印信息名称", trigger: "change" }
        ],
        printField: [
          {
            required: true,
            message: "请输入打印信息字段名",
            trigger: "change"
          },
          { validator: checkPrintField, trigger: "change" }
        ]
      }
    };
  },
  mounted() {
    this.shopId = this.$route.query.shopId;

    // 根据店铺ID获取小票模板数据
    this.getModelDatas();
  },
  computed: {
    // 基本信息
    list1() {
      return this.modelDatas.find(ele => ele.name == "基本信息");
    },
    // 商品信息
    list2() {
      return this.modelDatas.find(ele => ele.name == "商品信息");
    },
    // 支付信息
    list3() {
      return this.modelDatas.find(ele => ele.name == "支付信息");
    },
    // 用户信息
    list4() {
      return this.modelDatas.find(ele => ele.name == "用户信息");
    }
  },
  methods: {
    // 根据店铺ID获取小票模板数据
    async getModelDatas() {
      const res = await this.$http({
        url: this.$http.adornUrl("/admin/printInfo/getPrintModelData"),
        params: this.$http.adornParams({
          shopId: this.shopId
        })
      });
      console.log(res,'打印')
      if (res.data.code == 0) {
        console.log(res,'打印')
        this.printId = res.data.data.id;
        this.modelDatas = res.data.data.modelDatas;
        this.activeName = this.modelDatas[0].name;
        console.log(this.modelDatas);
      }
    },
    // 添加打印信息按钮回调
    handleAdd() {
      this.centerDialogVisible = true;
      this.$nextTick(() => {
        this.$refs.ruleForm.resetFields();
      });
      // 获取打印信息类型下拉列表
      this.getTypesList();
    },
    // 获取打印信息类型下拉列表
    async getTypesList() {
      const res = await this.$http({
        url: this.$http.adornUrl("/admin/printInfo/getPrintDropList")
      });
      // console.log(res);
      if (res.data.code == 0) {
        this.form.typesList = res.data.data;
      }
    },
    // 弹窗确定按钮回调
    handleConfirm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.handleAddReq();
        }
      });
    },
    // 添加打印信息请求
    async handleAddReq() {
      // console.log(this.form);
      const res = await this.$http({
        url: this.$http.adornUrl("/admin/printInfo/insertPrintInfo"),
        method: "post",
        data: this.$http.adornData({
          printField: this.form.printField,
          printName: this.form.printName,
          typeId: this.form.typeId,
          typeName: this.form.typesList[this.form.typeId-1].name
        })
      });
      console.log(res);
      if (res.data.code == 0) {
        this.centerDialogVisible = false;
        this.$message.success("添加成功");
        this.getModelDatas();
      }else{
        this.$message.success(res.data.msg);
      }
    },
    // 保存按钮回调
    async handleSave() {
      // console.log('解密:',decrypt(urlSafeBase64Decode(" ")))
      let data={
        id: this.printId,
          modelDatas: this.modelDatas,
          shopId: this.shopId
      }
    console.log('保存',data)
      const res = await this.$http({
        url: this.$http.adornUrl(
          this.printId
            ? "/admin/printInfo/updatePrintModelData"
            : "/admin/printInfo/insertPrintModelData"
        ),
        method: "post",
        data: this.$http.adornData({
          id: this.printId,
          modelDatas: this.modelDatas,
          shopId: this.shopId
        })
      });
      console.log(res,'结果');
      if (res.data.code == 0) {
        this.$message.success("保存成功");
      }
    },
    // 选项卡切换回调
    handleClick(tab) {
      // console.log(tab);
      if (tab._props.label == "基本信息") {
        this.defaultMsg = "平台名称、店铺名、订单号";
      } else if (tab._props.label == "商品信息") {
        this.defaultMsg = "商品名称、单价、数量、备注";
      } else if (tab._props.label == "支付信息") {
        this.defaultMsg = "打包费、商品费、优惠券、跑腿费、优惠活动和合计";
      } else if (tab._props.label == "用户信息") {
        this.defaultMsg = "姓名、电话号、地址 、订餐时间";
      }
    },
    // 基本信息切换回调
    changeType(e, ele) {
      if (e) {
        ele.isCheck = 1;
      } else {
        ele.isCheck = 0;
      }
    },
    // 返回上一级
    prev() {
      this.$router.back();
    }
  }
};
</script>

<style lang="scss" scoped>
.body {
  display: flex;
  // justify-content: space-around;

  .body_left {
    margin: 0 5%;
    width: 380px;

    .left_title {
      margin-bottom: 16px;
      font-size: 16px;
      font-weight: bold;
      color: #000;
    }

    .left_card {
      display: flex;
      justify-content: center;
      font-size: 16px;

      .card_box {
        margin-bottom: 10px;
        width: 350px;
      }

      .card_box2 {
        margin-bottom: 10px;
      }

      .card_table {
        display: flex;
        justify-content: space-between;
        margin-bottom: 10px;

        .table_box {
          flex: 1;
          text-align: center;
        }
      }
    }
  }

  .body_right {
    width: 450px;

    .right_title {
      display: flex;
      justify-content: space-between;
      margin-bottom: 16px;
      font-size: 16px;
      font-weight: bold;
      color: #000;
    }

    .title {
      display: flex;
      align-items: center;
      height: 34px;
      background-color: #edf1f7;

      .dot {
        margin: 0 7px 0 14px;
        width: 10px;
        height: 10px;
        background-color: #0061ff;
      }

      .title_text {
        font-size: 16px;
        font-weight: bold;
      }

      .title_desc {
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 12px;
      }
    }

    .check_box {
      margin: 15px 0;
      line-height: 30px;
    }

    .radio_box {
      margin-top: 13px;
      font-size: 16px;
      font-weight: bold;
    }

    .radio_list {
      margin-top: 12px;
      font-size: 16px;
    }

    .btn {
      display: flex;
      justify-content: flex-end;
      margin-top: 20px;
    }
  }
}
</style>
