<template>
  <div class="BidsList">
    <FullPage
      title="生产拆单列表"
      :list="set_list"
      @init="init"
      :loading="loading"
      @searchData="init"
      @selectTable="selectTable"
      @changePage="changePage"
      @changeSize="changeSize"
      :tableColums="tableColums"
      :showPage="false"
      :tableData="tableData"
      :page_index="page_index"
      :total="total"
    >
      <div slot="titleButton" style="display: flex">
        <Upload
          name="your_file"
          :show-upload-list="false"
          :headers="headers"
          :on-error="uploadError"
          :on-success="uploadSuccess"
          :action="$store.state.ip + '/api/measure_orders_import'"
        >
          <Button type="success" ghost icon="md-exit" style="margin-right: 10px"
            >导入</Button
          >
        </Upload>
        <Button
          @click="exportData"
          type="warning"
          ghost
          icon="md-return-left"
          style="margin-right: 10px"
          >导出</Button
        >
      </div>
      <!-- <div slot='navButton'
           style="display:flex;">
        <Button v-if='persimissionData["表头设置"]||persimissionData.all'
                @click="setupTableHeader"
                type="primary"
                ghost
                icon='ios-cog'>表头设置</Button>
      </div> -->
      <template slot="basicTypeSet" slot-scope="{ row }">
        <div>
          <span
            v-for="item in warningList"
            :key="item.id"
            :style="{ color: item.color }"
            v-show="item.id == row.warning_state"
            >{{ item.title }}</span
          >
        </div>
      </template>
      <template slot="set" slot-scope="{ row, index }">
        <div>
          <a
            style="margin: 0 5px"
            :disabled="row.is_click == 0"
            @click="handleSet(row, index, 1)"
            >下生产</a
          >
          <a
            style="margin: 0 5px"
            :disabled="row.sub_status == 3"
            @click="handleSet(row, index, 2)"
            >拆单</a
          >
          <a style="margin: 0 5px" @click="handleSet(row, index, 3)">详情</a>
          <a
            style="margin: 0 5px;"
            :disabled="row.sub_status != 2"
            :style="row.sub_status != 2 ? 'margin: 0 5px;' : 'margin: 0 5px;'"
            @click="handleSet(row, index, 4)"
            >删除</a
          >
          <a
            style="margin: 0 5px"
            :disabled="row.sub_status != 2"
            @click="handleSet(row, index, 5)"
            >成本预算</a
          >
        </div>
      </template>
      <template slot="pageSlot">
        <div class="pageSlotStyle">
          <Page
            :page-size-opts="[10, 20, 30, 40, 100, 1000]"
            @on-page-size-change="changeSize"
            @on-change="changePage"
            :current="page_index"
            show-total
            :total="total"
            show-sizer
            :page-size="page_size"
          />
        </div>
      </template>
    </FullPage>
    <Modal
      v-model="processModal"
      title="下生产"
      @on-ok="handleProcess"
      @on-cancel="processModal = false"
    >
      <div>
        <div class="process_modal">
          <span>生产人员:</span>
          <Select v-model="process_man" style="width: 150px">
            <Option
              v-for="item in processManList"
              :key="item.id"
              :label="item.nickname"
              :value="item.id"
            ></Option>
          </Select>
        </div>
        <div class="process_modal">
          <span>选择时间:</span>
          <DatePicker
            type="date"
            v-model="process_start_time"
            placeholder="年/月/日"
            style="width: 150px"
          ></DatePicker>
          至
          <DatePicker
            type="date"
            v-model="process_end_time"
            placeholder="年/月/日"
            style="width: 150px"
          ></DatePicker>
        </div>
      </div>
    </Modal>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';

