<template>
  <div class="dialogs">
    <!-- 订单打印 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogShow"
      width="500px"
      append-to-body
    >
      <div v-if="type == 'print'">
        <el-form
          ref="form"
          :model="printForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="选择打印机" prop="sn">
            <el-select v-model="printForm.sn" placeholder="请选择打印机">
              <el-option
                v-for="item in printList"
                :key="item.value"
                :label="item.name"
                :value="item.sn"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="type == 'offline'">
        <el-form ref="form" :model="offline" :rules="rules" label-width="100px">
          <el-form-item label="是否同意" prop="auditFirstUse">
            <el-radio-group v-model="offlineForm.confirmOfflinePayment">
              <el-radio :label="0" :name="0">用户已支付</el-radio>
              <el-radio :label="1" :name="1">用户未支付</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div v-if="type == 'prior'">
        <el-form
          ref="form"
          :model="priorForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="是否同意" prop="auditFirstUse">
            <el-radio-group v-model="priorForm.auditFirstUse">
              <el-radio :label="0" :name="0">同意</el-radio>
              <el-radio :label="1" :name="1">拒绝</el-radio>
            </el-radio-group>
          </el-form-item>
          <div>同意时:确认收货3天内.需确认用户是否完成支付</div>
          <div>拒绝时:将自动取消该订单.用户支付服务费将原路退回</div>
        </el-form>
      </div>
      <div v-if="type == 'taking'">
        <el-form
          ref="form"
          :model="priorForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="配送方式" prop="auditFirstUse">
            <el-radio-group v-model="priorForm.auditFirstUse">
              <el-radio :label="0" :name="0">找骑手</el-radio>
              <el-radio :label="1" :name="1">自行配送</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogSubmit">确 定</el-button>
        <el-button @click="dialogShow = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  auditFirstUserOrder,
  confirmUserOfflinePayment,
} from "@/api/manage/order";
import { printOrder, equipmentList } from "@/api/manage/equipment";
export default {
  props: {},
  data() {
    return {
      rules: {
        sn: [{ required: true, message: "请选择打印机", trigger: "blur" }],
        auditFirstUse: [{ required: true, message: "请选择", trigger: "blur" }],
      },
      title: "",
      printList: [],
      printForm: {
        sn: "",
      },
      offlineForm: {
        confirmOfflinePayment: "",
      },
      priorForm: {
        auditFirstUse: "",
      },
      dialogShow: false,
      type: "",
      tableInfo: null,
    };
  },
  mounted() {
    this.getEquipmentList();
  },
  methods: {
    getEquipmentList() {
      equipmentList().then((res) => {
        if (res.code == 200) {
          this.printList = res.data.records;
        }
      });
    },
    openDialog(type, list) {
      this.type = type;
      if (type == "print") {
        this.title = "打印订单";
        this.dialogShow = true;
        this.tableInfo = list;
      } else if (type == "prior") {
        this.title = "确认是否同意所选用户使用先用后付";
        this.tableInfo = list;
        this.dialogShow = true;
      } else if (type == "offline") {
        this.title = "确认所选用户是否已进行线下协商支付";
        this.tableInfo = list;
        this.dialogShow = true;
      } else if (type == "taking") {
        this.title = "选择配送方式";
        this.tableInfo = list;
        this.dialogShow = true;
      }
    },

    dialogSubmit() {
      if (this.type == "print") {
        this.printSubmit();
      } else if (this.type == "prior") {
        this.priorSubmit();
      } else if (this.type == "offline") {
        this.offlineSubmit();
      } else if (this.type == "taking") {
        this.takingSubmit();
      }
    },
    takingSubmit() {
      //接单
    },
    offlineSubmit() {
      confirmUserOfflinePayment({
        id: this.tableInfo.id,
        confirmOfflinePayment: this.offlineForm.confirmOfflinePayment,
      }).then((res) => {
        if (res.code == 200) {
          this.dialogShow = false;
          this.$modal.msgSuccess("操作成功！");
          this.$emit("orderDialogReturn", "offline");
        } else {
          this.dialogShow = false;
          this.$modal.msgError(res.msg);
        }
      });
    },
    priorSubmit() {
      auditFirstUserOrder({
        id: this.tableInfo.id,
        auditFirstUse: this.priorForm.auditFirstUse,
      }).then((res) => {
        if (res.code == 200) {
          this.dialogShow = false;
          this.$modal.msgSuccess("操作成功！");
          this.$emit("orderDialogReturn", "prior");
        } else {
          this.dialogShow = false;
          this.$modal.msgError(res.msg);
        }
      });
    },
    printSubmit() {
      if (!this.printForm.sn) {
        this.$message.error("请选择打印机！");
        return;
      }
      this.printForm.carPayNos = this.tableInfo.toString();
      printOrder(this.printForm).then((res) => {
        if (res.code == 200) {
          this.dialogShow = false;
          this.tableInfo = [];
          this.$modal.msgSuccess("操作成功！");
          this.$emit("orderDialogReturn", "print");
        } else {
          this.dialogShow = false;
          this.$modal.msgError(res.msg);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
