<template>
  <div>
    <Toptitle title="查看">
      <Button
        @click="handleGoProduction"
        type="primary"
        style="margin-right: 10px"
        >下生产</Button
      >
      <Button @click="back" type="primary" ghost style="margin-right: 10px"
        >返回</Button
      >
    </Toptitle>
    <Row style="padding: 10px;font-size: 18px;">
      <Col span="6">
        <span>项目编号：</span><span>{{ project_number }}</span>
      </Col>
      <Col span="6">
        <span>项目名称：</span><span>{{ project_name }}</span>
      </Col>
    </Row>
    <div class="context-tabs">
      <Row type="flex" align="middle" style="padding:10px 0">
        <Col span="4">
          <span>图号：</span>
          <span>
            <Select
              filterable
              multiple
              filter-by-label
              transfer
              v-model="matchedInfo.image_number"
              :max-tag-count="2"
              size="small"
              style="width: 150px"
            >
              <Option
                v-for="(item, index) in urlMatchedList"
                :key="index"
                :label="item"
                :value="item"
              ></Option>
            </Select>
          </span>
        </Col>
        <Col span="4">
          <span>产品名称：</span>
          <span>
            <Select
              filterable
              multiple
              filter-by-label
              transfer
              v-model="matchedInfo.product_name"
              :max-tag-count="2"
              size="small"
              style="width: 150px"
            >
              <Option
                v-for="(item, index) in productMatchedList"
                :key="index"
                :label="item"
                :value="item"
              ></Option>
            </Select>
          </span>
        </Col>
        <Col span="2">
          <Button @click="initData" size="small" type="primary">
            搜索
          </Button>
        </Col>
      </Row>
      <div
        v-for="matched_info in matchedList"
        :key="matched_info.number"
        class="matched-block"
      >
        <Row>
          <Col span="2">
            <span>图号：{{ matched_info.image_number }}</span>
          </Col>
          <Col span="6">
            <span>产品名称：{{ matched_info.product_name }}</span>
          </Col>
          <Col span="2">
            <span>{{
              matched_info.matching_status == 0
                ? "未匹配"
                : matched_info.matching_status == 1
                ? "匹配中"
                : "匹配完成"
            }}</span>
          </Col>
          <Col span="2" offset="8">
            <span>
              <Button
                @click="handleGoProduction(matched_info)"
                type="primary"
                size="small"
                >下生产</Button
              >
            </span>
          </Col>
          <Col span="2">
            总计
            <span style="color:red">{{ matched_info.num }}</span>
            条数据
          </Col>
          <Col span="2">
            <Button
              @click="handleShowCurrencyMatched(matched_info)"
              size="small"
              type="text"
              >{{ matched_info.isCurrenct ? "收缩" : "展开" }}</Button
            >
            <Icon
              size="20"
              :type="
                matched_info.isCurrenct
                  ? 'md-arrow-dropdown'
                  : 'md-arrow-dropright'
              "
              style="vertical-align: middle;"
            />
          </Col>
        </Row>
        <Row style="margin-top:20px" v-if="matched_info.isCurrenct">
          <Col span="4">
            <span>区域名称：</span>
            <span>
              <Select
                filterable
                multiple
                transfer
                filter-by-label
                v-model="matchedInfo.region"
                :max-tag-count="2"
                size="small"
                style="width: 150px"
              >
                <Option
                  v-for="(item, index) in regionMatchedList"
                  :key="index"
                  :label="item"
                  :value="item"
                ></Option>
              </Select>
            </span>
          </Col>
          <Col span="4">
            <span>房号：</span>
            <span>
              <Select
                filterable
                multiple
                transfer
                filter-by-label
                v-model="matchedInfo.number"
                :max-tag-count="2"
                size="small"
                style="width: 150px"
              >
                <Option
                  v-for="(item, index) in numberMatchedList"
                  :key="index"
                  :label="item"
                  :value="item"
                ></Option>
              </Select>
            </span>
          </Col>
          <Col span="4">
            <span>部件名称：</span>
            <span>
              <Select
                filterable
                multiple
                transfer
                filter-by-label
                v-model="matchedInfo.part_name"
                :max-tag-count="2"
                size="small"
                style="width: 150px"
              >
                <Option
                  v-for="(item, index) in partNameMatchedList"
                  :key="index"
                  :label="item"
                  :value="item"
                ></Option>
              </Select>
            </span>
          </Col>
          <Col span="2">
            <Button
              @click="getMatchedDetailList(matched_info)"
              size="small"
              type="primary"
            >
              搜索
            </Button>
          </Col>
        </Row>
        <div v-if="matched_info.isCurrenct">
          <div
            v-for="(matched_detail, index) in matchedDetailList"
            :key="index"
          >
            <Row type="flex" align="middle">
              <Col span="10" style="padding:10px 0">
                <span>匹配信息：</span>
                <span
                  v-for="(matched_info, index) in matched_detail.special"
                  :key="index"
                >
                  {{ matched_info.key }}:{{ matched_info.value }}
                </span>
                <span>{{ matched_detail.num }}组</span>
              </Col>
              <Col span="2" offset="12" style="padding:10px 0">
                <Button
                  @click="handleHiddenMatchedDetail(matched_detail)"
                  size="small"
                  type="text"
                >
                  {{ matched_detail.isHidden ? "展开" : "收缩" }}
                </Button>
              </Col>
            </Row>
            <Table
              v-show="!matched_detail.isHidden"
              :columns="matchedSheetTableColumns"
              border
              :max-height="500"
              :data="matched_detail.detail"
            >
            </Table>
          </div>
        </div>
      </div>
    </div>
    <Modal v-model="processModal" title="下生产">
      <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>
      <div slot="footer">
        <Button
          @click="processModal = false"
          type="primary"
          ghost
          style="margin-right: 10px"
          >取消</Button
        >
        <Button
          @click="handleProcess"
          type="primary"
          style="margin-right: 10px"
          :disabled="process_control"
          >确定</Button
        >
      </div>
    </Modal>
  </div>