export default {
  name: "BidSystemContractList",
  data() {
    // 这里存放数据
    return {
      tableColums: [
        {
          type: "selection",
          align: "center",
          key: "selection",
          minWidth: 100,
          fixed: "left",
          title: "全选",
        },
        { title: "订单号", align: "center", key: "order_no", minWidth: 150 },
        {
          title: "项目名称",
          align: "center",
          key: "residential_name",
          minWidth: 120,
        },
        { title: "详细地址", align: "center", key: "address", minWidth: 200 },
        // {
        //   title: '订单类型', align: 'center', key: 'renovation_type', minWidth: 100,
        //   render: (h, params) => h('span', {}, params.row.renovation_type == 1 ? '工装' : '家装')
        // },
        {
          title: "客户姓名",
          align: "center",
          key: "client_name",
          minWidth: 120,
        },
        { title: "手机号", align: "center", key: "mobile", minWidth: 150 },
        {
          title: "紧急程度",
          align: "center",
          key: "warning_state",
          minWidth: 100,
          slot: "basicTypeSet",
        },
        {
          title: "收款",
          align: "center",
          key: "pay_state",
          minWidth: 80,
          render: (h, params) =>
            h("span", {}, params.row.pay_state == 1 ? "是" : "否"),
        },
        { title: "业务员", align: "center", key: "salesman", minWidth: 100 },
        {
          title: "开始日期",
          align: "center",
          key: "start_time",
          minWidth: 150,
          render: (h, params) =>
            h("span", {}, this.func.replaceDateNoHMS(params.row.start_time)),
        },
        {
          title: "交付日期",
          align: "center",
          key: "end_time",
          minWidth: 150,
          render: (h, params) =>
            h("span", {}, this.func.replaceDateNoHMS(params.row.end_time)),
        },
        {
          title: "拆单状态",
          align: "center",
          key: "explode_status",
          minWidth: 100,
          render: (h, params) =>
            h(
              "span",
              {},
              // params.row.explode_status == 0
              //   ? "待审核"
              params.row.explode_status == 0
                ? "未拆单"
                : params.row.explode_status == 1
                ? "拆单中"
                : "拆单完成"
            ),
        },
        {
          title: "下单日期",
          align: "center",
          key: "crt_time",
          minWidth: 150,
          render: (h, params) =>
            h("span", {}, this.func.replaceDateNoHMS(params.row.crt_time)),
        },
        {
          title: "操作",
          align: "center",
          key: "set",
          slot: "set",
          fixed: "right",
          minWidth: 300,
          fixed: "right",
        },
      ],
      tableData: [],
      page_index: 1,
      page_size: 10,
      total: 0,
      loading: false,
      proxyObj: {},
      headers: { Authorization: localStorage.getItem("token") },
      selects: [],
      processModal: false,
      process_man: "",
      processManList: [],
      process_start_time: "",
      process_end_time: "",
      order_no: "",
      warningList: [],
    };
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.axios("/api/user").then(
      (res) => (this.processManList = res.data.data)
    );
    // 获取紧急程度
    this.axios.get("/api/warning_list").then((res) => {
      this.warningList = res.data.data;
    });
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  methods: {
    handleProcess() {
      this.axios({
        method: "get",
        url: "/api/order_area_pull",
        params: {
          order_no: this.order_no,
          sub_status: 3,
          process_man: this.process_man,
          process_start_time: this.func.replaceDateNoHMS(
            this.process_start_time
          ),
          process_end_time: this.func.replaceDateNoHMS(this.process_end_time),
        },
      }).then((res) => {
        if (res.code == 200) {
          this.$Message.success(res.msg);
          this.getData(this.proxyObj);
        }
      });
    },
    // 1下生产  2拆单  3详情 4删除 5成本预算
    handleSet(row, index, type) {
      switch (type) {
        case 1:
          this.process_man = "";
          this.process_start_time = "";
          this.process_end_time = "";
          this.processModal = true;
          this.order_no = row.order_no;
          // this.$Modal.confirm({
          //   title: '确认下生产？',
          //   content: '确认后订单将下至生产，请确认！',
          //   onOk: () => {
          //     this.axios({
          //       method: 'get',
          //       url: '/api/order_area_pull',
          //       params: {
          //         order_no: row.order_no,
          //         sub_status: 2
          //       }
          //     }).then((res) => {
          //       this.$Message.success(res.msg)
          //       this.getData(this.proxyObj)
          //     }).catch((err) => {});
          //   },
          //   onCancel: () => { }
          // })
          break;
        case 2:
          this.$router.push({
            path: "/cms/BidSystem/ProductDeOrder/detail",
            query: {
              type,
              order_no: row.order_no,
              oa_id: row.oa_id,
            },
          });
          break;
        case 3:
          this.$router.push({
            path: "/cms/measurementordermannage/edit",
            query: {
              type,
              order_no: row.order_no,
              oa_id: row.oa_id,
              editType: 1,
              // orders_area_product_detail_id:row.id
            },
          });
          break;
        case 4:
          this.$Modal.confirm({
            title: "确认删除？",
            content: "此操作确认后不可恢复，请确认！",
            onOk: () => {
              this.axios({
                method: "get",
                url: "/api/order_area_del",
                params: {
                  order_no: row.order_no,
                  sub_status: 2,
                },
              })
                .then((res) => {
                  this.$Message.success(res.msg);
                  this.getData(this.proxyObj);
                })
            },
            onCancel: () => {},
          });
          break;
        case 5:
          this.$router.push({
            path: "/cms/BidSystem/ProductDeOrder/budget",
            query: {
              type,
              order_no: row.order_no,
            },
          });
          break;
      }
    },
    init(row) {
      this.page_index = 1;
      // 2拆单  换接口 弃用
      // row.sub_status = 2;
      if (!row.explode_status&&row.explode_status!=0) {
        row.explode_status = 99
      }
      row.page_index = this.page_index;
      row.page_size = this.page_size;
      this.proxyObj = row;
      this.getData(row);
    },
    getData(row) {
      this.loading = true;
      this.axios("/api/order_area_explode_list", { params: row }).then((res) => {
        this.loading = false;
        this.tableData = res.data.data;
        this.total = res.data.total;
        this.tableheaders = res.data.tableSet || [];
      });
    },
    changePage(e) {
      this.page_index = e;
      this.proxyObj.page_index = this.page_index;
      this.getData(this.proxyObj);
    },
    changeSize(e) {
      this.page_size = e;
      this.proxyObj.page_size = this.page_size;
      this.getData(this.proxyObj);
    },
    async exportData() {
      const res = await this.axios("/api/production_export", {
        params: { ...this.proxyObj },
      });
      if (res.code == 200) {
        let url = `${this.$store.state.ip}/api/storage/${res.data.file}`;
        location.href = url;
      }
    },
    uploadSuccess(res) {
      if (res.code == 200) {
        this.$Message.success(res.msg || "上传成功");
      } else {
        this.$Message.warning(res.msg || "上传失败");
      }
      this.getData(this.proxyObj);
    },
    uploadError(err) {
      this.$Message.error(err.msg || "上传失败");
    },
    selectTable(e) {
      this.selects = e;
    },
  },
  // 监听属性 类似于data概念
  computed: {
    set_list() {
      return [
        {
          title: "订单编号",
          serverName: "order_no",
          name: "Input",
          value: "",
          placeholder: "请输入订单号",
        },
        {
          title: "项目名称",
          name: "Input",
          placeholder: "请输入",
          value: "",
          serverName: "residential_name",
        },
        {
          title: "客户昵称",
          name: "Input",
          placeholder: "请输入",
          value: "",
          serverName: "client_name",
        },
        {
          title: "手机号",
          name: "Input",
          placeholder: "请输入",
          value: "",
          serverName: "mobile",
        },
        {
          title: "拆单状态",
          name: "Select",
          placeholder: "请选择",
          serverName: "explode_status",
          value: 99,
          option: [
            //{ label: "待审核", value: 0 },
            { label: "未拆单", value: 0 },
            { label: "拆单中", value: 1 },
            { label: "拆单完成", value: 2 },
          ],
        },
        {
          title: "紧急程度",
          name: "Select",
          serverName: "warning_state",
          placeholder: "请选择",
          value: "",
          optionName: "title",
          optionValue: "id",
          option: this.warningList,
        },
      ];
    },
  },
  // 监控data中的数据变化
  watch: {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang="scss" scoped>
.pageSlotStyle {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}
.process_modal {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px;
}
</style>