</template>

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

export default {
  name: "",
  components: {},
  props: {},
  // import引入的组件需要注入到对象中才能使用
  data() {
    // 这里存放数据
    return {
      project_number: this.$route.query.project_number,
      project_name: this.$route.query.project_name,
      matchedInfo: {
        id: this.$route.query.id,
        image_number: "",
        product_name: "",
        region: [],
        number: [],
        part_name: [],
      },
      urlMatchedList: [],
      productMatchedList: [],
      regionMatchedList: [],
      numberMatchedList: [],
      partNameMatchedList: [],
      matchedDetailList: [],
      matchedList: [],
      matchedSheetTableColumns: [
        { title: "序号", align: "center", type: "index", minWidth: 60 },
        { title: "区域名称", align: "center", key: "region", minWidth: 100 },
        { title: "房号", align: "center", key: "house_number", minWidth: 60 },
        { title: "部件名称", align: "center", key: "part_name", minWidth: 80 },
        { title: "行号", align: "center", key: "line", minWidth: 70 },
        { title: "木皮1", align: "center", key: "skin1", minWidth: 60 },
        {
          title: "原材料",
          align: "center",
          minWidth: 80,
          render: (h, params) => {
            return h("span", {}, params.row.board1 + params.row.board2);
          },
        },
        { title: "木皮2", align: "center", key: "skin2", minWidth: 80 },
        { title: "毛料尺寸", align: "center", key: "wool_size", minWidth: 80 },
        {
          title: "原料数量",
          align: "center",
          key: "wool_number",
          minWidth: 80,
        },
        { title: "精裁尺寸", align: "center", key: "cut_size", minWidth: 80 },
        {
          title: "零部件数量",
          align: "center",
          key: "num",
          minWidth: 90,
        },
        { title: "贴皮面积", align: "center", key: "stick", minWidth: 80 },
        { title: "精裁面积", align: "center", key: "cut", minWidth: 80 },
        { title: "工艺要求", align: "center", key: "remark", minWidth: 80 },
      ], //匹配表头
      processModal: false,
      process_man: "",
      processManList: [],
      process_start_time: "",
      process_end_time: "",
      process_control: false,
      cut_order_product_ids: [],
    };
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.axios("/api/user").then(
      (res) => (this.processManList = res.data.data)
    );
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.initData();
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    handleGoProduction(row) {
      if (row) {
        this.cut_order_product_ids = [row.id];
      } else {
        this.cut_order_product_ids = this.matchedList.map((v) => {
          return v.id;
        });
      }
      this.processModal = true;
    },
    handleProcess() {
      this.process_control = true;
      this.axios({
        method: "post",
        url: "/api/bst_pull",
        data: {
          cut_order_product_ids: this.cut_order_product_ids,
          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.processModal = false;
        }
        setTimeout(() => {
          this.process_control = false;
        }, 500);
      });
    },
    initData() {
      this.axios
        .get("/api/bst_matching_on_list", {
          params: {
            id: this.matchedInfo.id,
            image_number: this.matchedInfo.image_number,
            product_name: this.matchedInfo.product_name,
          },
        })
        .then((res) => {
          console.log(res);
          res.data.list.map((v) => {
            v.isCurrenct = false;
          });
          this.urlMatchedList = res.data.image_number;
          this.productMatchedList = res.data.product_name;
          this.matchedList = res.data.list;
        });
    },
    handleHiddenMatchedDetail(row) {
      if (row.isHidden) {
        row.isHidden = !row.isHidden;
      } else {
        row.isHidden = true;
      }
      this.$forceUpdate();
    },
    handleShowCurrencyMatched(row) {
      if (row.isCurrenct) {
        row.isCurrenct = !row.isCurrenct;
      } else {
        this.matchedList.map((v) => (v.isCurrenct = false));
        row.isCurrenct = true;
      }
      this.getMatchedDetailList(row);
    },
    getMatchedDetailList(row) {
      row.isCurrenct &&
        this.axios
          .post("/api/bst_matching_on_detail", {
            cut_order_product_id: row.id,
            id: this.matchedInfo.id,
            number: this.matchedInfo.number,
            part_name: this.matchedInfo.part_name,
            region: this.matchedInfo.region,
          })
          .then((res) => {
            this.regionMatchedList = res.data.region;
            this.numberMatchedList = res.data.number;
            this.partNameMatchedList = res.data.part_name;
            this.matchedDetailList = res.data.list;
          });
    },
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang="scss" scoped>
.context-tabs {
  position: relative;
  max-height: 650px;
  overflow: hidden;
  overflow-y: auto;
  padding: 15px;
  margin: 10px;
  margin-bottom: 30px;
  border-radius: 5px;
  box-shadow: 1px 1px 5px 1px #999;
}
.matched-block {
  padding: 15px;
  margin-bottom: 30px;
  background-color: #e9ecef;
  border-radius: 5px;
}
.process_modal {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px;
}
</style>
